diff --git a/ffi/Cargo.lock b/ffi/Cargo.lock index 4d2d35650..818c06eb6 100644 --- a/ffi/Cargo.lock +++ b/ffi/Cargo.lock @@ -8,6 +8,8 @@ version = "0.0.0" dependencies = [ "bytes", "dep-fixture", + "serde", + "serde_json", "url", ] diff --git a/ffi/MIGRATE.md b/ffi/MIGRATE.md index a5ef742af..ca21a6578 100644 --- a/ffi/MIGRATE.md +++ b/ffi/MIGRATE.md @@ -2,6 +2,11 @@ ## Unreleased +- A type parameter bounded by serde's `Serialize` or `Deserialize` takes + `serde_json::Value`, not `String`: C passes a `_json` it parses from JSON text + with `_json_parse`, not a buffer, and a generic type at such a parameter is + named by `json` (`moq_json_snapshot_json`), not `string`. An item that clashes with + `_json` or its functions takes a `rename`. - A type parameter whose bounds imply bytes, through a supertrait such as moq's `IntoBytes: AsRef<[u8]>` or a `From` implementation such as moq's `impl> From for Timed`, exports as bytes rather than diff --git a/ffi/README.md b/ffi/README.md index 8d4c1d4ec..93b6385bf 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -76,6 +76,7 @@ export's, and adds nothing; another crate's type is refused. | `Duration` | `_duration` | | `SocketAddr`, `SocketAddrV4`, `SocketAddrV6` | `_socket_addr`: its IP's bytes, port, and whether it is IPv6; one of the other family passed to `V4` or `V6` aborts | | `url::Url` | an object `_url`, made by `_url_parse`, which fails with the parse error's message, and printed by `_to_string` | +| `serde_json::Value` | an object `_json`, made by `_json_parse` from JSON text, which fails with serde_json's message, and read back by `_to_text`; its contents are the caller's JSON library's to read | | `Vec` and `&[T]` of any other `T` | `_vec_`: an array of `T`'s C values, viewed passed in and owned returned, a slice as a clone; of references only returned | | `Waker`, taken by value | `_waker`, as a future's poll takes it | | `Range`, `Bound` of an integer | `_range_`, `_bound_`: a `_bound` kind and a value | @@ -83,8 +84,9 @@ export's, and adds nothing; another crate's type is refused. | `Option` | a null handle or buffer, or `_option_` | | a `pub const` | a `#define` if a `bool` or integer, else a function returning it | | `impl Into`, `impl AsRef`, or a bound implying one, as a supertrait does | `X`, or `&X` and then `String` or `Vec` where `X` is `str` or `[T]` | -| `impl Into`, where `X` does not map | what a `From` implementation into `X` takes, by its source parameter's bounds as above: `impl> From for Timed` takes `Bytes` | -| another type parameter | the first of `String`, `Bytes`, and `Vec` meeting its bounds, or a foreign implementation of its one exported trait; else an instance of the function at each type the exports implement a trait bounding it for, as `&X` where that meets its bounds too, so C keeps its handle, and, as an argument of a generic type, at each argument of an instance that meets its bounds and makes each generic type the signature names an instance | +| `impl Into`, where `X` does not map | what a `From` implementation into `X` takes: a source parameter by its bounds as above, so `impl> From for Timed` takes `Bytes`, and any other source as it is, so `impl<'a, V> From<&'a V> for Timed<&'a V>` takes `&V` | +| `impl IntoIterator`, where `X` is no type parameter | `Vec` | +| another type parameter | `serde_json::Value` for a serde `Serialize` or `Deserialize` bound, else the first of `String`, `Bytes`, and `Vec` meeting its bounds, or a foreign implementation of its one exported trait; else an instance of the function at each type the exports implement a trait bounding it for, as `&X` where that meets its bounds too, so C keeps its handle, and, as an argument of a generic type, at each argument of an instance that meets its bounds and makes each generic type the signature names an instance | A generic type infers the instances it exports at, with no config: the one at its defaults, if each type parameter has one; the one each inherent impl is of, such as diff --git a/ffi/fixtures/api/Cargo.toml b/ffi/fixtures/api/Cargo.toml index 7ce19da21..12f3de2bb 100644 --- a/ffi/fixtures/api/Cargo.toml +++ b/ffi/fixtures/api/Cargo.toml @@ -13,6 +13,8 @@ name = "api_lib" [dependencies] bytes = "1.11.1" dep-fixture = { path = "../dep" } +serde = "1.0.229" +serde_json = "1.0.154" url = "2.5.8" [lints] diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 6be34e648..87c8f301a 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -371,6 +371,22 @@ fn main() { api_lib::worth(None, Box::default()) ); println!("sums {:?}", api_lib::sums(vec![vec![1, 2], Vec::new(), vec![3]])); + let summed = api_lib::summed([Point { x: 1, y: 2 }, Point { x: -4, y: 6 }]); + let none = api_lib::summed([]); + println!("summed {} {} {} {}", summed.x, summed.y, none.x, none.y); + println!( + "longest {} {}", + api_lib::longest(["a".to_string(), "abc".to_string()]), + api_lib::longest([]) + ); + let doc: serde_json::Value = r#"{"b": [1, 2.5, null], "a": "\u00e9"}"#.parse().expect("a JSON document"); + println!("json {}", api_lib::encode(&doc)); + println!("timed text {}", api_lib::timed_text(&doc)); + println!("tagged {}", api_lib::tagged(doc, "t")); + match "{\"a\":".parse::() { + Ok(doc) => println!("json parse {doc}"), + Err(err) => println!("json parse err {err}"), + } let site = api_lib::Site::new(base); println!("site {} {}", site.home, site.home); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 2a259452c..1f0fef4b1 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -662,6 +662,18 @@ pub fn timed_hex(payload: impl Into> + 'static) -> String { hex(payload.into().value) } +/// A reference stamped with no time, as moq's `Timed` converts one. +impl<'a, V> From<&'a V> for Timed<&'a V> { + fn from(value: &'a V) -> Self { + Self { value, at: None } + } +} + +/// A timed value's compact JSON text, taken as moq's JSON tracks take theirs. +pub fn timed_text<'a, T: serde::Serialize + 'a>(value: impl Into>) -> String { + serde_json::to_string(value.into().value).expect("a JSON document serializes") +} + pub fn greet(name: impl Into, shape: Shape) -> String { format!("hello {}, a {shape:?}", name.into()) } @@ -1037,6 +1049,29 @@ pub fn sums(rows: Vec>) -> Vec { rows.iter().map(|row| row.iter().sum()).collect() } +/// The points added up, from anything that iterates them. +pub fn summed(points: impl IntoIterator) -> Point { + points.into_iter().fold(Point::default(), |sum, p| sum.offset(p.x, p.y)) +} + +/// The length of the longest word, naming its iterator. +pub fn longest(words: I) -> usize +where + I: IntoIterator, +{ + words.into_iter().map(|word| word.len()).max().unwrap_or(0) +} + +/// `value` as compact JSON text: a serde bound takes a JSON document. +pub fn encode(value: &T) -> String { + serde_json::to_string(value).expect("a JSON document serializes") +} + +/// `doc` under `tag`, as a new document. +pub fn tagged(doc: serde_json::Value, tag: &str) -> serde_json::Value { + serde_json::json!({ "tag": tag, "doc": doc }) +} + /// A site: every field is `pub`, but a URL is a handle C cannot copy, so it is an object /// whose getter returns a clone. pub struct Site { diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index babb04339..9c43664d7 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -43,6 +43,10 @@ fixtures/reject/src/lib.rs:320:1: rust2-ffi: cannot export fn `count_some`: Vec fixtures/reject/src/lib.rs:325:1: rust2-ffi: cannot export fn `count_slice`: slice of references `&str`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:330:1: rust2-ffi: cannot export fn `peek_all`: reference to a handle inside a value `[Opaque]`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:335:1: rust2-ffi: cannot export fn `none_opaque`: borrowed return that is not Clone `[Opaque]`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:347:1: rust2-ffi: cannot export fn `stash`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:362:1: rust2-ffi: cannot export fn `hold`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:367:1: rust2-ffi: cannot export fn `bound`: generic parameter `std::convert::Into`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:372:1: rust2-ffi: cannot export fn `kept`: generic parameter `std::convert::Into`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:40:1: rust2-ffi: cannot export fn `peek`: reference to a handle inside a value `std::boxed::Box`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:45:1: rust2-ffi: cannot export static `COUNT`: static; exclude it in the config to leave it out fixtures/reject/src/lib.rs:49:1: rust2-ffi: cannot export macro `noop`: macro; exclude it in the config to leave it out diff --git a/ffi/fixtures/reject/ffi.toml b/ffi/fixtures/reject/ffi.toml index f989b49a4..17ff99bfe 100644 --- a/ffi/fixtures/reject/ffi.toml +++ b/ffi/fixtures/reject/ffi.toml @@ -1,7 +1,7 @@ package = "reject-fixture" prefix = "reject" # An exclusion leaves an item or an instance out, so nothing refuses it. -exclude = ["Cell", "Excluded"] +exclude = ["Cell", "Excluded", "Held"] # A field that is none: `min` names nothing of `Limit`. [rename] diff --git a/ffi/fixtures/reject/src/lib.rs b/ffi/fixtures/reject/src/lib.rs index 86425fb3a..24f40be9a 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -335,3 +335,40 @@ pub fn peek_all(values: &[Opaque]) -> usize { pub fn none_opaque() -> &'static [Opaque] { &[] } + +/// A conversion only from what outlives every call, which C cannot lend. +impl From<&'static u32> for Excluded { + fn from(_: &'static u32) -> Self { + Excluded + } +} + +/// Takes only a `'static` borrow, through `Excluded`'s `From`. +pub fn stash(value: impl Into) -> u32 { + let _ = value.into(); + 0 +} + +/// Excluded, borrowing what it converts from. +pub struct Held<'a>(pub &'a u32); + +impl<'a> From<&'a u32> for Held<'a> { + fn from(value: &'a u32) -> Self { + Held(value) + } +} + +/// Takes a borrow only for as long as `'static`, which the target's lifetime demands. +pub fn hold(value: impl Into>) -> u32 { + *value.into().0 +} + +/// Takes a borrow only for as long as `'static`, which its own lifetime bound demands. +pub fn bound<'a: 'static>(value: impl Into>) -> u32 { + *value.into().0 +} + +/// Takes a borrow only for as long as `'static`, which its parameter's bound demands. +pub fn kept<'a>(value: impl Into> + 'static) -> u32 { + *value.into().0 +} diff --git a/ffi/moq/ffi.toml b/ffi/moq/ffi.toml index 3ad8a54b0..77ba6466e 100644 --- a/ffi/moq/ffi.toml +++ b/ffi/moq/ffi.toml @@ -44,7 +44,6 @@ exclude = [ "track::Fetching", "track::Resolving", "track::Subscribing", - "track::Subscription::with_ranges", "transport::Error", "transport::MaybeSend", "transport::MaybeSync", @@ -84,7 +83,7 @@ exclude = [ # Exported with moq-net, whose signatures name their types, such as `Patterns` and `Instant`. [crates.moq-pattern] -exclude = ["Pattern::captures", "Pattern::new", "Patterns::iter"] +exclude = ["Pattern::captures", "Patterns::iter"] [crates.moq-clock] exclude = [ @@ -310,8 +309,6 @@ exclude = [ "catalog::Guard", "catalog::IntoRendition", "catalog::Producer::audio", - "catalog::Producer::json_snapshot", - "catalog::Producer::json_stream", "catalog::Producer::modify", "catalog::Producer::stats", "catalog::Producer::text", @@ -325,10 +322,7 @@ exclude = [ "catalog::Select", "catalog::Stream", "catalog::hang::CatalogExt", - "catalog::hang::Extra::get", "catalog::hang::Extra::iter", - "catalog::hang::Extra::remove", - "catalog::hang::Extra::set", "codec::aac::Config::parse", "codec::annexb::NalIterator", "codec::annexb::build_prefix", @@ -336,12 +330,9 @@ exclude = [ "codec::annexb::from_length_prefixed", "codec::annexb::length_prefixed_to_annexb", "codec::av1::Error", - "codec::av1::Import::decode", "codec::flac::Config", "codec::h264::Export", - "codec::h264::Import::decode", "codec::h265::Export", - "codec::h265::Import::decode", "codec::opus::Config::parse", "codec::opus::Mapping::new", "codec::opus::mapping::Config", @@ -370,8 +361,6 @@ exclude = [ "follow::Event", "follow::Instances", "json::Consumer", - "json::Snapshot", - "json::Stream", ] [crates.moq-mux.rename] diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 5d6412ebe..006b88327 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -52,6 +52,45 @@ static void lists_stats(void *data, moq_hang_catalog_extra *catalog) { moq_catalog_stats_free(stats); } +/* Whether `buf` holds `s` anywhere. */ +static int contains(moq_buf buf, const char *s) { + size_t len = strlen(s); + for (size_t i = 0; i + len <= buf.len; i++) { + if (memcmp(buf.ptr + i, s, len) == 0) { + return 1; + } + } + return 0; +} + +/* Sets the lent catalog's `app` section to the JSON document in data, in place. */ +static void sets_app(void *data, moq_hang_catalog_extra *catalog) { + moq_catalog_hang_extra *sections = moq_hang_catalog_extra_ext(catalog); + moq_mux_error *err = NULL; + CHECK(moq_catalog_hang_extra_set(sections, str("app"), data, &err)); + moq_hang_catalog_extra_set_ext(catalog, sections); + moq_catalog_hang_extra_free(sections); +} + +/* What C reads back from the lent catalog: its JSON, and its `app` section as text. */ +struct readback { + moq_buf json; + moq_buf app; +}; + +static void reads_back(void *data, moq_hang_catalog_extra *catalog) { + struct readback *back = data; + moq_hang_error *hang_err = NULL; + CHECK(moq_hang_catalog_extra_to_json(catalog, &back->json, &hang_err)); + moq_catalog_hang_extra *sections = moq_hang_catalog_extra_ext(catalog); + moq_json *app = moq_catalog_hang_extra_get(sections, str("app")); + if (app != NULL) { + back->app = moq_json_to_text(app); + moq_json_free(app); + } + moq_catalog_hang_extra_free(sections); +} + static int wakes = 0; static void wake(void *data) { @@ -276,6 +315,25 @@ int main(void) { moq_mux_error_free(mux_err); mux_err = NULL; + /* A subscription's ranges, from any iterable of them: an array, copied in. */ + moq_track_range ranges[] = { + moq_track_range_live(), + moq_track_range_groups((moq_bounds_u64){.start = {.kind = MOQ_BOUND_INCLUDED, .value = 2}, + .end = {.kind = MOQ_BOUND_EXCLUDED, .value = 6}}), + }; + moq_track_subscription *unranged = moq_track_subscription_default(); + moq_track_subscription *ranged = + moq_track_subscription_with_ranges(unranged, (moq_vec_track_range){.ptr = ranges, .len = 2}); + moq_track_subscription_free(unranged); + moq_vec_track_range got = moq_track_subscription_ranges(ranged); + CHECK(got.len == 2 && moq_track_range_eq(&got.ptr[0], &ranges[0])); + CHECK(got.len == 2 && got.ptr[1].start.tag == MOQ_TRACK_ANCHOR_ABSOLUTE && got.ptr[1].start.absolute._0 == 2); + CHECK(got.len == 2 && got.ptr[1].end.some && got.ptr[1].end.value.absolute._0 == 6); + moq_vec_track_range_free(got); + moq_track_range_free(&ranges[0]); + moq_track_range_free(&ranges[1]); + moq_track_subscription_free(ranged); + /* A catalog at moq-mux's untyped extension, from the config C builds, whose generic * `is_claimed` is named by the last segment of its argument, and whose `mutate` lends * the catalog to C, which sets its fields in place. */ @@ -298,6 +356,64 @@ int main(void) { int listed_stats = 0; CHECK(moq_catalog_producer_extra_mutate(catalog, lists_stats, &listed_stats, &mux_err)); CHECK(listed_stats == 1); + /* A JSON track the catalog lists, published through moq-mux's generic `json_snapshot` + * at a JSON document, and a section C parses from text, set in the lent catalog and + * read back as text. */ + moq_track_producer *status = NULL; + CHECK(moq_broadcast_producer_create_track(publisher, str("status"), NULL, &status, &err)); + moq_json_config *json_config = moq_json_config_default(); + moq_json_options *json_options = moq_json_options_default(); + moq_json_snapshot_json *status_snapshot = NULL; + CHECK(moq_catalog_producer_extra_json_snapshot_config(catalog, status, json_config, json_options, &status_snapshot, + &mux_err)); + moq_json_config_free(json_config); + moq_json_options_free(json_options); + moq_track_producer_free(status); + CHECK(is(moq_json_snapshot_json_name(status_snapshot), "status")); + CHECK(moq_catalog_producer_extra_is_claimed_json_config(catalog, str("status"))); + /* A value C parses is published as the track's latest, which a subscriber reads back + * as JSON text. */ + moq_json *latest = NULL; + moq_buf json_err; + CHECK(moq_json_parse(str("{\"on\": true}"), &latest, &json_err)); + CHECK(moq_json_snapshot_json_update(status_snapshot, latest, &mux_err)); + moq_json_free(latest); + moq_track_subscriber *status_subscriber = moq_json_snapshot_json_consume(status_snapshot); + moq_track_subscriber_recv_group_future *status_receiving = moq_track_subscriber_recv_group(status_subscriber); + moq_group_consumer *status_group = NULL; + CHECK(moq_track_subscriber_recv_group_poll(status_receiving, waker, &status_group, &err) == MOQ_POLL_READY); + moq_track_subscriber_recv_group_future_free(status_receiving); + moq_frame_frame *status_frame = NULL; + if (status_group != NULL) { + moq_group_consumer_read_frame_future *status_reading = moq_group_consumer_read_frame(status_group); + CHECK(moq_group_consumer_read_frame_poll(status_reading, waker, &status_frame, &err) == MOQ_POLL_READY); + moq_group_consumer_read_frame_future_free(status_reading); + moq_group_consumer_free(status_group); + } + CHECK(status_frame != NULL && is(moq_frame_frame_payload(status_frame), "{\"on\":true}")); + moq_frame_frame_free(status_frame); + moq_track_subscriber_free(status_subscriber); + moq_json *app = NULL; + CHECK(moq_json_parse(str("{\"v\": [1, true]}"), &app, &json_err)); + CHECK(moq_catalog_producer_extra_mutate(catalog, sets_app, app, &mux_err)); + moq_json_free(app); + struct readback back = {0}; + CHECK(moq_catalog_producer_extra_mutate(catalog, reads_back, &back, &mux_err)); + CHECK(contains(back.json, "\"status\"")); + moq_buf_free(back.json); + CHECK(is(back.app, "{\"v\":[1,true]}")); + /* Malformed text fails to parse, and a reserved section is moq-mux's error. */ + CHECK(!moq_json_parse(str("{\"v\":"), &app, &json_err)); + CHECK(is(json_err, "EOF while parsing a value at line 1 column 5")); + moq_catalog_hang_extra *sections = moq_catalog_hang_extra_default(); + CHECK(moq_json_parse(str("1"), &app, &json_err)); + CHECK(!moq_catalog_hang_extra_set(sections, str("json"), app, &mux_err)); + CHECK(mux_err != NULL && is(moq_mux_error_to_string(mux_err), "reserved catalog section: json")); + moq_mux_error_free(mux_err); + mux_err = NULL; + moq_json_free(app); + moq_catalog_hang_extra_free(sections); + moq_json_snapshot_json_free(status_snapshot); moq_catalog_producer_extra_free(catalog); moq_broadcast_producer_free(publisher); diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index 8efd3636d..5d86e5780 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -175,6 +175,17 @@ int main() { auto truncated = moq::codec_opus_audio_config(moq::view(head, 8)); CHECK(!truncated && !truncated.error().to_string().empty()); + // A subscription's ranges, from any iterable of them: a vector. + { + moq::track_range live = moq::track_range::live(); + moq::track_subscription ranged = moq::track_subscription::default_().with_ranges( + {live, moq::track_range::groups({{MOQ_BOUND_INCLUDED, 2}, {MOQ_BOUND_EXCLUDED, 6}})}); + std::vector got = ranged.ranges(); + CHECK(got.size() == 2 && got[0] == live); + CHECK(got.size() == 2 && std::get(got[1].start)._0 == 2); + CHECK(got.size() == 2 && got[1].end && std::get(*got[1].end)._0 == 6); + } + // A catalog at moq-mux's untyped extension, from the config C++ builds, whose generic // `is_claimed` is named by the last segment of its argument, and whose `mutate` lends // the catalog to C++. @@ -194,6 +205,45 @@ int main() { } })); CHECK(read_catalog == 1); + + // A JSON track the catalog lists, published through moq-mux's generic + // `json_snapshot` at a JSON document, and a section C++ parses from text, set in the + // lent catalog and read back as text. + auto status = publisher.create_track("status", std::nullopt); + CHECK(status); + auto status_snapshot = catalog->json_snapshot_config(std::move(*status), moq::json_config::default_(), + moq::json_options::default_()); + CHECK(status_snapshot && is(status_snapshot->name(), "status")); + CHECK(catalog->is_claimed_json_config("status")); + // A value C++ parses is published as the track's latest, which a subscriber reads + // back as JSON text. + CHECK(status_snapshot->update(moq::json::parse(R"({"on": true})").value())); + moq::track_subscriber status_subscriber = status_snapshot->consume(); + auto status_group = status_subscriber.recv_group().get(); + CHECK(status_group && status_group.value()); + if (status_group && status_group.value()) { + auto status_frame = status_group.value()->read_frame().get(); + CHECK(status_frame && status_frame.value() && is(status_frame.value()->payload(), R"({"on":true})")); + } + auto app = moq::json::parse(R"({"v": [1, true]})"); + CHECK(app); + CHECK(catalog->mutate([&](moq::hang_catalog_extra &lent) { + moq::catalog_hang_extra sections = lent.ext(); + CHECK(sections.set("app", std::move(*app))); + lent.set_ext(std::move(sections)); + })); + CHECK(catalog->mutate([&](moq::hang_catalog_extra &lent) { + auto json = lent.to_json(); + CHECK(json && json.value().str().find("\"status\"") != std::string_view::npos); + auto section = lent.ext().get("app"); + CHECK(section && is(section->to_text(), R"({"v":[1,true]})")); + })); + // Malformed text fails to parse, and a reserved section is moq-mux's error. + auto broken = moq::json::parse(R"({"v":)"); + CHECK(!broken && is(broken.error(), "EOF while parsing a value at line 1 column 5")); + moq::catalog_hang_extra sections = moq::catalog_hang_extra::default_(); + auto reserved = sections.set("json", moq::json::parse("1").value()); + CHECK(!reserved && is(reserved.error().to_string(), "reserved catalog section: json")); } // A session's config, filled in as Rust assigns it, and a nested field by getting a copy, diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 18021e082..e9895f750 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -183,6 +183,13 @@ fun main() { expect(e.message.isNotEmpty(), "a truncated head's error has a message") } + // A subscription's ranges, from any iterable of them: a list. + val groups = TrackRange.groups(BoundsU64(BoundU64(Bound.INCLUDED, 2u), BoundU64(Bound.EXCLUDED, 6u))) + val ranged = TrackSubscription.default().withRanges(listOf(TrackRange.live(), groups)).ranges + expect(ranged == listOf(TrackRange.live(), groups), "a subscription keeps its ranges") + expect(groups.start == TrackAnchor.Absolute(2u), "groups start at their first") + expect(groups.end == TrackAnchor.Absolute(6u), "groups end before their end") + // A catalog at moq-mux's untyped extension, from the config Kotlin builds, whose generic // `is_claimed` is named by the last segment of its argument, and whose `mutate` lends // the catalog to Kotlin. @@ -196,6 +203,44 @@ fun main() { } expect(read == 1, "mutate lends the catalog once") + // A JSON track the catalog lists, published through moq-mux's generic `json_snapshot` at + // a JSON document, and a section Kotlin parses from text, set in the lent catalog and + // read back as text. + val status = catalog.jsonSnapshotConfig(publisher.createTrack("status", null), JsonConfig.default(), JsonOptions.default()) + expect(status.name() == "status", "a JSON snapshot has its track's name") + expect(catalog.isClaimedJsonConfig("status"), "the JSON snapshot claims its name") + // A value Kotlin parses is published as the track's latest, which a subscriber reads + // back as JSON text. + Json.parse("""{"on": true}""").use { status.update(it) } + runBlocking { + val frame = status.consume().recvGroup()?.readFrame() + expect(frame?.payload?.decodeToString() == """{"on":true}""", "a subscriber reads the latest value") + } + catalog.mutate { lent -> + val sections = lent.ext + sections.set("app", Json.parse("""{"v": [1, true]}""")) + lent.setExt(sections) + } + var app: String? = null + catalog.mutate { lent -> + expect(lent.toJson().contains("\"status\""), "the catalog lists the JSON track") + app = lent.ext.get("app")?.use { it.toText() } + } + expect(app == """{"v":[1,true]}""", "the catalog reads its section back as text") + // Malformed text fails to parse, and a reserved section is moq-mux's error. + try { + Json.parse("""{"v":""").close() + expect(false, "malformed JSON fails to parse") + } catch (e: StringException) { + expect(e.error == "EOF while parsing a value at line 1 column 5", "a parse error is serde_json's") + } + try { + CatalogHangExtra.default().use { it.set("json", Json.parse("1")) } + expect(false, "a reserved section is an error") + } catch (e: MuxErrorException) { + expect(e.message == "reserved catalog section: json", "a reserved section is moq-mux's error") + } + // moq-tokio's terminal connect errors, which moq-net's `Error` shares no names with. expect(ConnectError.UNAUTHORIZED.isAuth(), "unauthorized is an auth error") expect(ConnectError.UNAUTHORIZED.toString() == "unauthorized", "a connect error prints as Rust's") diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index dcb8efb86..bcddb168b 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -149,6 +149,12 @@ impl<'a> Abi<'a> { rest = &path[len..]; break; } + // No config names a foreign type, which is named by its role instead. + if key == "serde_json::Value" { + segments.push("json".to_string()); + rest = &path[len..]; + break; + } } segments.extend(rest.iter().map(|segment| snake(segment))); segments diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index ffba3f862..24d75bd5e 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -39,6 +39,11 @@ pub struct Why { pub note: Option<(Span, String)>, } +/// Types of crates that are no export, by crate and name, each exported as an object made +/// by parsing its text, where some crate builds with it: `url::Url`, and +/// `serde_json::Value`, which a `Serialize` or `Deserialize` parameter takes. +const FOREIGN: [(&str, &str); 2] = [("url", "Url"), ("serde_json", "Value")]; + /// The construct of an instance inference missed: a bug in the exporter, not the crate. pub const NOT_INFERRED: &str = "instance not inferred"; @@ -162,8 +167,9 @@ struct Collector<'tcx> { /// implementation or a lent closure, with the type each takes. impls: std::cell::RefCell, Type)>>, bytes: Option, - /// `url::Url`, when no export is the `url` crate, and the object it exports as. - url: Option<(DefId, usize)>, + /// Each type in [`FOREIGN`] that some crate builds with and no export is, and the + /// object it exports as. + foreign: Vec<(DefId, usize)>, /// `std::ops::Bound`, and `std::ops::RangeBounds`, which names it. bound: Option, range_bounds: Option, @@ -182,7 +188,10 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>, crates: &[Crate<'_>]) -> Collected { .and_then(|child| child.res.opt_def_id()) }; let bytes = root("bytes", "Bytes"); - let url = root("url", "Url").filter(|url| !crates.iter().any(|c| c.krate == url.krate)); + let foreign: Vec = (FOREIGN.iter()) + .filter_map(|&(krate, name)| root(krate, name)) + .filter(|ty| !crates.iter().any(|c| c.krate == ty.krate)) + .collect(); let range_bounds = tcx.get_diagnostic_item(rustc_span::Symbol::intern("RangeBounds")); // `RangeBounds::start_bound` returns a `Bound`, which has no diagnostic item of its own. let bound = range_bounds.and_then(|tr| { @@ -221,7 +230,7 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>, crates: &[Crate<'_>]) -> Collected { left_out: Default::default(), impls: Default::default(), bytes, - url: url.map(|url| (url, 0)), + foreign: foreign.iter().map(|&ty| (ty, 0)).collect(), bound, range_bounds, orphans: orphans.clone(), @@ -510,14 +519,18 @@ impl<'tcx> Collector<'tcx> { } /// The first type argument an instance is named by that cannot map as [`Self::arg`] - /// requires, which is `()`, a primitive, `String`, or an export's type. + /// requires, which is `()`, a primitive, `String`, a foreign object, or an export's type. fn unarguable(&self, site: &Site<'tcx>) -> Option> { let tcx = self.tcx; site.args.types().take(site.explicit).find(|&ty| match ty.kind() { ty::Tuple(list) => !list.is_empty(), ty::Bool | ty::Char | ty::Float(_) => false, ty::Int(_) | ty::Uint(_) => int(ty).is_none(), - ty::Adt(adt, _) => tcx.lang_items().string() != Some(adt.did()) && !self.exported(adt.did().krate), + ty::Adt(adt, _) => { + tcx.lang_items().string() != Some(adt.did()) + && !self.exported(adt.did().krate) + && !self.foreign.iter().any(|&(ty, _)| ty == adt.did()) + } _ => true, }) } @@ -876,45 +889,89 @@ impl<'tcx> Collector<'tcx> { } } } - // After every declared object, so `prune` can drop it if no export names it. Until - // here, it maps only to decide what does, never to an index kept. - if let Some((url, index)) = &mut self.url { - *index = self.api.objects.len(); - let span = tcx.def_span(*url); - let ty = Type::Object(*index); - let path = |rest: &[&str]| -> Path { ["url", "Url"].iter().chain(rest).map(|s| s.to_string()).collect() }; - self.api.objects.push(Object { - path: path(&[]), - args: Vec::new(), - span, - docs: "`url::Url`: a URL, made by parsing its text.".to_string(), - sync: true, - copy: false, - lifetimes: 0, - methods: vec![Function { - path: path(&["parse"]), - span, - docs: "Parse an absolute URL, failing with why it is not one.".to_string(), - receiver: None, - params: vec![Param { - name: "input".to_string(), - ty: Type::Ref(Mutability::Not, Box::new(Type::Str)), - }], - ok: ty, - err: Some(Type::String), - future: false, - eager: false, - access: Access::Parse, - instance: None, - }], - traits: Traits { + // After every declared object, so `prune` can drop each that no export names. Until + // here, each maps only to decide what does, never to an index kept. + for n in 0..self.foreign.len() { + let index = self.api.objects.len(); + self.foreign[n].1 = index; + let object = self.foreign_object(self.foreign[n].0, index); + self.api.objects.push(object); + } + } + + /// The object a type in [`FOREIGN`] exports as, at `index`: made by parsing its text. + fn foreign_object(&self, def_id: DefId, index: usize) -> Object { + let tcx = self.tcx; + let span = tcx.def_span(def_id); + let ty = Type::Object(index); + let krate = tcx.crate_name(def_id.krate); + let path = |rest: &str| -> Path { + [krate.as_str(), tcx.item_name(def_id).as_str(), rest] + .iter() + .filter(|s| !s.is_empty()) + .map(|s| s.to_string()) + .collect() + }; + let parse = |docs: &str| Function { + path: path("parse"), + span, + docs: docs.to_string(), + receiver: None, + params: vec![Param { + name: "input".to_string(), + ty: Type::Ref(Mutability::Not, Box::new(Type::Str)), + }], + ok: ty.clone(), + err: Some(Type::String), + future: false, + eager: false, + access: Access::Parse, + instance: None, + }; + let (docs, methods, traits) = match krate.as_str() { + "url" => ( + "`url::Url`: a URL, made by parsing its text.", + vec![parse("Parse an absolute URL, failing with why it is not one.")], + Traits { clone: true, eq: true, display: true, debug: true, default: false, }, - }); + ), + // Its contents are the caller's JSON library's to read, from its text. + _ => ( + "`serde_json::Value`: a JSON document, made by parsing its text and read back as text.", + vec![ + parse("Parse a JSON document, failing with why it is not one."), + Function { + path: path("to_text"), + span, + docs: "The document as JSON text.".to_string(), + receiver: Some(Receiver::Ref), + params: Vec::new(), + ok: Type::String, + err: None, + future: false, + eager: false, + access: Access::Text, + instance: None, + }, + ], + Traits::default(), + ), + }; + Object { + path: path(""), + args: Vec::new(), + span, + docs: docs.to_string(), + sync: true, + copy: false, + lifetimes: 0, + methods, + traits, } } @@ -1245,29 +1302,69 @@ impl<'tcx> Collector<'tcx> { } } - /// Drop `url::Url`'s object if no export names it, so a crate that only builds with - /// `url` exports none of it. + /// Drop each foreign type's object that no export names, so a crate that only builds + /// with `url` exports none of it. They are the last objects, so dropping one renumbers + /// only those after it. fn prune(&mut self) { - let Some((_, url)) = self.url else { - return; - }; - let api = &self.api; - let signature = |f: &Function| { - (f.params.iter().map(|p| &p.ty)) - .chain([&f.ok]) - .chain(&f.err) - .any(|ty| names(ty, url)) - }; - let fields = |fields: &[Field]| fields.iter().any(|f| names(&f.ty, url)); - let used = api.functions.iter().any(signature) - || (api.records.iter()).any(|r| fields(&r.fields) || r.methods.iter().any(signature)) - || (api.enums.iter()) - .any(|e| e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature)) - || (api.objects.iter().enumerate()).any(|(i, o)| i != url && o.methods.iter().any(signature)) - || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); - if !used { - assert_eq!(url, api.objects.len() - 1, "url::Url is the last object"); - self.api.objects.pop(); + for n in (0..self.foreign.len()).rev() { + let gone = self.foreign[n].1; + let api = &self.api; + let signature = |f: &Function| { + (f.params.iter().map(|p| &p.ty)) + .chain([&f.ok]) + .chain(&f.err) + .chain(f.instance.iter().flat_map(|i| i.args.iter().flatten().chain(&i.named))) + .any(|ty| names(ty, gone)) + }; + let fields = |fields: &[Field]| fields.iter().any(|f| names(&f.ty, gone)); + let args = |args: &[Type]| args.iter().any(|ty| names(ty, gone)); + let used = api.functions.iter().any(signature) + || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) + || (api.enums.iter()).any(|e| { + args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) + }) + || (api.objects.iter().enumerate()) + .any(|(i, o)| i != gone && (args(&o.args) || o.methods.iter().any(signature))) + || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); + if used { + continue; + } + self.api.objects.remove(gone); + self.foreign.remove(n); + for later in &mut self.foreign[n..] { + later.1 -= 1; + } + let api = &mut self.api; + let functions = (api.functions.iter_mut()) + .chain(api.records.iter_mut().flat_map(|r| &mut r.methods)) + .chain(api.enums.iter_mut().flat_map(|e| &mut e.methods)) + .chain(api.objects.iter_mut().flat_map(|o| &mut o.methods)) + .chain(api.callbacks.iter_mut().flat_map(|c| &mut c.methods)); + for f in functions { + let instance = (f.instance.iter_mut()).flat_map(|i| i.args.iter_mut().flatten().chain(&mut i.named)); + (f.params.iter_mut().map(|p| &mut p.ty)) + .chain([&mut f.ok]) + .chain(&mut f.err) + .chain(instance) + .for_each(|ty| renumber(ty, gone)); + } + for r in &mut api.records { + (r.args.iter_mut()) + .chain(r.fields.iter_mut().map(|f| &mut f.ty)) + .for_each(|ty| renumber(ty, gone)); + } + for e in &mut api.enums { + (e.args.iter_mut()) + .chain( + e.variants + .iter_mut() + .flat_map(|v| v.fields.iter_mut().map(|f| &mut f.ty)), + ) + .for_each(|ty| renumber(ty, gone)); + } + for o in &mut api.objects { + o.args.iter_mut().for_each(|ty| renumber(ty, gone)); + } } } @@ -1774,7 +1871,8 @@ impl<'tcx> Collector<'tcx> { // The first refusal ends it, before any later parameter's candidates are mapped. GenericArgKind::Const(_) => return Err(vec![why("const generic", "")]), GenericArgKind::Type(param) if self.lent(def_id, param) => kept.push(param), - GenericArgKind::Type(param) => match self.choose(def_id, param, &base) { + // Its bounds see each earlier parameter chosen, as `impl Into>` names `T`. + GenericArgKind::Type(param) => match self.choose(def_id, param, &args) { // Typing takes a later type only where an earlier one's export is refused. Ok(ranked) if self.inferring => { let last = ranked.iter().position(|&ty| !self.names_export(ty)); @@ -1853,8 +1951,9 @@ impl<'tcx> Collector<'tcx> { /// The types a type parameter may be exported as, best first, which typing takes the /// first of: `X` for each `Into` or `AsRef` its bounds imply, or `&X` and then - /// `String` or `Vec` where `X` is `str` or `[T]`, then `String`, `Bytes`, and - /// `Vec`, each that meets its bounds and maps. + /// `String` or `Vec` where `X` is `str` or `[T]`, `Vec` for + /// `impl IntoIterator`, `serde_json::Value` for a serde bound, then `String`, + /// `Bytes`, and `Vec`, each that meets its bounds and maps. fn choose(&self, def_id: DefId, param: Ty<'tcx>, base: &[ty::GenericArg<'tcx>]) -> Result>, Why> { let tcx = self.tcx; let clauses = tcx.clauses_of(def_id); @@ -1899,8 +1998,11 @@ impl<'tcx> Collector<'tcx> { // An `Into` target that does not map takes what converts into it instead. let into = (tcx.is_diagnostic_item(sym::Into, tr) && !self.maps(target(), Pos::Param)) .then(|| self.sources(def_id, target())); - // A source that must outlive the call, or converts into a parameter that must, owns it. - let sources = into.into_iter().flatten().map(|(x, source)| (x, outlives || source)); + // A source that must outlive the call, or converts into a parameter that must, owns + // it, so one that borrows, as `&'a u32` into `impl Into> + 'static`, is not taken. + let sources = (into.into_iter().flatten()) + .filter(|(x, _)| !outlives || !x.walk().any(|arg| arg.as_region().is_some())) + .map(|(x, source)| (x, outlives || source)); for (target, outlives) in std::iter::once((target(), outlives)).chain(sources) { let owned = match target.kind() { ty::Str => string, @@ -1919,6 +2021,10 @@ impl<'tcx> Collector<'tcx> { } } else if Some(tr) == self.range_bounds { candidates.extend(self.bounds(target())); + } else if tcx.is_diagnostic_item(sym::IntoIterator, tr) { + candidates.extend(self.items(&identity.clauses, param).and_then(vec)); + } else if serde(tcx, tr) { + candidates.extend(self.json()); } } candidates.extend(string); @@ -1950,10 +2056,36 @@ impl<'tcx> Collector<'tcx> { } } - /// What `impl Into` takes through the `From` implementations into `target` - /// whose source is a type parameter: each `Into` or `AsRef` its bounds imply, and + /// `serde_json::Value`, if it exports as a foreign object. + fn json(&self) -> Option> { + let tcx = self.tcx; + (self.foreign.iter()) + .find(|&&(ty, _)| tcx.crate_name(ty.krate).as_str() == "serde_json") + .map(|&(ty, _)| Ty::new_adt(tcx, tcx.adt_def(ty), ty::List::empty())) + } + + /// The `Item` that `clauses` bind `param`'s `IntoIterator` to, if it names no type + /// parameter, which a `Vec` of it then iterates as. + fn items(&self, clauses: &[ty::Unnormalized<'tcx, ty::Clause<'tcx>>], param: Ty<'tcx>) -> Option> { + let tcx = self.tcx; + (clauses.iter()) + .filter_map(|clause| clause.skip_norm_wip().as_projection_clause()) + .map(|pred| pred.skip_binder()) + .find(|pred| { + let term = pred.projection_term; + let item = term.expect_projection_def_id(); + term.self_ty() == param + && tcx.is_diagnostic_item(sym::IntoIterator, tcx.parent(item)) + && tcx.item_name(item) == sym::Item + }) + .and_then(|pred| pred.term.as_type()) + .filter(|item| !item.has_non_region_param()) + } + + /// What `impl Into` takes through the `From` implementations into `target`: a + /// source that is a type parameter by each `Into` or `AsRef` its bounds imply, and /// whether they make it outlive the call, so `impl> From for - /// Timed` makes `impl Into>` bytes. + /// Timed` makes `impl Into>` bytes; any other source as it is. fn sources(&self, def_id: DefId, target: Ty<'tcx>) -> Vec<(Ty<'tcx>, bool)> { let tcx = self.tcx; let Some(from) = tcx.get_diagnostic_item(sym::From) else { @@ -1962,6 +2094,8 @@ impl<'tcx> Collector<'tcx> { let Some(simple) = simplify_type(tcx, target, TreatParams::AsRigid) else { return Vec::new(); }; + let sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip(); + let (statics, _) = self.borrows(def_id, sig.skip_binder()); let mut found = Vec::new(); for &imp in tcx .trait_impls_of(from) @@ -1977,13 +2111,33 @@ impl<'tcx> Collector<'tcx> { let trait_ref = tcx.impl_trait_ref(imp).instantiate(tcx, args).skip_norm_wip(); let source = trait_ref.args.type_at(1); let ocx = ObligationCtxt::new(&infcx); - if !source.is_ty_var() - || ocx - .eq(&ObligationCause::dummy(), env, trait_ref.self_ty(), target) - .is_err() + if ocx + .eq(&ObligationCause::dummy(), env, trait_ref.self_ty(), target) + .is_err() { continue; } + // Any other source is taken as it is: `impl<'a, V> From<&'a V> for Timed<&'a V>` + // makes `impl Into>` take `&T`. One that may have to outlive the call, + // by naming `'static`, by a lifetime bound, or by a lifetime the target ties to one + // the export makes `'static`, as `impl Into>` does, takes nothing C lends. + if !source.is_ty_var() { + let regions = |ty: Ty<'tcx>| ty.walk().filter_map(|arg| arg.as_region()).collect::>(); + let lasting = regions(source).iter().any(|r| r.is_static()) + || (!regions(source).is_empty() && regions(target).iter().any(|r| statics.contains(r))) + || (tcx.clauses_of(imp).instantiate(tcx, args).clauses.iter()).any(|clause| { + let clause = clause.skip_norm_wip(); + clause.as_type_outlives_clause().is_some() || clause.as_region_outlives_clause().is_some() + }); + if lasting { + continue; + } + let x = tcx.erase_and_anonymize_regions(infcx.deeply_resolve_ignoring_regions(source)); + if !x.has_non_region_infer() && !found.contains(&(x, false)) { + found.push((x, false)); + } + continue; + } let source = infcx.deeply_resolve_ignoring_regions(source); let clauses = tcx.clauses_of(imp).instantiate(tcx, args).clauses; let implied: Vec<_> = elaborate(tcx, clauses.iter().map(|clause| clause.skip_norm_wip())).collect(); @@ -2434,7 +2588,7 @@ impl<'tcx> Collector<'tcx> { ty.walk().filter_map(|arg| arg.as_type()).any(|ty| match ty.kind() { ty::Adt(adt, args) => { let object = matches!(self.decl(adt.did(), args), Some(Decl::Object(_))) - || self.url.is_some_and(|(url, _)| url == adt.did()); + || self.foreign.iter().any(|&(ty, _)| ty == adt.did()); object && !implements(tcx, ty, adt.did(), sym::Copy) } _ => false, @@ -2646,12 +2800,10 @@ impl<'tcx> Collector<'tcx> { .flatten() { Type::SocketAddr(family) - } else if let Some((url, i)) = self.url - && did == url - { + } else if let Some(&(_, i)) = self.foreign.iter().find(|&&(ty, _)| ty == did) { // A value holds only values, and handles it can copy. match pos { - Pos::Field => return Err(why("object in a value", "url::Url")), + Pos::Field => return Err(why("object in a value", tcx.def_path_str(did))), _ => Type::Object(i), } } else if tcx.is_diagnostic_item(rustc_span::Symbol::intern("Waker"), did) { @@ -2831,6 +2983,27 @@ fn returns(api: &Api, ty: &Type, objects: &mut Vec) { } } +/// Whether `tr` is serde's `Serialize`, `Deserialize`, or `DeserializeOwned`, which +/// `serde_json::Value` meets as a document of any shape. +fn serde(tcx: TyCtxt<'_>, tr: DefId) -> bool { + matches!(tcx.crate_name(tr.krate).as_str(), "serde" | "serde_core") + && matches!( + tcx.item_name(tr).as_str(), + "Serialize" | "Deserialize" | "DeserializeOwned" + ) +} + +/// Renumber each object after `gone`, which is dropped, in `ty`. +fn renumber(ty: &mut Type, gone: usize) { + match ty { + Type::Object(o) if *o > gone => *o -= 1, + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(inner) => renumber(inner, gone), + Type::Tuple(elements) => elements.iter_mut().for_each(|ty| renumber(ty, gone)), + Type::Closure(params, ret) => params.iter_mut().chain([&mut **ret]).for_each(|ty| renumber(ty, gone)), + _ => {} + } +} + /// Whether `ty` names the object `i`. fn names(ty: &Type, i: usize) -> bool { match ty { diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index e349b7bc3..603e6323e 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -55,17 +55,21 @@ impl Crate<'_> { ) }) .collect(); - // `url::Url`, exported as an object when no export is `url`, resolves to the version - // the exports build with, as `bytes` does: one `url` 2 per resolution. - let foreign = self.abi.api.objects.iter().any(|o| o.path == ["url", "Url"]) - && !self.exports.iter().any(|export| export.root() == "url"); - let dependencies = match foreign { - true => format!( - "{dependencies}{} = {{ package = \"url\", version = \"2\" }}\n", - dependency("url") - ), - false => dependencies, - }; + // A foreign type, exported as an object when no export is its crate, resolves to the + // version the exports build with, as `bytes` does: one per major version per + // resolution. + let foreign: String = [("url", "2"), ("serde_json", "1")] + .into_iter() + .filter(|(krate, _)| self.abi.api.objects.iter().any(|o| o.path[0] == *krate)) + .filter(|(krate, _)| !self.exports.iter().any(|export| export.root() == *krate)) + .map(|(krate, version)| { + format!( + "{} = {{ package = \"{krate}\", version = \"{version}\" }}\n", + dependency(krate) + ) + }) + .collect(); + let dependencies = format!("{dependencies}{foreign}"); let manifest = format!( "# Generated by rust2-ffi: the C ABI of the pub API of {packages}.\n\ [package]\n\ @@ -1040,6 +1044,7 @@ impl Gen<'_> { self.rust(&owner.as_ref().expect("a parse has its type").1), args[0] ), + Access::Text => format!("::std::string::ToString::to_string({})", args[0]), }; let body = stmts.join("\n\t"); if let Some((_, poll, free)) = &export.future { diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index f06bbb401..2fb2170a7 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -188,6 +188,8 @@ pub enum Access { /// It parses its receiver's type from text, with `FromStr`, failing with the parse /// error's message: a foreign type exported as an object. Parse, + /// It writes its receiver as text, with `Display`: a foreign type exported as an object. + Text, } impl Function { diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 3bfb1e8df..7f4d9ae3f 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -641,6 +641,33 @@ static int script(void) { api_vec_u32 sums = api_sums((api_vec_vec_u32){.ptr = rows, .len = 3}); printf("sums [%u, %u, %u]\n", sums.ptr[0], sums.ptr[1], sums.ptr[2]); api_vec_u32_free(sums); + api_point summands[] = {{.x = 1, .y = 2}, {.x = -4, .y = 6}}; + api_point summed = api_summed((api_vec_point){.ptr = summands, .len = 2}); + api_point none_summed = api_summed((api_vec_point){.ptr = summands, .len = 0}); + printf("summed %d %d %d %d\n", summed.x, summed.y, none_summed.x, none_summed.y); + api_buf longest_words[] = {str("a"), str("abc")}; + size_t most = api_longest((api_vec_string){.ptr = longest_words, .len = 2}); + size_t no_longest = api_longest((api_vec_string){.ptr = longest_words, .len = 0}); + printf("longest %zu %zu\n", most, no_longest); + + /* JSON crosses as a document, made from text and read back as text. */ + api_json *doc; + if (!api_json_parse(str("{\"b\": [1, 2.5, null], \"a\": \"\\u00e9\"}"), &doc, &parse_err)) { + return 1; + } + print("json ", api_encode(doc), "\n"); + print("timed text ", api_timed_text(doc), "\n"); + api_json *tagged = api_tagged(doc, str("t")); + api_json_free(doc); + print("tagged ", api_json_to_text(tagged), "\n"); + api_json_free(tagged); + api_json *broken; + if (api_json_parse(str("{\"a\":"), &broken, &parse_err)) { + print("json parse ", api_json_to_text(broken), "\n"); + api_json_free(broken); + } else { + print("json parse err ", parse_err, "\n"); + } api_url *home; if (!api_url_parse(str("https://example.com/a/b"), &home, &parse_err)) { diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 5069ceb64..6491ae6a6 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -507,6 +507,21 @@ int script() { auto sums = api::sums({{1, 2}, {}, {3}}); std::printf("sums [%u, %u, %u]\n", sums[0], sums[1], sums[2]); + auto summed = api::summed({{1, 2}, {-4, 6}}); + auto none_summed = api::summed({}); + std::printf("summed %d %d %d %d\n", summed.x, summed.y, none_summed.x, none_summed.y); + std::printf("longest %zu %zu\n", api::longest({"a", "abc"}), api::longest({})); + + api::json doc = api::json::parse(R"({"b": [1, 2.5, null], "a": "\u00e9"})").value(); + print("json ", api::encode(doc), "\n"); + print("timed text ", api::timed_text(doc), "\n"); + api::json tagged = api::tagged(std::move(doc), "t"); + print("tagged ", tagged.to_text(), "\n"); + if (auto parsed = api::json::parse("{\"a\":")) { + print("json parse ", parsed->to_text(), "\n"); + } else { + print("json parse err ", parsed.error(), "\n"); + } api::site site = api::site::new_(base); print("site ", site.home().to_string(), " "); diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 02aed8e6b..c0ff663d9 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -307,6 +307,19 @@ fun script() { check(runCatching { counter.get() }.exceptionOrNull() is IllegalStateException, "a counter moved in is closed") } println("sums ${sums(listOf(listOf(1u, 2u), emptyList(), listOf(3u)))}") + val summed = summed(listOf(Point(1, 2), Point(-4, 6))) + val noneSummed = summed(emptyList()) + println("summed ${summed.x} ${summed.y} ${noneSummed.x} ${noneSummed.y}") + println("longest ${longest(listOf("a", "abc"))} ${longest(emptyList())}") + val doc = Json.parse("""{"b": [1, 2.5, null], "a": "\u00e9"}""") + println("json ${encode(doc)}") + println("timed text ${timedText(doc)}") + tagged(doc, "t").use { tagged -> println("tagged ${tagged.toText()}") } + try { + Json.parse("{\"a\":").close() + } catch (e: StringException) { + println("json parse err ${e.error}") + } val site = Site.new(base) site.home.use { a -> site.home.use { b -> println("site $a $b") } } diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index fd4492405..e62107927 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -47,7 +47,6 @@ Every generator on the C ABI: ## Required -- [Iterator and JSON arguments the exporter can't map yet](/quest/m1/ffi-export/instance-keys.md) - `IntoIterator` as a `Vec`, and `serde_json::Value` - [Returned iterators cross as arrays](/quest/m1/ffi-export/iterators.md) - `Hops::iter`, `Versions::iter`, and `Patterns::iter` export, returning arrays as `&[T]` does - [moq-clock's C names once it moves into kio](/quest/m1/ffi-export/crates.md) - `moq_now` and `moq_origin` become the clock's, after moq moves it into `kio::clock` - [Sessions on moq-tokio](/quest/m1/ffi-export/session-tokio.md) - bindings connect and accept with one call, the library running moq-tokio inside diff --git a/quest/m1/ffi-export/instance-keys.md b/quest/m1/ffi-export/instance-keys.md deleted file mode 100644 index bb95d6623..000000000 --- a/quest/m1/ffi-export/instance-keys.md +++ /dev/null @@ -1,27 +0,0 @@ -# [M] Iterator and JSON arguments the exporter can't map yet - -## Goal - -moq-net's `track::Subscription::with_ranges(impl IntoIterator)` exports, -taking a `Vec` of `Range`, and moq-mux's `catalog::Producer::json_snapshot` and -`json_stream` export at `serde_json::Value` as their `T`, crossing as something every -generator can read. Both come from fixed rules, with no config: moq's crates export as -they are. - -## Plan - -Decided with the maintainer on 2026-10-27, superseding this quest's plan to extend the -config's `instances` key: translators take stock crates as they are, so the exporter -infers the instances it exports at, and the `instances` key is gone -([#152](https://github.com/kixelated/rust2/pull/152)). That PR also exports this quest's -other two items: moq-net's `with_publisher` at `&origin::Producer`, and `DEFAULT_NAME` on -hang's `Catalog<()>`. What remains is two type mappings. - -- `IntoIterator` as a `Vec` needs a `Vec` of records to map first. -- `serde_json::Value` crosses the C ABI as something every generator can read; decide - its shape (JSON text is the obvious one) when the quest starts. The `T: Serialize` - parameter now takes `String` by the choice rules, which serializes one JSON string - rather than a document; moq's config excludes both methods until then. -- Report moq-ffi's coverage and the census before and after. - -Public API: the C names and types these methods export with.