From c027ef5b8deab066087237978378bc957023415f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:46:29 -0700 Subject: [PATCH 1/9] chore(quest): claim quest/m1/ffi-export/foreign-types.md Quest-Claim: f7dcad73d3172280 From 19caaf0172bbdbeea9f74769bdd50605ccbae728 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 14:49:44 -0700 Subject: [PATCH 2/9] feat(ffi): export Url, SocketAddr, paths, and Vec across the C ABI `url::Url` exports as an object parsed from text, failing with the parse error's message; `SocketAddr` as `_socket_addr`; `&Path` and `PathBuf` as a buffer of the OS's bytes; and `Vec` of any other mapped `T` as `_vec_`, an array C frees with what it holds. C++ gets `std::vector`, lowered through an arena that lives for the call. moq's config drops the excludes these unblock and renames moq-mux's `catalog::Consumer`, which now clashes with hang's. Co-Authored-By: Claude Opus 5.5 --- ffi/Cargo.lock | 280 ++++++++++++++++++++++++ ffi/MIGRATE.md | 6 + ffi/README.md | 12 +- ffi/fixtures/api/Cargo.toml | 1 + ffi/fixtures/api/examples/transcript.rs | 47 ++++ ffi/fixtures/api/src/lib.rs | 76 +++++++ ffi/fixtures/reject/expected.txt | 3 +- ffi/fixtures/reject/src/lib.rs | 5 + ffi/moq/ffi.toml | 119 ++-------- ffi/moq/moq.c | 39 ++++ ffi/runtime/rt.hpp | 37 ++++ ffi/runtime/rt.rs | 138 ++++++++++++ ffi/src/abi.rs | 62 +++++- ffi/src/c.rs | 19 ++ ffi/src/collect.rs | 137 ++++++++++-- ffi/src/cpp.rs | 157 +++++++++++-- ffi/src/glue.rs | 69 +++++- ffi/src/ir.rs | 11 + ffi/tests/api.c | 153 +++++++++++++ ffi/tests/api.cpp | 108 +++++++++ ffi/tests/fixtures.rs | 2 + quest/m1/ffi-export/README.md | 1 - quest/m1/ffi-export/foreign-types.md | 23 -- quest/m1/ffi-export/session-tokio.md | 4 +- 24 files changed, 1329 insertions(+), 180 deletions(-) delete mode 100644 quest/m1/ffi-export/foreign-types.md diff --git a/ffi/Cargo.lock b/ffi/Cargo.lock index db502630c..4d2d35650 100644 --- a/ffi/Cargo.lock +++ b/ffi/Cargo.lock @@ -8,6 +8,7 @@ version = "0.0.0" dependencies = [ "bytes", "dep-fixture", + "url", ] [[package]] @@ -20,18 +21,142 @@ checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" name = "dep-fixture" version = "0.0.0" +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "equivalent" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + [[package]] name = "hashbrown" version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +[[package]] +name = "icu_collections" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe090a42a0d3140fed8c63263ed1fecc02422cb2f4bfe55cd8de2de1b8e105dd" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "404a3b8c486f205c87fe7f23ab6894d84a4ad76d6e4671ae1981ba7def87c599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20f0088bfb7441f7a37fa0c9e20cb1e9cba1cac6b9ce8cd2a6dc44294f8e8cbc" + +[[package]] +name = "icu_properties" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ea83d991c56158f7cf898bf86436c1dfe7c07ee82fe88951f2695c9f37a379f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98f29af82d02b163fe50161388350e743bb3107cc8e8c2f7314cb505faddfcf" + +[[package]] +name = "icu_provider" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + [[package]] name = "indexmap" version = "2.14.2" @@ -48,12 +173,33 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + [[package]] name = "memchr" version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + [[package]] name = "proc-macro2" version = "1.0.107" @@ -149,6 +295,18 @@ dependencies = [ "serde_core", ] +[[package]] +name = "smallvec" +version = "1.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8" + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + [[package]] name = "syn" version = "3.0.9" @@ -160,6 +318,27 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinystr" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "toml" version = "1.1.8+spec-1.1.0" @@ -205,12 +384,113 @@ version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "winnow" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "yoke" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zmij" version = "1.0.23" diff --git a/ffi/MIGRATE.md b/ffi/MIGRATE.md index 752e6f1ff..6b7b20d96 100644 --- a/ffi/MIGRATE.md +++ b/ffi/MIGRATE.md @@ -2,6 +2,12 @@ ## Unreleased +- `Vec`, `PathBuf`, `SocketAddr`, and `url::Url` map, so a struct or enum whose + fields were refused only for them is a value now, not an object: moq-tokio's + `listen::Bind` is the C enum `moq_listen_bind`, freed with `moq_listen_bind_free(&b)`, + not a handle. An item that clashes with the new names `_socket_addr`, + `_url`, or a `_vec_` takes a `rename`. + - In `exclude` and `rename`, a path that a field shares with a method or constant of its type, such as `Backoff::initial`, names only the method, as in rustdoc. Name the field `field@Backoff::initial`: an `exclude` that left out both now needs both entries. diff --git a/ffi/README.md b/ffi/README.md index a937eff45..98f60ccf5 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -70,7 +70,11 @@ export's, and adds nothing; another crate's type is refused. | `poll_x`, when `x` exports as a future | left out: the future is its poll | | `bool`, integers but 128-bit, floats, `char` | `bool`, `` types, `float`, `double`, a `uint32_t` code point | | `&str`, `String`, `&[u8]`, `Vec`, `bytes::Bytes` | `_buf`: a view passed in, an owned buffer returned | +| `&Path`, `PathBuf` | `_buf` of the path's bytes as the OS holds them on Unix, which need not be UTF-8, else UTF-8 | | `Duration` | `_duration` | +| `SocketAddr` | `_socket_addr`: its IP's bytes, port, and whether it is IPv6 | +| `url::Url` | an object `_url`, made by `_url_parse`, which fails with the parse error's message, and printed by `_to_string` | +| `Vec` of any other `T` | `_vec_`: an array of `T`'s C values, viewed passed in and owned returned; `Vec<&T>` 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 | | `impl RangeBounds` of an integer | `_bounds_`: its start and end bounds | @@ -107,6 +111,11 @@ refused, and the census counts why. future, which may be polled on any thread. A borrow Rust would refuse at compile time aborts, naming the function, as does a null or moved-out handle: there is no Rust caller to return an error to. +- **Arrays** of a `Vec` are a pointer, a length, and an owner, as a buffer is: passed + in, Rust copies each element out of C's array; returned, `_vec__free` frees + the array and its first `len` elements, so a binding that takes the elements sets + `len` to 0 first. A null pointer is `None`, so C passes a non-null one for `Some` of + an empty array. - **Values** are copied in from the caller's memory, which Rust never frees, and lowered into memory the caller frees once with the type's `_free`. - **Futures** run no runtime: C polls with a `_waker` and polls again when woken. @@ -172,7 +181,8 @@ exceptions and RTTI on or off: it throws nothing and reads no type information. traits are free functions taking it first. `PartialEq` is `==`, `Display` `to_string()`, `Debug` `debug()`, and `Default` `default_()`. - **Bytes** are passed as a `view`, which any contiguous bytes convert to, and returned as - a `buf`, which owns Rust's buffer without a copy. `Option` is `std::optional`, a + a `buf`, which owns Rust's buffer without a copy. A `Vec` is a `std::vector`, of + `view`s for bytes passed in, and a socket address `socket_addr`, the C struct. `Option` is `std::optional`, a tuple `std::tuple`, and `Result` a `result`, whose `value()` aborts on an error. - **Traits** are abstract classes, passed as a `std::unique_ptr` that Rust deletes. diff --git a/ffi/fixtures/api/Cargo.toml b/ffi/fixtures/api/Cargo.toml index b663c5496..7ce19da21 100644 --- a/ffi/fixtures/api/Cargo.toml +++ b/ffi/fixtures/api/Cargo.toml @@ -13,6 +13,7 @@ name = "api_lib" [dependencies] bytes = "1.11.1" dep-fixture = { path = "../dep" } +url = "2.5.8" [lints] workspace = true diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 6d78e56d4..6a02e57c3 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -296,4 +296,51 @@ fn main() { retry.delay() ); println!("slot {}", api_lib::Slot::::new(200).value()); + + // Foreign types: a URL parsed from text, socket addresses, OS paths, and `Vec`s. + let base = url::Url::parse("https://example.com/a/b").unwrap(); + println!("url {base} host {:?}", api_lib::host(&base)); + match api_lib::resolve(base.clone(), "c?d") { + Ok(url) => println!("resolve {url} {}", url == base), + Err(err) => println!("resolve err {err}"), + } + match url::Url::parse("no scheme") { + Ok(url) => println!("parse {url}"), + Err(err) => println!("parse err {err}"), + } + let v4: std::net::SocketAddr = "127.0.0.1:8080".parse().unwrap(); + let v6: std::net::SocketAddr = "[::1%3]:443".parse().unwrap(); + println!("ports {:?}", api_lib::ports(vec![v4, v6])); + let peer = api_lib::Peer { + addr: v6, + names: vec!["a".to_string(), "b".to_string()], + } + .next("c"); + println!("peer {peer:?} {:?}", peer.aliases()); + let dir = std::path::PathBuf::from(::from_vec( + b"\xffdir".to_vec(), + )); + let file = api_lib::file(&dir, "f"); + println!("file {}", file.as_os_str().as_encoded_bytes().escape_ascii()); + let parents = api_lib::parents(vec![file, std::path::PathBuf::from("/")]); + let parents: Vec = (parents.iter()) + .map(|p| match p { + Some(p) => p.as_os_str().as_encoded_bytes().escape_ascii().to_string(), + None => "none".to_string(), + }) + .collect(); + println!("parents {}", parents.join(" ")); + let words = api_lib::words(" one two three "); + println!("words {} {words:?}", words.len()); + println!( + "sentence {} {}", + api_lib::sentence(Some(words)), + api_lib::sentence(None) + ); + println!("sentence {}|", api_lib::sentence(Some(Vec::new()))); + println!("chunks {:?}", api_lib::chunks(b"abcde", 2)); + let counters = api_lib::counters(vec!["x".to_string(), "y".to_string()]); + let counters: Vec = counters.iter().map(|c| format!("{} {}", c.name(), c.get())).collect(); + println!("counters {}", counters.join(", ")); + println!("sums {:?}", api_lib::sums(vec![vec![1, 2], Vec::new(), vec![3]])); } diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index d5e2d706a..ebd25c927 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -833,3 +833,79 @@ impl Slot { pub fn wide(value: u32) -> Slot { Slot::new(value) } + +/// A peer: a record of a socket address and its names, which holds a `Vec`. +#[derive(Clone, Debug, PartialEq)] +pub struct Peer { + pub addr: std::net::SocketAddr, + pub names: Vec, +} + +impl Peer { + /// Its names, each borrowed. + pub fn aliases(&self) -> Vec<&str> { + self.names.iter().map(String::as_str).collect() + } + + /// The peer at the next port, also named `name`. + pub fn next(&self, name: &str) -> Peer { + let mut addr = self.addr; + addr.set_port(addr.port() + 1); + let mut names = self.names.clone(); + names.push(name.to_string()); + Peer { addr, names } + } +} + +/// `url`'s host, if it has one. +pub fn host(url: &url::Url) -> Option { + url.host_str().map(str::to_string) +} + +/// `path` resolved against `base`, or why it does not resolve. +pub fn resolve(base: url::Url, path: &str) -> Result { + base.join(path).map_err(|err| err.to_string()) +} + +/// Each address's port. +pub fn ports(addrs: Vec) -> Vec { + addrs.iter().map(std::net::SocketAddr::port).collect() +} + +/// `name` in `dir`. +pub fn file(dir: &std::path::Path, name: &str) -> std::path::PathBuf { + dir.join(name) +} + +/// Each path's parent, if it has one. +pub fn parents(paths: Vec) -> Vec> { + paths + .iter() + .map(|path| path.parent().map(std::path::Path::to_path_buf)) + .collect() +} + +/// The words of `text`. +pub fn words(text: &str) -> Vec { + text.split_whitespace().map(str::to_string).collect() +} + +/// `words` joined by spaces, or `none` for none. +pub fn sentence(words: Option>) -> String { + words.map_or_else(|| "none".to_string(), |words| words.join(" ")) +} + +/// `bytes` in chunks of up to `size`, which is not 0. +pub fn chunks(bytes: &[u8], size: usize) -> Vec> { + bytes.chunks(size).map(<[u8]>::to_vec).collect() +} + +/// A counter for each name, at 1. +pub fn counters(names: Vec) -> Vec { + names.iter().map(|name| Counter::new(name, 1)).collect() +} + +/// Each row's sum. +pub fn sums(rows: Vec>) -> Vec { + rows.iter().map(|row| row.iter().sum()).collect() +} diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index 2229dda24..4db94fc8c 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -8,7 +8,7 @@ fixtures/reject/src/lib.rs:113:2: rust2-ffi: cannot export method `Text::replace fixtures/reject/src/lib.rs:118:2: rust2-ffi: cannot export method `Text::later`: returned object that borrows `impl std::future::Future>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:125:1: rust2-ffi: cannot export fn `either`: reference inside a parameter `std::option::Option<&str>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:130:1: rust2-ffi: cannot export struct `Lease`: object no export returns; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:149:1: rust2-ffi: cannot export instance `Cell>`: Vec `std::string::String`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:149:1: rust2-ffi: cannot export instance `Cell>`: type argument `std::vec::Vec`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:14:1: rust2-ffi: cannot export fn `map`: foreign type `std::collections::HashMap`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:159:2: rust2-ffi: cannot export method `Cell::nest`: instance holding an instance of a type inside it `reject_fixture::Cell>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:165:1: rust2-ffi: cannot export fn `excluded_cell`: excluded type `reject_fixture::Cell`; exclude it in the config to leave it out @@ -25,6 +25,7 @@ fixtures/reject/src/lib.rs:212:1: rust2-ffi: cannot export fn `later`: closure k fixtures/reject/src/lib.rs:217:1: rust2-ffi: cannot export fn `twice`: closure bound `std::clone::Clone`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:223:1: rust2-ffi: cannot export fn `both`: closure type taken twice `F`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:229:1: rust2-ffi: cannot export fn `read`: lent object that borrows `&Text<'_>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:234:1: rust2-ffi: cannot export fn `count_words`: Vec of references `&str`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:26:1: rust2-ffi: cannot export fn `borrowing`: borrowing future from a function that is not async; exclude it in the config to leave it out fixtures/reject/src/lib.rs:31:1: rust2-ffi: cannot export fn `nested`: nested Option `std::option::Option>>`; 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 diff --git a/ffi/fixtures/reject/src/lib.rs b/ffi/fixtures/reject/src/lib.rs index dca98f6a2..bc420c639 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -229,3 +229,8 @@ pub fn both(a: F, b: F) { pub fn read(f: impl FnOnce(&Text<'_>)) { f(&Text("")) } + +/// Words C would lend inside an array, which C lends nothing inside. +pub fn count_words(words: Vec<&str>) -> usize { + words.len() +} diff --git a/ffi/moq/ffi.toml b/ffi/moq/ffi.toml index 8ee7e5425..063b55194 100644 --- a/ffi/moq/ffi.toml +++ b/ffi/moq/ffi.toml @@ -10,7 +10,6 @@ exclude = [ "ALPNS", "BoundsExceeded", "Client::connect", - "Client::with_url", "Consume", "DecodeError", "Driver", @@ -27,17 +26,10 @@ exclude = [ "Timestamp::as_millis", "Timestamp::as_nanos", "Timestamp::as_scale", - "Versions::alpns", "Versions::iter", "announce::Announce", "announce::Consumer::poll_synced", - "auth::Dial", "auth::EmptyToken", - "auth::Token", - "auth::Tokens::changed", - "auth::Tokens::dial", - "auth::Tokens::new", - "auth::Tokens::set", "bytes", "frame::Buffer", "frame::Producer::group", @@ -50,18 +42,13 @@ exclude = [ "server::Handshake", "stats::Failure::ALL", "stats::RefusalKind::ALL", - "stats::Report::sessions", - "stats::Report::traffic", - "stats::SessionEntry", "stats::Snapshot::control", "stats::Snapshot::sessions", "stats::Snapshot::traffic", - "stats::TrafficEntry", "time::Driver", "track::Fetching", "track::Resolving", "track::Subscribing", - "track::Subscription::ranges", "track::Subscription::with_ranges", "transport::Error", "transport::MaybeSend", @@ -128,18 +115,13 @@ exclude = [ [crates.moq-tokio] exclude = [ - "Addrs", "Client::connect", - "Listener::accept_health", - "Listener::local_addr", - "Listener::tcp_local_addr", - "Listener::websocket_local_addr", + "tcp::Error", + "websocket::Error", + "Addrs", "Log::level", "Log::new", "RedactedUrl", - "Server::accept_health", - "Server::local_addr", - "Server::websocket_local_addr", "accept::Failure::ALL", "accept::Failure::classify", "accept::Health::failed", @@ -156,11 +138,6 @@ exclude = [ "cli::check_env", "cli::usage_rs", "connect::Addr", - "connect::Config::bind", - "connect::Config::tokens", - "connect::Config::url", - "connect::Config::version", - "connect::Resolved::bind", "connection::Monitor::poll_presence", "crypto::install_default", "crypto::provider", @@ -168,18 +145,12 @@ exclude = [ "field@Log::level", "listen::Config::preferred_v4", "listen::Config::preferred_v6", - "listen::Config::version", - "listen::Reset::secret", "notify", "panic::isolate", "panic::message", - "quic::Config::keylog", - "quic::Config::qlog", "quic::Error", "quic::Limits", "quic::LimitsError", - "quic::Resolved::keylog", - "quic::Resolved::qlog", "quic::driver::AckFrequencyConfig", "quic::driver::ApplicationClose", "quic::driver::AsyncUdpSocket", @@ -200,7 +171,6 @@ exclude = [ "quic::driver::Connection::open_uni", "quic::driver::Connection::peer_identity", "quic::driver::Connection::read_datagram", - "quic::driver::Connection::remote_address", "quic::driver::Connection::send_datagram", "quic::driver::Connection::set_limits", "quic::driver::Connection::stats", @@ -245,28 +215,11 @@ exclude = [ "rustls", "secret::Secret::as_bytes", "server::Config::websocket", - "server::Link::local", - "server::Link::remote", - "server::Request::local_addr", - "server::Request::remote_addr", - "server::Request::url", "server::Request::within", - "tcp::Config::bind", - "tcp::Error", "tcp::Listener", - "tls::Certificates::fingerprints", "tls::Connect::build", - "tls::Connect::cert", - "tls::Connect::fingerprint", - "tls::Connect::key", - "tls::Connect::root", "tls::Identity::generate", - "tls::KeyLog", - "tls::Listen::cert", - "tls::Listen::generate", - "tls::Listen::key", "tls::Listen::load_roots", - "tls::Listen::root", "tls::Listen::server_config", "tls::PeerIdentity::chain", "tls::PeerIdentity::from_chain", @@ -277,24 +230,16 @@ exclude = [ "transport::RecvStream", "transport::SendStream", "transport::Session", - "unix::Allow::gid", - "unix::Allow::pid", - "unix::Allow::uid", - "unix::Config::bind", "unix::Listener::accept", - "unix::Listener::path", "unix::Listener::with_protocols", "watch::Files", - "websocket::Error", "websocket::Listener", "websocket::Options", "websocket::accept", "worker::Group::finished", - "worker::Group::local_addr", "worker::Group::members", "worker::Member", "worker::Spawner", - "worker::Workers::local_addr", ] [crates.moq-tokio.rename] @@ -314,16 +259,19 @@ exclude = [ [crates.hang] exclude = [ + "health::PublisherThresholds::skipped", + "health::TransportThresholds::headroom", + "health::TransportThresholds::loss", + "health::ViewerThresholds::errors", + "health::ViewerThresholds::late", + "health::ViewerThresholds::stalled", "Catalog::from_reader", - "Catalog::subscribe", - "catalog::Archive::store", "catalog::Archive::timelines", "catalog::Audio::enabled", "catalog::Audio::ranked", "catalog::Audio::renditions", "catalog::Binary::tracks", "catalog::BinaryConfig::extra", - "catalog::Consumer", "catalog::H265", "catalog::Json::tracks", "catalog::JsonConfig::extra", @@ -336,20 +284,11 @@ exclude = [ "catalog::Video::renditions", "container::Frame::encode", "echo::Snapshot", - "health::Connection::evidence", - "health::Evidence", "health::Health", "health::Limits", - "health::PublisherThresholds::skipped", - "health::Rendition::evidence", "health::Report", "health::Sample", - "health::TransportThresholds::headroom", - "health::TransportThresholds::loss", "health::TransportThresholds::rtt", - "health::ViewerThresholds::errors", - "health::ViewerThresholds::late", - "health::ViewerThresholds::stalled", "health::publisher", "health::viewer", "stats::Snapshot", @@ -370,9 +309,17 @@ exclude = [ [crates.moq-mux] exclude = [ + "catalog::Producer::audio", + "catalog::Producer::text", + "catalog::Producer::video", + "catalog::Reserved::audio", + "catalog::Reserved::text", + "catalog::Reserved::video", + "container::ts::Export", + "timeline::Entry", + "timeline::Producer::push", "Linger", "Source::bind", - "Source::catalog", "Source::follow", "Source::resolve", "Source::resolve_reference", @@ -382,23 +329,16 @@ exclude = [ "binary::Consumer::next", "binary::Consumer::poll_latest", "binary::Consumer::poll_next", - "catalog::Consumer", "catalog::Entry", "catalog::Guard", "catalog::IntoRendition", - "catalog::Producer::audio", "catalog::Producer::json_snapshot", "catalog::Producer::json_stream", "catalog::Producer::modify", "catalog::Producer::stats", - "catalog::Producer::text", "catalog::Producer::track", - "catalog::Producer::video", "catalog::RenditionConfig", - "catalog::Reserved::audio", - "catalog::Reserved::text", "catalog::Reserved::track", - "catalog::Reserved::video", "catalog::Select", "catalog::Stream", "catalog::hang::CatalogExt", @@ -414,22 +354,11 @@ exclude = [ "codec::annexb::length_prefixed_to_annexb", "codec::av1::Error", "codec::av1::Import::decode", - "codec::av1::Split::decode", - "codec::av1::Split::flush", "codec::flac::Config", - "codec::h264::Avcc::pps", - "codec::h264::Avcc::sps", "codec::h264::Export", "codec::h264::Import::decode", - "codec::h264::Split::decode", - "codec::h264::Split::flush", "codec::h265::Export", - "codec::h265::Hvcc::pps", - "codec::h265::Hvcc::sps", - "codec::h265::Hvcc::vps", "codec::h265::Import::decode", - "codec::h265::Split::decode", - "codec::h265::Split::flush", "codec::opus::Config::parse", "codec::opus::Mapping::new", "codec::opus::mapping::Config", @@ -439,26 +368,20 @@ exclude = [ "container::Producer", "container::flv::Export", "container::fmp4::Export", - "container::fmp4::Fragmenter::push", - "container::fmp4::Fragmenter::push_group", "container::fmp4::Muxer::fragment", - "container::fmp4::Muxer::read", "container::fmp4::Wire::new", "container::fmp4::Wire::trak", "container::fmp4::mp4_atom", "container::mkv::Error", "container::mkv::Export", "container::ts::Catalog", - "container::ts::Export", "container::ts::Follower", "container::ts::Import", - "container::ts::Mpegts::program_descriptors", "container::ts::Mpegts::si", "container::ts::Mpegts::tracks", "container::ts::MultipleProgramsError", "container::ts::Programs::decode", "container::ts::Programs::finish", - "container::ts::Track::descriptors", "container::ts::stats::Export::streams", "container::ts::stats::Snapshot::streams", "follow::Event", @@ -466,14 +389,14 @@ exclude = [ "json::Consumer", "json::Snapshot", "json::Stream", - "timeline::Entry", - "timeline::Producer::push", ] [crates.moq-mux.rename] -# The rule names these and moq-net's `Error` and hang's `container::Frame` alike. +# The rule names these and moq-net's `Error`, and hang's `container::Frame` and +# `catalog::Consumer`, alike. "Error" = "mux_error" "container::Frame" = "mux_container_frame" +"catalog::Consumer" = "mux_catalog_consumer" # The rule makes both this constant and `CatalogFormat`'s `Default` `moq_catalog_catalog_format_default`. "catalog::CatalogFormat::DEFAULT" = "catalog_catalog_format_fallback" # The rule makes both this constant and the method `Clock::timescale` `moq_clock_timescale`. diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 6fa284343..92ba301d6 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -315,6 +315,45 @@ int main(void) { CHECK(initial.secs == 1 && initial.nanos == 0); moq_backoff_free(backoff); + /* Session config's foreign types. A URL is parsed from text, failing with why. */ + moq_url *url = NULL; + moq_buf parse_err; + CHECK(!moq_url_parse(str("relay.example"), &url, &parse_err)); + CHECK(is(parse_err, "relative URL without a base")); + CHECK(moq_url_parse(str("https://relay.example:4443/demo"), &url, &parse_err)); + moq_auth_tokens *tokens = moq_auth_tokens_new((moq_vec_auth_token){0}); + moq_versions *versions = moq_versions_default(); + moq_auth_dial *dial = moq_auth_tokens_dial(tokens, url, versions); + moq_url *dialed = moq_auth_dial_url(dial); + CHECK(moq_url_eq(dialed, url)); + CHECK(is(moq_url_to_string(dialed), "https://relay.example:4443/demo")); + moq_url_free(dialed); + moq_auth_dial_free(dial); + moq_auth_tokens_free(tokens); + moq_url_free(url); + /* A `Vec`, each element a buffer the array frees. */ + moq_vec_string alpns = moq_versions_alpns(versions); + CHECK(alpns.len > 0); + for (size_t i = 0; i < alpns.len; i++) { + CHECK(alpns.ptr[i].len > 0); + } + moq_vec_string_free(alpns); + moq_versions_free(versions); + /* A socket address, crossing in a listener's bind. */ + moq_listen_bind bind = {.tag = MOQ_LISTEN_BIND_ADDR, .addr = {._0 = {.ip = {[15] = 1}, .port = 4443, .v6 = true}}}; + CHECK(is(moq_listen_bind_to_string(&bind), "[::1]:4443")); + moq_listen_bind v4 = {.tag = MOQ_LISTEN_BIND_ADDR, .addr = {._0 = {.ip = {127, 0, 0, 1}, .port = 0}}}; + CHECK(is(moq_listen_bind_to_string(&v4), "127.0.0.1:0")); + CHECK(!moq_listen_bind_eq(&bind, &v4)); + /* TLS files, as OS paths, and fingerprints, as strings: none by default. */ + moq_tls_connect *tls = moq_tls_connect_default(); + moq_vec_path roots = moq_tls_connect_root(tls); + moq_vec_string fingerprints = moq_tls_connect_fingerprint(tls); + CHECK(roots.ptr != NULL && roots.len == 0 && fingerprints.len == 0); + moq_vec_path_free(roots); + moq_vec_string_free(fingerprints); + moq_tls_connect_free(tls); + if (failures == 0) { printf("ok\n"); } diff --git a/ffi/runtime/rt.hpp b/ffi/runtime/rt.hpp index deb5cc1c2..fd8060a54 100644 --- a/ffi/runtime/rt.hpp +++ b/ffi/runtime/rt.hpp @@ -513,6 +513,43 @@ inline c_buf none() noexcept { return c_buf{nullptr, 0, nullptr}; } +// The arrays a call's lowered vectors point into, kept until the call returns. +class arena { + ::std::vector<::std::shared_ptr> held_; + +public: + // `items` as the C array `C`, each lowered by `lower`: never null, so an empty one is + // told apart from a missing one. + template + C list(const R &items, F lower) { + using E = ::std::remove_const_t<::std::remove_pointer_t>; + auto array = ::std::make_shared<::std::vector>(); + array->reserve(items.size()); + for (const auto &item : items) { + array->push_back(lower(item)); + } + static const E empty{}; + const E *ptr = array->empty() ? &empty : array->data(); + ::std::size_t len = array->size(); + held_.push_back(::std::move(array)); + return C{ptr, len, nullptr}; + } +}; + +// The C array `c` Rust returned, as `T`s each lifted by `lift`, which takes what its +// element holds; `free` then frees the array alone. +template +::std::vector lift_list(C c, F lift, Free free) { + ::std::vector out; + out.reserve(c.len); + for (::std::size_t i = 0; i < c.len; i++) { + out.push_back(lift(c.ptr[i])); + } + c.len = 0; + free(c); + return out; +} + // An object Rust lends a closure for one call, as its class over the lent handle, which // it gives back unfreed. Moving out of it would keep the borrow past the call. template diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index f2b34e6a1..942b6ffe5 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -93,6 +93,31 @@ impl Buf { Self::owned(vec.into()) } + /// The path a caller passed in, which it keeps: its bytes as the OS holds them on + /// Unix, which may be any, else UTF-8. + /// + /// # Safety + /// As [`Self::bytes`]. + pub unsafe fn path(&self, what: &str) -> &std::path::Path { + #[cfg(unix)] + { + use std::os::unix::ffi::OsStrExt; + // SAFETY: the caller's contract. + std::path::Path::new(std::ffi::OsStr::from_bytes(unsafe { self.bytes(what) })) + } + #[cfg(not(unix))] + { + // SAFETY: the caller's contract. + std::path::Path::new(unsafe { self.str(what) }) + } + } + + /// A path handed to the caller: its bytes as the OS holds them on Unix, else as + /// Rust encodes them, which is UTF-8 for any path that has a UTF-8 form. + pub fn path_buf(path: std::path::PathBuf) -> Self { + Self::vec(path.into_os_string().into_encoded_bytes()) + } + /// Free a buffer Rust returned; a view or `None` frees nothing. /// /// # Safety @@ -128,6 +153,119 @@ impl Duration { } } +/// `std::net::SocketAddr`: an IPv4 address in the first 4 bytes of `ip`, with the rest of +/// `ip`, `flowinfo`, and `scope_id` zero, or an IPv6 address. `ip` is in network order and +/// `port` in the host's. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SocketAddr { + pub ip: [u8; 16], + pub port: u16, + pub v6: bool, + pub flowinfo: u32, + pub scope_id: u32, +} + +impl SocketAddr { + pub fn lift(self, what: &str) -> std::net::SocketAddr { + if self.v6 { + let ip = std::net::Ipv6Addr::from(self.ip); + return std::net::SocketAddrV6::new(ip, self.port, self.flowinfo, self.scope_id).into(); + } + let [a, b, c, d, rest @ ..] = self.ip; + if rest != [0; 12] || self.flowinfo != 0 || self.scope_id != 0 { + fail(what, "an IPv4 socket address has IPv6 bytes or fields"); + } + std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(a, b, c, d), self.port).into() + } + + pub fn lower(addr: std::net::SocketAddr) -> Self { + match addr { + std::net::SocketAddr::V4(v4) => { + let mut ip = [0; 16]; + ip[..4].copy_from_slice(&v4.ip().octets()); + Self { + ip, + port: v4.port(), + v6: false, + flowinfo: 0, + scope_id: 0, + } + } + std::net::SocketAddr::V6(v6) => Self { + ip: v6.ip().octets(), + port: v6.port(), + v6: true, + flowinfo: v6.flowinfo(), + scope_id: v6.scope_id(), + }, + } + } +} + +/// `Vec` across the ABI, as an array of its elements' C values. Passed in, a view Rust +/// copies each element out of and never frees. Returned, an array the caller frees once, +/// with what its first `len` elements hold. A null `ptr` is `None`. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct List { + pub ptr: *const T, + pub len: usize, + pub owner: *mut c_void, +} + +impl List { + pub const NONE: Self = Self { + ptr: std::ptr::null(), + len: 0, + owner: std::ptr::null_mut(), + }; + + pub fn is_none(&self) -> bool { + self.ptr.is_null() + } + + /// The elements a caller passed in, which it keeps. + /// + /// # Safety + /// `ptr` points at `len` readable elements while `self` lives. + pub unsafe fn items(&self, what: &str) -> &[T] { + match (self.ptr.is_null(), self.len) { + (_, 0) => &[], + (true, _) => fail(what, "an array has a length but no pointer"), + // SAFETY: the caller's contract. + (false, len) => unsafe { std::slice::from_raw_parts(self.ptr, len) }, + } + } + + /// Elements handed to the caller, which frees them. + pub fn owned(items: Vec) -> Self { + let items = Box::new(items); + Self { + ptr: items.as_ptr(), + len: items.len(), + owner: Box::into_raw(items).cast(), + } + } + + /// Free an array Rust returned, after `each` frees what each of its first `len` + /// elements holds: a binding that took the elements frees the array with `len` 0. + /// + /// # Safety + /// `owner` is null or came from [`Self::owned`], and is freed once. + pub unsafe fn free(self, what: &str, each: impl Fn(T)) { + if self.owner.is_null() { + return; + } + // SAFETY: the caller's contract. + let items = unsafe { Box::from_raw(self.owner.cast::>()) }; + if !std::ptr::eq(self.ptr, items.as_ptr()) || self.len > items.len() { + fail(what, "an array is not as Rust returned it"); + } + items[..self.len].iter().copied().for_each(each); + } +} + /// `std::ops::Range`. #[repr(C)] #[derive(Clone, Copy)] diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 39bce0545..6c3a2fa44 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -31,6 +31,8 @@ pub enum Ty { Buf, /// `_duration` Duration, + /// `_socket_addr` + SocketAddr, /// A struct: a record, a data enum, an option, or a callback's table and data. Struct(String), /// A plain enum, passed as its C enum, which is an `int`. @@ -89,6 +91,8 @@ pub struct Abi<'a> { pub options: BTreeMap, /// Each range, bound, and pair of bounds struct by C name, with its type. pub ranges: BTreeMap, + /// Each `Vec` struct by C name, with `T`. + pub lists: BTreeMap, /// Every C name claimed, with what claimed it. names: HashMap, /// Each name claimed twice. @@ -105,6 +109,7 @@ impl<'a> Abi<'a> { suffixes: Default::default(), options: BTreeMap::new(), ranges: BTreeMap::new(), + lists: BTreeMap::new(), names: HashMap::new(), clashes: Vec::new(), }; @@ -288,6 +293,11 @@ impl<'a> Abi<'a> { format!("{}_option_{}", self.prefix, self.key(inner)) } + /// The C name of `Vec`'s struct: `_vec_string`. + pub fn list(&self, elem: &Type) -> String { + format!("{}_vec_{}", self.prefix, self.key(elem)) + } + /// The C name of a range, bound, or pair of bounds struct: `_range_u64`. pub fn range(&self, ty: &Type) -> String { format!("{}_{}", self.prefix, self.key(ty)) @@ -299,12 +309,17 @@ impl<'a> Abi<'a> { match ty { Type::Unit => "unit".to_string(), Type::Bool => "bool".to_string(), - Type::String => "string".to_string(), + Type::Str | Type::String => "string".to_string(), + Type::Slice | Type::Vec | Type::Bytes => "bytes".to_string(), + Type::Path | Type::PathBuf => "path".to_string(), Type::Int(int) => int.rust().to_string(), Type::F32 => "f32".to_string(), Type::F64 => "f64".to_string(), Type::Char => "char".to_string(), Type::Duration => "duration".to_string(), + Type::SocketAddr => "socket_addr".to_string(), + Type::Option(inner) => format!("option_{}", self.key(inner)), + Type::List(inner) => format!("vec_{}", self.key(inner)), Type::Record(i) => strip(self.record(*i)), Type::Enum(i) => strip(self.enumeration(*i)), Type::Object(i) => strip(self.object(*i)), @@ -320,10 +335,16 @@ impl<'a> Abi<'a> { pub fn is_buf(ty: &Type) -> bool { matches!( ty.base(), - Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes + Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes | Type::Path | Type::PathBuf ) } + /// Whether `Option` crosses as `ty`, with a null pointer for `None`: a handle, a + /// buffer, or a `Vec`'s array. + pub fn nullable(ty: &Type) -> bool { + matches!(ty.base(), Type::Object(_) | Type::List(_)) || Self::is_buf(ty) + } + /// The C type of `ty` as a value: a parameter, field, or what a function returns. pub fn ty(&self, ty: &Type) -> Ty { match ty { @@ -333,13 +354,14 @@ impl<'a> Abi<'a> { Type::F32 => Ty::F32, Type::F64 => Ty::F64, Type::Char => Ty::Char, - Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes => Ty::Buf, + Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes | Type::Path | Type::PathBuf => Ty::Buf, Type::Duration => Ty::Duration, + Type::SocketAddr => Ty::SocketAddr, Type::Waker => Ty::Waker, - Type::Option(inner) => match inner.base() { - Type::Object(_) => self.ty(inner), - base if Self::is_buf(base) => Ty::Buf, - _ => Ty::Struct(self.option(inner)), + Type::List(inner) => Ty::Struct(self.list(inner)), + Type::Option(inner) => match Self::nullable(inner) { + true => self.ty(inner), + false => Ty::Struct(self.option(inner)), }, Type::Box(inner) => self.ty(inner), Type::Ref(m, inner) => match &**inner { @@ -373,6 +395,7 @@ impl<'a> Abi<'a> { Type::Ref(_, inner) => owned(inner), Type::Option(inner) => Type::Option(Box::new(owned(inner))), Type::Box(inner) => Type::Box(Box::new(owned(inner))), + Type::List(inner) => Type::List(Box::new(owned(inner))), ty => ty.clone(), } } @@ -503,7 +526,18 @@ impl<'a> Abi<'a> { .unwrap_or(rustc_span::DUMMY_SP); // C's runtime, then what C++'s adds in the namespace its types share. for runtime in [ - "contract", "buf", "buf_free", "duration", "waker", "poll", "view", "result", "task", "future", "poll_t", + "contract", + "buf", + "buf_free", + "duration", + "socket_addr", + "waker", + "poll", + "view", + "result", + "task", + "future", + "poll_t", "detail", ] { self.claim(self.rt(runtime), "the runtime".to_string(), crate_span); @@ -596,15 +630,20 @@ impl<'a> Abi<'a> { } let mut options = BTreeMap::new(); let mut ranges = BTreeMap::new(); + let mut lists = BTreeMap::new(); let mut stack = types; while let Some(ty) = stack.pop() { match ty { Type::Option(inner) => { - if !matches!(inner.base(), Type::Object(_)) && !Self::is_buf(inner) { + if !Self::nullable(inner) { options.insert(self.option(inner), (**inner).clone()); } stack.push(inner); } + Type::List(inner) => { + lists.insert(self.list(inner), (**inner).clone()); + stack.push(inner); + } Type::Ref(_, inner) | Type::Box(inner) => stack.push(inner), Type::Tuple(elements) => stack.extend(elements), Type::Closure(params, ret) => { @@ -627,6 +666,10 @@ impl<'a> Abi<'a> { for name in ranges.keys() { self.claim(name.clone(), "a range".to_string(), crate_span); } + for name in lists.keys() { + self.claim(name.clone(), "a Vec".to_string(), crate_span); + self.claim(format!("{name}_free"), "a Vec".to_string(), crate_span); + } if ranges.values().any(|ty| matches!(ty, Type::Bound(_))) { let bound = self.rt("bound"); for kind in ["", "_UNBOUNDED", "_INCLUDED", "_EXCLUDED"] { @@ -639,6 +682,7 @@ impl<'a> Abi<'a> { } self.options = options; self.ranges = ranges; + self.lists = lists; } /// Claim the functions a type's std traits export. diff --git a/ffi/src/c.rs b/ffi/src/c.rs index 02a800884..bf333850e 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -78,6 +78,9 @@ impl Header<'_> { self.line(format!( "typedef struct {p}_duration {{\n\tuint64_t secs;\n\tuint32_t nanos;\n}} {p}_duration;\n" )); + self.line(format!( + "/* A socket address: IPv4 in the first 4 bytes of ip, with the rest of ip, flowinfo,\n * and scope_id zero, or IPv6 if v6. ip is in network order and port in the host's. */\ntypedef struct {p}_socket_addr {{\n\tuint8_t ip[16];\n\tuint16_t port;\n\tbool v6;\n\tuint32_t flowinfo;\n\tuint32_t scope_id;\n}} {p}_socket_addr;\n" + )); self.line(format!( "/* Wakes a future's poller: wake may be called from any thread, any number of\n * times, until drop releases data. A poll passing the same wake and data again\n * reuses the waker. Either function may be null. */\ntypedef struct {p}_waker {{\n\tvoid (*wake)(void *data);\n\tvoid (*drop)(void *data);\n\tvoid *data;\n}} {p}_waker;\n" )); @@ -117,6 +120,9 @@ impl Header<'_> { } // Value structs, each after the values it holds. + for name in abi.lists.keys() { + self.value(&Ty::Struct(name.clone())); + } for name in abi.ranges.keys() { self.value(&Ty::Struct(name.clone())); } @@ -256,6 +262,18 @@ impl Header<'_> { self.line(format!("typedef struct {name} {{\n{fields}\n}} {name};\n")); return; } + if let Some(elem) = abi.lists.get(name) { + let elem = abi.ty(elem); + self.value(&elem); + let ptr = match elem { + Ty::Handle { name, .. } => format!("{name} *const *ptr"), + elem => self.decl(&Ty::In(Box::new(elem)), "ptr"), + }; + self.line(format!( + "/* A Vec. Passed in, an array Rust reads during the call and never frees. Returned,\n * an array to free once with {name}_free, which frees its first len elements too:\n * set len to 0 first to keep them. A null ptr is None. */\ntypedef struct {name} {{\n\t{ptr};\n\tsize_t len;\n\tvoid *owner;\n}} {name};\n\nvoid {name}_free({name} vec);\n" + )); + return; + } if let Some(inner) = abi.options.get(name) { let inner = abi.ty(inner); self.value(&inner); @@ -421,6 +439,7 @@ pub fn decl(abi: &Abi<'_>, ty: &Ty, name: &str) -> String { Ty::Char => space("uint32_t"), Ty::Buf => space(&abi.rt("buf")), Ty::Duration => space(&abi.rt("duration")), + Ty::SocketAddr => space(&abi.rt("socket_addr")), Ty::Struct(n) | Ty::Enum(n) => space(n), Ty::Handle { name: n, constant } => match constant { true => format!("const {n} *{name}"), diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 72e5f65ca..182881169 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -158,6 +158,8 @@ 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)>, /// `std::ops::Bound`, and `std::ops::RangeBounds`, which names it. bound: Option, range_bounds: Option, @@ -168,16 +170,15 @@ struct Collector<'tcx> { /// Collect the exported crates' API, leaving out what each one's `exclude` names. pub fn collect<'tcx>(tcx: TyCtxt<'tcx>, crates: &[Crate<'_>]) -> Collected { - let bytes = tcx - .crates(()) - .iter() - .find(|&&krate| tcx.crate_name(krate).as_str() == "bytes") - .and_then(|&krate| { - tcx.module_children(krate.as_def_id()) - .iter() - .find(|child| child.ident.as_str() == "Bytes") - .and_then(|child| child.res.opt_def_id()) - }); + // A type at a crate's root, by the crate's name and its own. + let root = |krate: &str, name: &str| { + let krate = *tcx.crates(()).iter().find(|&&k| tcx.crate_name(k).as_str() == krate)?; + (tcx.module_children(krate.as_def_id()).iter()) + .find(|child| child.ident.as_str() == name) + .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 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| { @@ -217,6 +218,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)), bound, range_bounds, orphans: orphans.clone(), @@ -233,6 +235,7 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>, crates: &[Crate<'_>]) -> Collected { c.infer(named); c.declare(); c.define(); + c.prune(); c }; // Refusing an object no export returns refuses what takes it, which may leave another @@ -757,6 +760,46 @@ 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 { + clone: true, + eq: true, + display: true, + debug: true, + default: false, + }, + }); + } } /// How a type or trait is exported: a struct whose fields are all `pub`, or an enum, @@ -1105,6 +1148,32 @@ 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. + 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(); + } + } + /// A getter for each `pub` field of a struct exported as an object, returning a copy. fn getters(&mut self, site: &Site<'tcx>) -> Vec { let tcx = self.tcx; @@ -2235,6 +2304,9 @@ impl<'tcx> Collector<'tcx> { ty::Ref(_, inner, m) => { let inner_ty = match inner.kind() { ty::Str => Type::Str, + ty::Adt(adt, _) if tcx.is_diagnostic_item(rustc_span::Symbol::intern("Path"), adt.did()) => { + Type::Path + } ty::Slice(elem) if *elem == tcx.types.u8 => Type::Slice, ty::Slice(elem) => return Err(why("slice", elem.to_string())), ty::Dynamic(..) => return Err(why("trait object", inner.to_string())), @@ -2252,7 +2324,7 @@ impl<'tcx> Collector<'tcx> { }; match (&inner_ty, pos, mutability) { // Returned as a clone: a handle in it is a new handle to its clone. - (Type::Str | Type::Slice, Pos::Return | Pos::Returned, _) => {} + (Type::Str | Type::Slice | Type::Path, Pos::Return | Pos::Returned, _) => {} (_, Pos::Return | Pos::Returned, _) if !self.is_clone(*inner) => { return Err(why("borrowed return that is not Clone", inner.to_string())); } @@ -2273,14 +2345,30 @@ impl<'tcx> Collector<'tcx> { if tcx.lang_items().string() == Some(did) { Type::String } else if tcx.is_diagnostic_item(sym::Vec, did) { - match args.type_at(0) == tcx.types.u8 { - true => Type::Vec, - false => return Err(why("Vec", args.type_at(0).to_string())), + let elem = args.type_at(0); + if elem == tcx.types.u8 { + Type::Vec + } else { + let inner = self.ty(elem, pos.inner())?; + // C lends nothing inside an array, and an element lowered back into + // one is a clone of what Rust lent. + if matches!(inner, Type::Ref(..)) && !matches!(pos, Pos::Return | Pos::Returned) { + return Err(why("Vec of references", elem.to_string())); + } + Type::List(Box::new(inner)) } } else if Some(did) == self.bytes { Type::Bytes } else if tcx.is_diagnostic_item(rustc_span::Symbol::intern("Duration"), did) { Type::Duration + } else if tcx.is_diagnostic_item(rustc_span::Symbol::intern("PathBuf"), did) { + Type::PathBuf + } else if tcx.crate_name(did.krate) == sym::core && tcx.item_name(did).as_str() == "SocketAddr" { + Type::SocketAddr + } else if let Some((url, i)) = self.url + && did == url + { + Type::Object(i) } else if tcx.is_diagnostic_item(rustc_span::Symbol::intern("Waker"), did) { match pos { Pos::Param => Type::Waker, @@ -2404,9 +2492,9 @@ enum Pos { /// copied by it if kept. fn foreign_arg(ty: &Type) -> bool { match ty { - Type::Bool | Type::Int(_) | Type::F32 | Type::F64 | Type::Char | Type::Duration => true, - Type::String | Type::Vec | Type::Bytes => true, - Type::Ref(Mutability::Not, inner) => matches!(**inner, Type::Str | Type::Slice), + Type::Bool | Type::Int(_) | Type::F32 | Type::F64 | Type::Char | Type::Duration | Type::SocketAddr => true, + Type::String | Type::Vec | Type::Bytes | Type::PathBuf => true, + Type::Ref(Mutability::Not, inner) => matches!(**inner, Type::Str | Type::Slice | Type::Path), Type::Option(inner) => foreign_arg(inner), _ => false, } @@ -2430,7 +2518,9 @@ fn buildable(api: &Api, ty: &Type, held: &[bool]) -> bool { fn returns(api: &Api, ty: &Type, objects: &mut Vec) { match ty { Type::Object(i) => objects.push(*i), - Type::Ref(_, inner) | Type::Box(inner) | Type::Option(inner) => returns(api, inner, objects), + Type::Ref(_, inner) | Type::Box(inner) | Type::Option(inner) | Type::List(inner) => { + returns(api, inner, objects) + } Type::Tuple(elements) => elements.iter().for_each(|ty| returns(api, ty, objects)), Type::Record(i) => api.records[*i].fields.iter().for_each(|f| returns(api, &f.ty, objects)), Type::Enum(i) => api.enums[*i] @@ -2442,6 +2532,17 @@ fn returns(api: &Api, ty: &Type, objects: &mut Vec) { } } +/// Whether `ty` names the object `i`. +fn names(ty: &Type, i: usize) -> bool { + match ty { + Type::Object(o) => *o == i, + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(inner) => names(inner, i), + Type::Tuple(elements) => elements.iter().any(|ty| names(ty, i)), + Type::Closure(params, ret) => params.iter().chain([&**ret]).any(|ty| names(ty, i)), + _ => false, + } +} + /// Whether `ty` holds a reference inside it, such as `Option<&T>`. fn lends_inside(ty: &Type) -> bool { match ty { diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 96257c29d..60d76031d 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -93,8 +93,12 @@ impl<'a> Header<'a> { Type::F32 => "float".to_string(), Type::F64 => "double".to_string(), Type::Char => "char32_t".to_string(), - Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes => format!("::{}::buf", self.ns), + Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes | Type::Path | Type::PathBuf => { + format!("::{}::buf", self.ns) + } Type::Duration => format!("::{}::duration", self.ns), + Type::SocketAddr => format!("::{}::socket_addr", self.ns), + Type::List(inner) => format!("::std::vector<{}>", self.owned(inner)), Type::Waker => format!("::{}::waker", self.ns), Type::Option(inner) => format!("::std::optional<{}>", self.owned(inner)), Type::Box(inner) | Type::Ref(_, inner) => self.owned(inner), @@ -119,6 +123,12 @@ impl<'a> Header<'a> { } match ty { Type::Option(inner) if Abi::is_buf(inner) => format!("::std::optional<::{}::view>", self.ns), + Type::Option(inner) if matches!(inner.base(), Type::List(_)) => { + format!("const ::std::optional<{}> &", self.items(inner.base())) + } + // Moved out of, each handle as a handle taken by value is. + Type::List(inner) if moves(abi, inner) => self.items(ty), + Type::List(_) => format!("const {} &", self.items(ty)), Type::Option(inner) => match &**inner { // Lent, or copied out: a pointer, null for none. Type::Ref(m, object) if matches!(**object, Type::Object(_)) => { @@ -148,6 +158,50 @@ impl<'a> Header<'a> { } } + /// The C++ vector a `Vec` parameter of type `ty` takes: of views, for bytes. + fn items(&self, ty: &Type) -> String { + let Type::List(inner) = ty else { + unreachable!("{ty:?} is no Vec") + }; + match Abi::is_buf(inner) { + true => format!("::std::vector<::{}::view>", self.ns), + false => format!("::std::vector<{}>", self.owned(inner)), + } + } + + /// Whether lowering a `ty` lowers a `Vec`, whose array lives in the arena `a_` until + /// the call returns. + fn arena(&self, ty: &Type) -> bool { + let api = self.abi.api; + match ty { + Type::List(_) => true, + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) => self.arena(inner), + Type::Record(i) => api.records[*i].fields.iter().any(|f| self.arena(&f.ty)), + Type::Enum(i) => (api.enums[*i].variants.iter()).any(|v| v.fields.iter().any(|f| self.arena(&f.ty))), + _ => false, + } + } + + /// The arena a value's `lower` takes, if it lowers a `Vec`. + fn arena_param(&self, ty: &Type) -> String { + match self.arena(ty) { + true => format!(", {}::arena &a_", self.d()), + false => String::new(), + } + } + + /// The arena a body lowering each of `tys` declares, if one needs it. + fn scope<'t>(&self, tys: impl IntoIterator) -> String { + match tys.into_iter().any(|ty| self.arena(ty)) { + true => format!( + " {}::arena a_; +", + self.d() + ), + false => String::new(), + } + } + /// The C value of the C++ value `e` of type `ty`, as a parameter or a field passes it. fn lower(&self, ty: &Type, e: &str) -> String { let abi = self.abi; @@ -160,10 +214,20 @@ impl<'a> Header<'a> { Type::Unit => unreachable!("no value is unit"), Type::Bool | Type::Int(_) | Type::F32 | Type::F64 => e.to_string(), Type::Char => format!("static_cast({e})"), - Type::Duration | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => e.to_string(), + Type::Duration | Type::SocketAddr | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => e.to_string(), Type::Waker => format!("{d}::give({d}::access::raw({e}))"), + Type::List(inner) => format!( + "a_.list<::{}>({e}, [&](const auto &x) {{ return {}; }})", + abi.list(inner), + self.lower(inner, "x") + ), Type::Option(inner) => match inner.base() { Type::Object(_) => format!("({e} ? {d}::access::raw(*{e}) : nullptr)"), + base @ Type::List(elem) => format!( + "({e} ? {} : ::{}{{}})", + self.lower(base, &format!("(*{e})")), + abi.list(elem) + ), // Through a view, so an empty buffer C++ made crosses as `Some`, not as null. _ if Abi::is_buf(inner) => format!("({e} ? {d}::access::raw(::{ns}::view(*{e})) : {d}::none())"), _ => { @@ -173,6 +237,7 @@ impl<'a> Header<'a> { } }, Type::Box(inner) | Type::Ref(_, inner) => self.lower(inner, e), + Type::Record(_) | Type::Enum(_) if self.arena(ty) => format!("{d}::lower({e}, a_)"), Type::Record(_) => format!("{d}::lower({e})"), Type::Enum(i) => match abi.api.enums[*i].plain() { true => format!("static_cast<::{}>({e})", abi.enumeration(*i)), @@ -181,7 +246,9 @@ impl<'a> Header<'a> { Type::Object(_) => format!("{d}::access::raw({e})"), Type::Callback(..) => format!("{d}::lower(::std::move({e}))"), Type::Closure(..) => unreachable!("a closure lowers to its trampoline"), - Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes => unreachable!("a buffer is above"), + Type::Str | Type::String | Type::Slice | Type::Vec | Type::Bytes | Type::Path | Type::PathBuf => { + unreachable!("a buffer is above") + } Type::Tuple(_) => unreachable!("a tuple is only returned"), } } @@ -197,13 +264,23 @@ impl<'a> Header<'a> { match ty { Type::Bool | Type::Int(_) | Type::F32 | Type::F64 => e.to_string(), Type::Char => format!("static_cast({e})"), - Type::Duration | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => e.to_string(), + Type::Duration | Type::SocketAddr | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => e.to_string(), + Type::List(inner) => format!( + "{d}::lift_list<{}>({e}, [&](const auto &c) {{ return {}; }}, &::{}_free)", + self.owned(inner), + self.lift(inner, "c"), + abi.list(inner) + ), Type::Option(inner) => { let owned = self.owned(inner); match inner.base() { Type::Object(_) => { format!("({e} ? ::std::optional<{owned}>({d}::access::wrap<{owned}>({e})) : ::std::nullopt)") } + Type::List(_) => format!( + "({e}.ptr ? ::std::optional<{owned}>({}) : ::std::nullopt)", + self.lift(inner, e) + ), _ if Abi::is_buf(inner) => format!("{d}::access::adopt_option({e})"), _ => format!( "({e}.some ? ::std::optional<{owned}>({}) : ::std::nullopt)", @@ -276,6 +353,7 @@ impl<'a> Header<'a> { "type_traits", "utility", "variant", + "vector", ] { self.line(format!("#include <{include}>")); } @@ -292,6 +370,7 @@ impl<'a> Header<'a> { upper = p.to_uppercase() )); self.line(format!("using duration = ::{p}_duration;")); + self.line(format!("using socket_addr = ::{p}_socket_addr;")); if abi.ranges.values().any(|ty| matches!(ty, Type::Bound(_))) { self.line(format!("using bound = ::{};", abi.rt("bound"))); } @@ -488,7 +567,7 @@ impl<'a> Header<'a> { )); } let mut members = Vec::new(); - self.traits(&name, &c, t, true, &mut body, &mut members); + self.traits(&name, &c, t, None, &mut body, &mut members); let owner = Ty::Handle { name: c.clone(), constant: false, @@ -508,21 +587,25 @@ impl<'a> Header<'a> { } /// The members a type's std traits export beyond its operators: an object's, which - /// holds a handle, or a record's, which is lowered. + /// holds a handle, or a record's, `value`, which is lowered. fn traits( &mut self, name: &str, c: &str, t: Traits, - object: bool, + value: Option<&Type>, body: &mut Vec, members: &mut Vec, ) { let ns = self.ns.clone(); let d = self.d(); - let (this, lower) = match object { - true => ("raw_", String::new()), - false => ("&c_self_", format!("\tauto c_self_ = {d}::lower(*this);\n")), + let object = value.is_none(); + let (this, lower) = match value { + None => ("raw_", String::new()), + Some(ty) => ( + "&c_self_", + format!("{}\tauto c_self_ = {};\n", self.scope([ty]), self.lower(ty, "*this")), + ), }; if t.display { body.push(format!("\t::{ns}::buf to_string() const;")); @@ -640,7 +723,9 @@ impl<'a> Header<'a> { let c = match ty { Type::Record(i) => abi.record(*i), Type::Enum(i) if !abi.api.enums[*i].plain() => abi.enumeration(*i), - Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) => return self.value(inner), + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(inner) => { + return self.value(inner); + } Type::Tuple(elements) => { for element in elements { self.value(element); @@ -674,11 +759,13 @@ impl<'a> Header<'a> { "\tbool operator==(const {name} &other) const;\n\tbool operator!=(const {name} &other) const {{\n\t\treturn !(*this == other);\n\t}}" )); self.def(format!( - "inline bool {name}::operator==(const {name} &other) const {{\n\tauto a = {d}::lower(*this);\n\tauto b = {d}::lower(other);\n\treturn ::{c}_eq(&a, &b);\n}}\n", - d = self.d() + "inline bool {name}::operator==(const {name} &other) const {{\n{}\tauto a = {};\n\tauto b = {};\n\treturn ::{c}_eq(&a, &b);\n}}\n", + self.scope([ty]), + self.lower(ty, "*this"), + self.lower(ty, "other"), )); } - self.traits(&name, &c, t, false, &mut body, &mut members); + self.traits(&name, &c, t, Some(ty), &mut body, &mut members); let owner = Ty::Struct(c.clone()); let mut taken = fields.clone(); taken.push(name.clone()); @@ -732,17 +819,18 @@ impl<'a> Header<'a> { let lifted = self.lift(&ty, "c_ret_"); let lower = |e: &str| self.lower(&ty, e); let (a, b, this) = (lower("a"), lower("b"), lower("self")); + let scope = self.scope([&ty]); let t = e.traits; if t.eq { let (a_, b_) = (declare(¶m, "a"), declare(¶m, "b")); self.def(format!( - "inline bool operator==({a_}, {b_}) {{\n\tauto c_a_ = {a};\n\tauto c_b_ = {b};\n\treturn ::{c}_eq(&c_a_, &c_b_);\n}}\n\ninline bool operator!=({a_}, {b_}) {{\n\treturn !(a == b);\n}}\n" + "inline bool operator==({a_}, {b_}) {{\n{scope}\tauto c_a_ = {a};\n\tauto c_b_ = {b};\n\treturn ::{c}_eq(&c_a_, &c_b_);\n}}\n\ninline bool operator!=({a_}, {b_}) {{\n\treturn !(a == b);\n}}\n" )); } for (on, trait_name) in [(t.display, "to_string"), (t.debug, "debug")] { if on { self.def(format!( - "inline ::{ns}::buf {name}_{trait_name}({}) {{\n\tauto c_self_ = {this};\n\treturn {d}::access::adopt(::{c}_{trait_name}(&c_self_));\n}}\n", + "inline ::{ns}::buf {name}_{trait_name}({}) {{\n{scope}\tauto c_self_ = {this};\n\treturn {d}::access::adopt(::{c}_{trait_name}(&c_self_));\n}}\n", declare(¶m, "self"), d = self.d() )); @@ -768,13 +856,15 @@ impl<'a> Header<'a> { for i in 0..api.records.len() { let c = abi.record(i); let name = self.name(&c); - self.line(format!("inline ::{c} lower(const ::{ns}::{name} &v);")); + let arena = self.arena_param(&Type::Record(i)); + self.line(format!("inline ::{c} lower(const ::{ns}::{name} &v{arena});")); self.line(format!("inline ::{ns}::{name} lift(const ::{c} &c);")); } for (i, _) in api.enums.iter().enumerate().filter(|(_, e)| !e.plain()) { let c = abi.enumeration(i); let name = self.name(&c); - self.line(format!("inline ::{c} lower(const ::{ns}::{name} &v);")); + let arena = self.arena_param(&Type::Enum(i)); + self.line(format!("inline ::{c} lower(const ::{ns}::{name} &v{arena});")); self.line(format!("inline ::{ns}::{name} lift(const ::{c} &c);")); } for i in 0..api.callbacks.len() { @@ -800,8 +890,9 @@ impl<'a> Header<'a> { true => ("", ""), false => (" &v", " &c"), }; + let arena = self.arena_param(&Type::Record(i)); self.line(format!( - "inline ::{c} lower(const ::{ns}::{name}{v}) {{\n\treturn ::{c}{{{}}};\n}}\n", + "inline ::{c} lower(const ::{ns}::{name}{v}{arena}) {{\n\treturn ::{c}{{{}}};\n}}\n", lowered.join(", ") )); self.line(format!( @@ -842,8 +933,9 @@ impl<'a> Header<'a> { fields.join(", ") )); } + let arena = self.arena_param(&Type::Enum(i)); self.line(format!( - "inline ::{c} lower(const ::{ns}::{name} &v) {{\n\t::{c} c{{}};\n\tswitch (v.index()) {{\n{}\n\t}}\n\treturn c;\n}}\n", + "inline ::{c} lower(const ::{ns}::{name} &v{arena}) {{\n\t::{c} c{{}};\n\tswitch (v.index()) {{\n{}\n\t}}\n\treturn c;\n}}\n", lower.join("\n") )); self.line(format!( @@ -927,6 +1019,8 @@ impl<'a> Header<'a> { let mut params: Vec = Vec::new(); let mut args: Vec = Vec::new(); let mut prelude = Vec::new(); + // What the call lowers, which may need an arena. + let mut lowered: Vec = function.params.iter().map(|p| p.ty.clone()).collect(); let receiver = function.receiver.filter(|_| owner.is_some()); let mut c_params = export.call.params.iter(); if receiver.is_some() { @@ -934,17 +1028,23 @@ impl<'a> Header<'a> { match (ty, free) { (Ty::Handle { .. }, _) => args.push("raw_".to_string()), (_, false) => { - prelude.push(format!("auto c_self_ = {d}::lower(*this);")); + let ty = record(abi, owner_c.as_deref().expect("a member method has its record")); + prelude.push(format!("auto c_self_ = {};", self.lower(&ty, "*this"))); args.push("&c_self_".to_string()); + lowered.push(ty); } (_, true) => { let ty = enumeration(abi, owner_c.as_deref().expect("a free method has its enum")); params.push(declare(&self.param(&ty), "self")); prelude.push(format!("auto c_self_ = {};", self.lower(&ty, "self"))); args.push("&c_self_".to_string()); + lowered.push(ty); } } } + if lowered.iter().any(|ty| self.arena(ty)) { + prelude.insert(0, format!("{d}::arena a_;")); + } let mut templates = Vec::new(); for param in &function.params { let (_, c_ty) = c_params.next().expect("each parameter has its C one"); @@ -1128,6 +1228,21 @@ fn enumeration(abi: &Abi<'_>, c: &str) -> Type { Type::Enum(i.expect("a free method's owner is an enum")) } +/// The record named `c` in C. +fn record(abi: &Abi<'_>, c: &str) -> Type { + let i = (0..abi.api.records.len()).find(|&i| abi.record(i) == c); + Type::Record(i.expect("a member method's value is a record")) +} + +/// Whether passing a `Vec` of `elem` moves each handle out, as a handle taken by value is. +fn moves(abi: &Abi<'_>, elem: &Type) -> bool { + match elem { + Type::Object(i) => !abi.api.objects[*i].copy, + Type::Option(inner) | Type::Box(inner) | Type::List(inner) => moves(abi, inner), + _ => false, + } +} + /// Whether the object named `c` in C is `Copy`. fn copy(abi: &Abi<'_>, c: &str) -> bool { (0..abi.api.objects.len()).any(|i| abi.api.objects[i].copy && abi.object(i) == c) diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 15bae186b..c82ac8968 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -54,6 +54,17 @@ 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, + }; let manifest = format!( "# Generated by rust2-ffi: the C ABI of the pub API of {packages}.\n\ [package]\n\ @@ -148,6 +159,10 @@ impl Gen<'_> { Type::Vec => "::std::vec::Vec".to_string(), Type::Bytes => "::bytes::Bytes".to_string(), Type::Duration => "::std::time::Duration".to_string(), + Type::Path => "::std::path::Path".to_string(), + Type::PathBuf => "::std::path::PathBuf".to_string(), + Type::SocketAddr => "::std::net::SocketAddr".to_string(), + Type::List(inner) => format!("::std::vec::Vec<{}>", self.rust(inner)), Type::Option(inner) => format!("Option<{}>", self.rust(inner)), Type::Ref(Mutability::Not, inner) => format!("&{}", self.rust(inner)), Type::Ref(Mutability::Mut, inner) => format!("&mut {}", self.rust(inner)), @@ -169,6 +184,7 @@ impl Gen<'_> { Ty::Char => "u32".to_string(), Ty::Buf => "rt::Buf".to_string(), Ty::Duration => "rt::Duration".to_string(), + Ty::SocketAddr => "rt::SocketAddr".to_string(), Ty::Struct(name) => name.clone(), // A C enum is an `int`. Ty::Enum(_) | Ty::Poll => "i32".to_string(), @@ -200,11 +216,16 @@ impl Gen<'_> { Type::Str | Type::String => format!("unsafe {{ {e}.str(w) }}.to_owned()"), Type::Slice | Type::Vec => format!("unsafe {{ {e}.bytes(w) }}.to_vec()"), Type::Bytes => format!("::bytes::Bytes::copy_from_slice(unsafe {{ {e}.bytes(w) }})"), - Type::Duration => format!("{e}.lift(w)"), + Type::Path | Type::PathBuf => format!("unsafe {{ {e}.path(w) }}.to_path_buf()"), + Type::Duration | Type::SocketAddr => format!("{e}.lift(w)"), Type::Waker => format!("{e}.lift()"), + Type::List(inner) => format!( + "unsafe {{ {e}.items(w) }}.iter().map(|c| {}).collect::<::std::vec::Vec<_>>()", + self.lift(inner, "(*c)") + ), Type::Option(inner) => match inner.base() { Type::Object(_) => format!("if {e}.is_null() {{ None }} else {{ Some({}) }}", self.lift(inner, e)), - base if Abi::is_buf(base) => { + base if Abi::nullable(base) => { format!("if {e}.is_none() {{ None }} else {{ Some({}) }}", self.lift(inner, e)) } _ => format!( @@ -290,11 +311,19 @@ impl Gen<'_> { Type::Slice => format!("rt::Buf::vec({e}.to_vec())"), Type::Vec => format!("rt::Buf::vec({e})"), Type::Bytes => format!("rt::Buf::owned({e})"), + Type::Path => format!("rt::Buf::path_buf({e}.to_path_buf())"), + Type::PathBuf => format!("rt::Buf::path_buf({e})"), Type::Duration => format!("rt::Duration::lower({e})"), + Type::SocketAddr => format!("rt::SocketAddr::lower({e})"), + Type::List(inner) => format!( + "rt::List::owned(::std::iter::IntoIterator::into_iter({e}).map(|v| {}).collect())", + self.lower(inner, "v") + ), Type::Option(inner) => { let none = match inner.base() { Type::Object(_) => "::std::ptr::null()".to_string(), base if Abi::is_buf(base) => "rt::Buf::NONE".to_string(), + Type::List(_) => "rt::List::NONE".to_string(), _ => { let name = self.abi.option(inner); return format!( @@ -307,7 +336,7 @@ impl Gen<'_> { } Type::Box(inner) => self.lower(inner, &format!("*{e}")), Type::Ref(_, inner) => match &**inner { - Type::Str | Type::Slice => self.lower(inner, e), + Type::Str | Type::Slice | Type::Path => self.lower(inner, e), // A handle in the clone is a new handle. inner => self.lower(inner, &format!("::std::clone::Clone::clone({e})")), }, @@ -327,10 +356,17 @@ impl Gen<'_> { fn free(&self, ty: &Type, e: &str) -> Option { match ty { _ if Abi::is_buf(ty) => Some(format!("unsafe {{ {e}.free() }};")), + Type::List(inner) => { + let each = self.free(inner, "c").unwrap_or_default(); + Some(format!( + "unsafe {{ {e}.free(\"{}_free\", |c| {{ let _ = c; {each} }}) }};", + self.abi.list(inner) + )) + } Type::Option(inner) => match inner.base() { base if Abi::is_buf(base) => Some(format!("unsafe {{ {e}.free() }};")), - // A null handle frees nothing. - Type::Object(_) => self.free(inner, e), + // A null handle or array frees nothing. + Type::Object(_) | Type::List(_) => self.free(inner, e), _ => self .free(inner, &format!("{e}.value")) .map(|free| format!("if {e}.some {{ {free} }}")), @@ -350,7 +386,7 @@ impl Gen<'_> { fn owns(&self, ty: &Type) -> bool { match ty { _ if Abi::is_buf(ty) => true, - Type::Object(_) => true, + Type::Object(_) | Type::List(_) => true, Type::Option(inner) | Type::Box(inner) => self.owns(inner), Type::Record(i) => self.abi.api.records[*i].fields.iter().any(|f| self.owns(&f.ty)), Type::Enum(i) => self.abi.api.enums[*i] @@ -369,6 +405,7 @@ impl Gen<'_> { )); self.line("#![allow(non_camel_case_types, reason = \"the C names\")]"); self.line("#![allow(unused_unsafe, reason = \"every conversion is in its own block\")]"); + self.line("#![allow(unused_parens, reason = \"each element is lifted from `(*c)`\")]"); self.line("#![allow(clippy::all, reason = \"generated\")]"); self.line("#![allow(dead_code, reason = \"the runtime serves every API\")]\n"); self.line("mod rt;\n"); @@ -398,6 +435,16 @@ impl Gen<'_> { }; self.line(format!("pub type {name} = rt::{kind}<{}>;\n", int.rust())); } + for (name, elem) in &self.abi.lists.clone() { + let c = self.rs(&self.abi.ty(elem)); + self.line(format!("pub type {name} = rt::List<{c}>;\n")); + let free = self + .free(&Type::List(Box::new(elem.clone())), "this") + .expect("an array is freed"); + self.line(format!( + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: {name}) {{\n\t{free}\n}}\n" + )); + } for (name, inner) in &self.abi.options.clone() { let value = self.rs(&self.abi.ty(inner)); self.line(format!( @@ -781,10 +828,12 @@ impl Gen<'_> { match ty { Type::Ref(_, inner) => match &**inner { Type::Str => format!("rt::Buf::view({e}.as_bytes())"), + Type::Path => format!("rt::Buf::view({e}.as_os_str().as_encoded_bytes())"), _ => format!("rt::Buf::view({e})"), }, Type::String => format!("rt::Buf::view({e}.as_bytes())"), Type::Vec | Type::Bytes => format!("rt::Buf::view(&{e}[..])"), + Type::PathBuf => format!("rt::Buf::view({e}.as_os_str().as_encoded_bytes())"), Type::Option(inner) => match &**inner { inner if Abi::is_buf(inner) => format!( "match &{e} {{ Some(v) => {}, None => rt::Buf::NONE }}", @@ -839,6 +888,9 @@ impl Gen<'_> { (Type::Slice, Mutability::Not, false) => { (format!("let {local} = unsafe {{ {c}.bytes(w) }};"), local.to_string()) } + (Type::Path, Mutability::Not, false) => { + (format!("let {local} = unsafe {{ {c}.path(w) }};"), local.to_string()) + } (inner, m, _) => { let (binding, pass) = match m { Mutability::Mut => ("let mut", format!("&mut {local}")), @@ -962,6 +1014,11 @@ impl Gen<'_> { Access::Call => format!("{callee}({})", args.join(", ")), Access::Field { .. } => format!("&({}).{}", args[0], function.name()), Access::Const => callee, + Access::Parse => format!( + "<{} as ::std::str::FromStr>::from_str({}).map_err(|e| ::std::string::ToString::to_string(&e))", + self.rust(&owner.as_ref().expect("a parse has its type").1), + 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 1917da348..502186d24 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -184,6 +184,9 @@ pub enum Access { Field { shared: bool }, /// It reads a constant that is not a `bool` or integer. Const, + /// 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, } impl Function { @@ -241,6 +244,14 @@ pub enum Type { Bytes, /// `std::time::Duration` Duration, + /// `std::path::Path`, only behind a reference. + Path, + /// `std::path::PathBuf` + PathBuf, + /// `std::net::SocketAddr` + SocketAddr, + /// `Vec` of any `T` but `u8`, which is [`Type::Vec`]. + List(Box), /// `std::task::Waker`, only as a parameter taken by value. Waker, Option(Box), diff --git a/ffi/tests/api.c b/ffi/tests/api.c index ddaf4f863..ec256e353 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -85,6 +85,31 @@ static bool move_lent(void *data, api_counter *counter) { return false; } +/* Bytes as Rust's `escape_ascii` prints those of a path. */ +static void print_escaped(const char *before, api_buf buf, const char *after) { + printf("%s", before); + for (size_t i = 0; i < buf.len; i++) { + uint8_t b = buf.ptr[i]; + if (b >= 0x20 && b < 0x7f && b != '\\' && b != '\'' && b != '"') { + putchar(b); + } else { + printf("\\x%02x", b); + } + } + printf("%s", after); + api_buf_free(buf); +} + +/* A socket address, as Rust's Display prints the ones the script makes. */ +static void print_addr(api_socket_addr addr) { + if (!addr.v6) { + printf("%u.%u.%u.%u:%u", addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3], addr.port); + return; + } + /* Only the loopback, `::1`. */ + printf("[::%u%%%u]:%u", addr.ip[15], addr.scope_id, addr.port); +} + /* A bound, as Rust's Debug prints it. */ static void print_bound(api_bound_u64 bound) { switch (bound.kind) { @@ -435,6 +460,127 @@ static int script(void) { api_slot_u8 *slot = api_slot_u8_new(200); printf("slot %u\n", api_slot_u8_value(slot)); api_slot_u8_free(slot); + /* Foreign types: a URL parsed from text, socket addresses, OS paths, and `Vec`s. */ + api_url *base; + api_buf parse_err; + if (!api_url_parse(str("https://example.com/a/b"), &base, &parse_err)) { + return 1; + } + print("url ", api_url_to_string(base), " host "); + api_buf host = api_host(base); + if (host.ptr != NULL) { + print("Some(\"", host, "\")\n"); + } else { + printf("None\n"); + } + api_url *resolved; + api_buf resolve_err; + api_url *base_clone = api_url_clone(base); + if (api_resolve(base_clone, str("c?d"), &resolved, &resolve_err)) { + print("resolve ", api_url_to_string(resolved), " "); + printf("%s\n", api_url_eq(resolved, base) ? "true" : "false"); + api_url_free(resolved); + } else { + print("resolve err ", resolve_err, "\n"); + } + api_url_free(base_clone); + api_url_free(base); + api_url *bad; + if (api_url_parse(str("no scheme"), &bad, &parse_err)) { + print("parse ", api_url_to_string(bad), "\n"); + api_url_free(bad); + } else { + print("parse err ", parse_err, "\n"); + } + + api_socket_addr addrs[2] = { + {.ip = {127, 0, 0, 1}, .port = 8080}, + {.ip = {[15] = 1}, .port = 443, .v6 = true, .scope_id = 3}, + }; + api_vec_u16 ports = api_ports((api_vec_socket_addr){.ptr = addrs, .len = 2}); + printf("ports [%u, %u]\n", ports.ptr[0], ports.ptr[1]); + api_vec_u16_free(ports); + api_buf peer_names[] = {str("a"), str("b")}; + api_peer peer_start = {.addr = addrs[1], .names = {.ptr = peer_names, .len = 2}}; + api_peer peer = api_peer_next(&peer_start, str("c")); + printf("peer Peer { addr: "); + print_addr(peer.addr); + printf(", names: ["); + for (size_t i = 0; i < peer.names.len; i++) { + printf("%s\"%.*s\"", i == 0 ? "" : ", ", (int)peer.names.ptr[i].len, (const char *)peer.names.ptr[i].ptr); + } + printf("] } ["); + api_vec_string aliases = api_peer_aliases(&peer); + for (size_t i = 0; i < aliases.len; i++) { + printf("%s\"%.*s\"", i == 0 ? "" : ", ", (int)aliases.ptr[i].len, (const char *)aliases.ptr[i].ptr); + } + printf("]\n"); + api_vec_string_free(aliases); + api_buf debug = api_peer_debug(&peer); + api_peer_free(&peer); + api_buf_free(debug); + + /* A path's bytes as the OS holds them, which need not be UTF-8. */ + api_buf dir = {.ptr = (const uint8_t *)"\xff" "dir", .len = 4}; + api_buf file = api_file(dir, str("f")); + api_buf paths[] = {file, str("/")}; + api_vec_option_path parents = api_parents((api_vec_path){.ptr = paths, .len = 2}); + print_escaped("file ", file, "\n"); + printf("parents"); + for (size_t i = 0; i < parents.len; i++) { + if (parents.ptr[i].ptr != NULL) { + print_escaped(" ", parents.ptr[i], ""); + } else { + printf(" none"); + } + } + printf("\n"); + /* Each element taken: the array alone is freed. */ + parents.len = 0; + api_vec_option_path_free(parents); + + api_vec_string words = api_words(str(" one two three ")); + printf("words %zu [", words.len); + for (size_t i = 0; i < words.len; i++) { + printf("%s\"%.*s\"", i == 0 ? "" : ", ", (int)words.ptr[i].len, (const char *)words.ptr[i].ptr); + } + printf("]\n"); + print("sentence ", api_sentence(words), " "); + print("", api_sentence((api_vec_string){0}), "\n"); + api_vec_string_free(words); + /* An empty array that is not None: its pointer is not null. */ + api_buf no_words[1]; + print("sentence ", api_sentence((api_vec_string){.ptr = no_words, .len = 0}), "|\n"); + + api_vec_bytes chunks = api_chunks(str("abcde"), 2); + printf("chunks ["); + for (size_t i = 0; i < chunks.len; i++) { + printf("%s[", i == 0 ? "" : ", "); + for (size_t j = 0; j < chunks.ptr[i].len; j++) { + printf("%s%u", j == 0 ? "" : ", ", chunks.ptr[i].ptr[j]); + } + printf("]"); + } + printf("]\n"); + api_vec_bytes_free(chunks); + + api_buf counter_names[] = {str("x"), str("y")}; + api_vec_counter counters = api_counters((api_vec_string){.ptr = counter_names, .len = 2}); + printf("counters"); + for (size_t i = 0; i < counters.len; i++) { + print(i == 0 ? " " : ", ", api_counter_name(counters.ptr[i]), " "); + printf("%llu", (unsigned long long)api_counter_get(counters.ptr[i])); + } + printf("\n"); + api_vec_counter_free(counters); + + uint32_t row0[] = {1, 2}; + uint32_t row2[] = {3}; + api_vec_u32 rows[] = {{.ptr = row0, .len = 2}, {.ptr = row0, .len = 0}, {.ptr = row2, .len = 1}}; + 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); + /* Each future dropped its waker once it finished, and Rust the one it was given. */ return drops == 5 ? 0 : 1; } @@ -473,6 +619,13 @@ static int broken(const char *which) { api_registry_edit(registry, NULL, NULL); } else if (strcmp(which, "bound") == 0) { api_count((api_bounds_u64){.start = {.kind = (api_bound)3}}); + } else if (strcmp(which, "socket") == 0) { + api_socket_addr addr = {.ip = {127, 0, 0, 1, 1}}; + api_ports((api_vec_socket_addr){.ptr = &addr, .len = 1}); + } else if (strcmp(which, "array") == 0) { + api_vec_u16 ports = api_ports((api_vec_socket_addr){0}); + ports.len = 1; + api_vec_u16_free(ports); } return 0; } diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 60cd1072a..52afe0c59 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "api.hpp" @@ -21,6 +22,28 @@ void print(const char *before, const api::buf &buf, const char *after) { std::printf("%s%.*s%s", before, static_cast(buf.size()), reinterpret_cast(buf.data()), after); } +// Bytes as Rust's `escape_ascii` prints those of a path. +void print_escaped(const char *before, const api::buf &buf, const char *after) { + std::printf("%s", before); + for (std::uint8_t b : buf) { + if (b >= 0x20 && b < 0x7f && b != '\\' && b != '\'' && b != '"') { + std::putchar(b); + } else { + std::printf("\\x%02x", b); + } + } + std::printf("%s", after); +} + +// Strings as Rust's Debug prints a list of them. +void print_strings(const std::vector &strings) { + std::printf("["); + for (std::size_t i = 0; i < strings.size(); i++) { + print(i == 0 ? "\"" : ", \"", strings[i], "\""); + } + std::printf("]"); +} + int wakes = 0; // The Log callback: keeps even levels. @@ -365,6 +388,91 @@ int script() { std::optional limit = retry.limit_setting(); std::printf("retry %d %u %u %u\n", limit.has_value(), limit.value_or(0), retry.limit(), retry.delay()); std::printf("slot %u\n", api::slot_u8::new_(200).value()); + + // Foreign types: a URL parsed from text, socket addresses, OS paths, and `Vec`s. + api::url base = api::url::parse("https://example.com/a/b").value(); + print("url ", base.to_string(), " host "); + if (auto host = api::host(base)) { + print("Some(\"", *host, "\")\n"); + } else { + std::printf("None\n"); + } + if (auto resolved = api::resolve(base, "c?d")) { + print("resolve ", resolved->to_string(), " "); + std::printf("%s\n", *resolved == base ? "true" : "false"); + } else { + print("resolve err ", resolved.error(), "\n"); + } + if (auto parsed = api::url::parse("no scheme")) { + print("parse ", parsed->to_string(), "\n"); + } else { + print("parse err ", parsed.error(), "\n"); + } + + api::socket_addr v4{}; + v4.ip[0] = 127; + v4.ip[3] = 1; + v4.port = 8080; + api::socket_addr v6{}; + v6.ip[15] = 1; + v6.port = 443; + v6.v6 = true; + v6.scope_id = 3; + auto ports = api::ports({v4, v6}); + std::printf("ports [%u, %u]\n", ports[0], ports[1]); + std::vector names; + names.emplace_back(api::view("a")); + names.emplace_back(api::view("b")); + api::peer peer = api::peer{v6, names}.next("c"); + std::printf("peer Peer { addr: [::%u%%%u]:%u, names: ", peer.addr.ip[15], peer.addr.scope_id, peer.addr.port); + print_strings(peer.names); + std::printf(" } "); + print_strings(peer.aliases()); + std::printf("\n"); + + // A path's bytes as the OS holds them, which need not be UTF-8. + api::buf file = api::file(api::view("\xff" "dir"), "f"); + auto parents = api::parents({file, "/"}); + print_escaped("file ", file, "\n"); + std::printf("parents"); + for (const auto &parent : parents) { + if (parent) { + print_escaped(" ", *parent, ""); + } else { + std::printf(" none"); + } + } + std::printf("\n"); + + auto words = api::words(" one two three "); + std::printf("words %zu ", words.size()); + print_strings(words); + std::printf("\n"); + print("sentence ", api::sentence(std::vector(words.begin(), words.end())), " "); + print("", api::sentence(std::nullopt), "\n"); + print("sentence ", api::sentence(std::vector()), "|\n"); + + std::printf("chunks ["); + auto chunks = api::chunks("abcde", 2); + for (std::size_t i = 0; i < chunks.size(); i++) { + std::printf("%s[", i == 0 ? "" : ", "); + for (std::size_t j = 0; j < chunks[i].size(); j++) { + std::printf("%s%u", j == 0 ? "" : ", ", chunks[i].data()[j]); + } + std::printf("]"); + } + std::printf("]\n"); + + auto counters = api::counters({"x", "y"}); + std::printf("counters"); + for (std::size_t i = 0; i < counters.size(); i++) { + print(i == 0 ? " " : ", ", counters[i].name(), " "); + std::printf("%llu", static_cast(counters[i].get())); + } + std::printf("\n"); + + auto sums = api::sums({{1, 2}, {}, {3}}); + std::printf("sums [%u, %u, %u]\n", sums[0], sums[1], sums[2]); return 0; } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index fd59b4440..d047b60c4 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -176,6 +176,8 @@ fn api() { ), ("enum", "api_greet: an enum value is invalid"), ("bound", "api_count: a bound's kind is invalid"), + ("socket", "api_ports: an IPv4 socket address has IPv6 bytes or fields"), + ("array", "api_vec_u16_free: an array is not as Rust returned it"), ("lent", "api_registry_edit: a lent handle was freed"), ( "lent moved", diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index b56623757..8538edd03 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -50,7 +50,6 @@ Every generator on the C ABI: - [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` - [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` - [Setters for an object's pub fields](/quest/m1/ffi-export/field-setters.md) - C fills in a session's config, as Rust assigns it -- [Session config's foreign types](/quest/m1/ffi-export/foreign-types.md) - `Url`, `SocketAddr`, `PathBuf`, and `Vec` cross the C ABI - [Sessions on moq-tokio](/quest/m1/ffi-export/session-tokio.md) - bindings connect and accept with one call, the library running moq-tokio inside - [Sessions on sans-IO QUIC](/quest/m1/ffi-export/session-quic.md) - bindings move UDP datagrams and drive the session's poll themselves - [C++ bindings on MSVC and Clang](/quest/m1/ffi-export/cpp.md) - the C++ header built and tested beyond GCC on Linux diff --git a/quest/m1/ffi-export/foreign-types.md b/quest/m1/ffi-export/foreign-types.md deleted file mode 100644 index 75c67e493..000000000 --- a/quest/m1/ffi-export/foreign-types.md +++ /dev/null @@ -1,23 +0,0 @@ -# [M] Session config's foreign types - -## Goal - -The exporter maps the types moq-tokio's session config and calls take that it refuses -today: `url::Url` (`connect::Config::url`, `Client::connect`), `std::net::SocketAddr` -(`connect::Config::bind`, `Server::local_addr`), `PathBuf` (TLS files), and `Vec` -(`tls.fingerprint`, `tls.generate`). Proven from C in `moq/moq.c`, including a test that -binds port 0 and reads the address back. - -## Plan - -Found from C on 2026-10-27 in [#161](https://github.com/kixelated/rust2/pull/161). - -- These are general rules, not moq's: each maps wherever it appears, with no config. -- `Url` and `SocketAddr` could cross as text that Rust parses, failing loud with the - parse error, or as a type of their own; prefer what every generator can read. -- `PathBuf` holds OS bytes, not UTF-8; don't lose the ones a `String` can't hold. -- `Vec` may be the first `Vec` of a non-byte type; share its shape with the - `Vec` that [instance keys](/quest/m1/ffi-export/instance-keys.md) need. -- Report the census before and after. - -Public API: the C names the exporter emits. diff --git a/quest/m1/ffi-export/session-tokio.md b/quest/m1/ffi-export/session-tokio.md index 0295ad95d..757e15804 100644 --- a/quest/m1/ffi-export/session-tokio.md +++ b/quest/m1/ffi-export/session-tokio.md @@ -4,7 +4,8 @@ A binding connects and accepts moq sessions with one call, as moq-ffi does today: the exported library runs moq-tokio's sockets and runtime inside, and the binding never drives -a transport. Proven from C in `moq/moq.c` against a local relay or server. +a transport. Proven from C in `moq/moq.c` against a local relay or server, including a +server that binds port 0 and reads its address back. ## Plan @@ -35,5 +36,4 @@ Public API: the C names moq's bindings get. ## Required - [Setters for an object's pub fields](/quest/m1/ffi-export/field-setters.md) - C fills in the session config -- [Session config's foreign types](/quest/m1/ffi-export/foreign-types.md) - `Url`, `SocketAddr`, `PathBuf`, and `Vec` cross the C ABI From 30642d5c5cf92dae36e85cd7f063d7ecd321a060 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 14:53:43 -0700 Subject: [PATCH 3/9] chore(ffi): keep moq's excludes sorted Co-Authored-By: Claude Opus 5.5 --- ffi/moq/ffi.toml | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ffi/moq/ffi.toml b/ffi/moq/ffi.toml index 063b55194..cab4029f7 100644 --- a/ffi/moq/ffi.toml +++ b/ffi/moq/ffi.toml @@ -115,10 +115,8 @@ exclude = [ [crates.moq-tokio] exclude = [ - "Client::connect", - "tcp::Error", - "websocket::Error", "Addrs", + "Client::connect", "Log::level", "Log::new", "RedactedUrl", @@ -216,6 +214,7 @@ exclude = [ "secret::Secret::as_bytes", "server::Config::websocket", "server::Request::within", + "tcp::Error", "tcp::Listener", "tls::Connect::build", "tls::Identity::generate", @@ -233,6 +232,7 @@ exclude = [ "unix::Listener::accept", "unix::Listener::with_protocols", "watch::Files", + "websocket::Error", "websocket::Listener", "websocket::Options", "websocket::accept", @@ -259,12 +259,6 @@ exclude = [ [crates.hang] exclude = [ - "health::PublisherThresholds::skipped", - "health::TransportThresholds::headroom", - "health::TransportThresholds::loss", - "health::ViewerThresholds::errors", - "health::ViewerThresholds::late", - "health::ViewerThresholds::stalled", "Catalog::from_reader", "catalog::Archive::timelines", "catalog::Audio::enabled", @@ -286,9 +280,15 @@ exclude = [ "echo::Snapshot", "health::Health", "health::Limits", + "health::PublisherThresholds::skipped", "health::Report", "health::Sample", + "health::TransportThresholds::headroom", + "health::TransportThresholds::loss", "health::TransportThresholds::rtt", + "health::ViewerThresholds::errors", + "health::ViewerThresholds::late", + "health::ViewerThresholds::stalled", "health::publisher", "health::viewer", "stats::Snapshot", @@ -309,15 +309,6 @@ exclude = [ [crates.moq-mux] exclude = [ - "catalog::Producer::audio", - "catalog::Producer::text", - "catalog::Producer::video", - "catalog::Reserved::audio", - "catalog::Reserved::text", - "catalog::Reserved::video", - "container::ts::Export", - "timeline::Entry", - "timeline::Producer::push", "Linger", "Source::bind", "Source::follow", @@ -332,13 +323,19 @@ exclude = [ "catalog::Entry", "catalog::Guard", "catalog::IntoRendition", + "catalog::Producer::audio", "catalog::Producer::json_snapshot", "catalog::Producer::json_stream", "catalog::Producer::modify", "catalog::Producer::stats", + "catalog::Producer::text", "catalog::Producer::track", + "catalog::Producer::video", "catalog::RenditionConfig", + "catalog::Reserved::audio", + "catalog::Reserved::text", "catalog::Reserved::track", + "catalog::Reserved::video", "catalog::Select", "catalog::Stream", "catalog::hang::CatalogExt", @@ -375,6 +372,7 @@ exclude = [ "container::mkv::Error", "container::mkv::Export", "container::ts::Catalog", + "container::ts::Export", "container::ts::Follower", "container::ts::Import", "container::ts::Mpegts::si", @@ -389,6 +387,8 @@ exclude = [ "json::Consumer", "json::Snapshot", "json::Stream", + "timeline::Entry", + "timeline::Producer::push", ] [crates.moq-mux.rename] From e9e929d5bb7d9a1174d4ae7ebf78e3d3551c6b95 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:01:59 -0700 Subject: [PATCH 4/9] fix(ffi): keep Url out of values, free returned Vec<&T> clones A `Url` field makes its type an object, as any handle C cannot copy does, so reading the value no longer consumes it. A returned `Vec<&T>`'s free drops the clones it holds. A parameter's `Vec` refuses a reference nested in its elements, and C++ takes an optional `Vec` of handles by value. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 2 ++ ffi/fixtures/api/src/lib.rs | 18 +++++++++++++++++ ffi/fixtures/reject/expected.txt | 1 + ffi/fixtures/reject/src/lib.rs | 5 +++++ ffi/src/abi.rs | 23 ++++++++++++--------- ffi/src/collect.rs | 22 ++++++++++++++++---- ffi/src/cpp.rs | 4 ++++ ffi/tests/api.c | 27 +++++++++++++++++++++++++ ffi/tests/api.cpp | 17 ++++++++++++++++ 9 files changed, 105 insertions(+), 14 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 6a02e57c3..43d0f7d02 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -343,4 +343,6 @@ fn main() { let counters: Vec = counters.iter().map(|c| format!("{} {}", c.name(), c.get())).collect(); println!("counters {}", counters.join(", ")); println!("sums {:?}", api_lib::sums(vec![vec![1, 2], Vec::new(), vec![3]])); + 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 ebd25c927..871bf109d 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -505,6 +505,7 @@ static DROPPED: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new /// A token: every field is `pub`, but its `Drop` must run when C frees it, so it is an /// object rather than a record. +#[derive(Clone)] pub struct Token { pub id: u32, } @@ -513,6 +514,11 @@ impl Token { pub fn new(id: u32) -> Self { Self { id } } + + /// The token twice, borrowed: C gets a clone of each, which the array frees. + pub fn twice(&self) -> Vec<&Token> { + vec![self, self] + } } impl Drop for Token { @@ -909,3 +915,15 @@ pub fn counters(names: Vec) -> Vec { pub fn sums(rows: Vec>) -> Vec { rows.iter().map(|row| row.iter().sum()).collect() } + +/// 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 { + pub home: url::Url, +} + +impl Site { + pub fn new(home: url::Url) -> Self { + Self { home } + } +} diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index 4db94fc8c..e2321df33 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -26,6 +26,7 @@ fixtures/reject/src/lib.rs:217:1: rust2-ffi: cannot export fn `twice`: closure b fixtures/reject/src/lib.rs:223:1: rust2-ffi: cannot export fn `both`: closure type taken twice `F`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:229:1: rust2-ffi: cannot export fn `read`: lent object that borrows `&Text<'_>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:234:1: rust2-ffi: cannot export fn `count_words`: Vec of references `&str`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:239:1: rust2-ffi: cannot export fn `count_some`: Vec of references `std::option::Option<&str>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:26:1: rust2-ffi: cannot export fn `borrowing`: borrowing future from a function that is not async; exclude it in the config to leave it out fixtures/reject/src/lib.rs:31:1: rust2-ffi: cannot export fn `nested`: nested Option `std::option::Option>>`; 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 diff --git a/ffi/fixtures/reject/src/lib.rs b/ffi/fixtures/reject/src/lib.rs index bc420c639..00ad4a587 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -234,3 +234,8 @@ pub fn read(f: impl FnOnce(&Text<'_>)) { pub fn count_words(words: Vec<&str>) -> usize { words.len() } + +/// Words C would lend inside an `Option` inside an array. +pub fn count_some(words: Vec>) -> usize { + words.iter().flatten().count() +} diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 6c3a2fa44..f70fba1a6 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -73,6 +73,17 @@ pub struct Export { pub future: Option<(String, Sig, Sig)>, } +/// `ty` as a clone of each borrow it holds. +fn owned(ty: &Type) -> Type { + match ty { + Type::Ref(_, inner) => owned(inner), + Type::Option(inner) => Type::Option(Box::new(owned(inner))), + Type::Box(inner) => Type::Box(Box::new(owned(inner))), + Type::List(inner) => Type::List(Box::new(owned(inner))), + ty => ty.clone(), + } +} + /// The suffix naming each instance of a generic item, by its arguments. type Suffixes = HashMap, String>; @@ -390,15 +401,6 @@ impl<'a> Abi<'a> { /// The C type of `ty` returned: a borrow is returned as a clone, so a handle in it is /// the caller's own. fn returned(&self, ty: &Type) -> Ty { - fn owned(ty: &Type) -> Type { - match ty { - Type::Ref(_, inner) => owned(inner), - Type::Option(inner) => Type::Option(Box::new(owned(inner))), - Type::Box(inner) => Type::Box(Box::new(owned(inner))), - Type::List(inner) => Type::List(Box::new(owned(inner))), - ty => ty.clone(), - } - } self.ty(&owned(ty)) } @@ -641,7 +643,8 @@ impl<'a> Abi<'a> { stack.push(inner); } Type::List(inner) => { - lists.insert(self.list(inner), (**inner).clone()); + // A returned `Vec<&T>` holds clones, which its free frees. + lists.insert(self.list(inner), owned(inner)); stack.push(inner); } Type::Ref(_, inner) | Type::Box(inner) => stack.push(inner), diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 182881169..d06112930 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -2179,8 +2179,9 @@ impl<'tcx> Collector<'tcx> { let tcx = self.tcx; ty.walk().filter_map(|arg| arg.as_type()).any(|ty| match ty.kind() { ty::Adt(adt, args) => { - matches!(self.decl(adt.did(), args), Some(Decl::Object(_))) - && !implements(tcx, ty, adt.did(), sym::Copy) + let object = matches!(self.decl(adt.did(), args), Some(Decl::Object(_))) + || self.url.is_some_and(|(url, _)| url == adt.did()); + object && !implements(tcx, ty, adt.did(), sym::Copy) } _ => false, }) @@ -2352,7 +2353,7 @@ impl<'tcx> Collector<'tcx> { let inner = self.ty(elem, pos.inner())?; // C lends nothing inside an array, and an element lowered back into // one is a clone of what Rust lent. - if matches!(inner, Type::Ref(..)) && !matches!(pos, Pos::Return | Pos::Returned) { + if borrows(&inner) && !matches!(pos, Pos::Return | Pos::Returned) { return Err(why("Vec of references", elem.to_string())); } Type::List(Box::new(inner)) @@ -2368,7 +2369,11 @@ impl<'tcx> Collector<'tcx> { } else if let Some((url, i)) = self.url && did == url { - Type::Object(i) + // A value holds only values, and handles it can copy. + match pos { + Pos::Field => return Err(why("object in a value", "url::Url")), + _ => Type::Object(i), + } } else if tcx.is_diagnostic_item(rustc_span::Symbol::intern("Waker"), did) { match pos { Pos::Param => Type::Waker, @@ -2543,6 +2548,15 @@ fn names(ty: &Type, i: usize) -> bool { } } +/// Whether `ty` is or holds a reference. +fn borrows(ty: &Type) -> bool { + match ty { + Type::Ref(..) => true, + Type::Option(inner) | Type::Box(inner) | Type::List(inner) => borrows(inner), + _ => false, + } +} + /// Whether `ty` holds a reference inside it, such as `Option<&T>`. fn lends_inside(ty: &Type) -> bool { match ty { diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 60d76031d..c565b16f3 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -123,6 +123,10 @@ impl<'a> Header<'a> { } match ty { Type::Option(inner) if Abi::is_buf(inner) => format!("::std::optional<::{}::view>", self.ns), + // Moved out of, each handle as a handle taken by value is. + Type::Option(inner) if matches!(inner.base(), Type::List(elem) if moves(abi, elem)) => { + format!("::std::optional<{}>", self.items(inner.base())) + } Type::Option(inner) if matches!(inner.base(), Type::List(_)) => { format!("const ::std::optional<{}> &", self.items(inner.base())) } diff --git a/ffi/tests/api.c b/ffi/tests/api.c index ec256e353..116d0afa1 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -581,6 +581,33 @@ static int script(void) { printf("sums [%u, %u, %u]\n", sums.ptr[0], sums.ptr[1], sums.ptr[2]); api_vec_u32_free(sums); + api_url *home; + if (!api_url_parse(str("https://example.com/a/b"), &home, &parse_err)) { + return 1; + } + api_site *site = api_site_new(home); + api_url *home0 = api_site_home(site); + api_url *home1 = api_site_home(site); + print("site ", api_url_to_string(home0), " "); + print("", api_url_to_string(home1), "\n"); + api_url_free(home0); + api_url_free(home1); + api_site_free(site); + api_url_free(home); + + /* A returned `Vec<&Token>` holds clones, which its free drops. */ + api_token *twice = api_token_new(9); + uint32_t before = api_dropped(); + api_vec_token tokens = api_token_twice(twice); + if (tokens.len != 2 || api_token_id(tokens.ptr[1]) != 9) { + return 1; + } + api_vec_token_free(tokens); + if (api_dropped() != before + 2) { + return 1; + } + api_token_free(twice); + /* Each future dropped its waker once it finished, and Rust the one it was given. */ return drops == 5 ? 0 : 1; } diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 52afe0c59..4cfb0b4a7 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -473,6 +473,23 @@ int script() { auto sums = api::sums({{1, 2}, {}, {3}}); std::printf("sums [%u, %u, %u]\n", sums[0], sums[1], sums[2]); + + api::site site = api::site::new_(base); + print("site ", site.home().to_string(), " "); + print("", site.home().to_string(), "\n"); + + // A returned `Vec<&Token>` holds clones, which the vector drops. + api::token twice = api::token::new_(9); + std::uint32_t before = api::dropped(); + { + auto tokens = twice.twice(); + if (tokens.size() != 2 || tokens[1].id() != 9) { + return 1; + } + } + if (api::dropped() != before + 2) { + return 1; + } return 0; } From 58d657d347d076fbc76f93c7d2968aa351a8e61c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:47:04 -0700 Subject: [PATCH 5/9] feat(ffi): carry Url, SocketAddr, paths, and Vec through the Kotlin export A path is a String, which throws for one that is not UTF-8; a SocketAddr is an InetSocketAddress, which throws for an IPv6 flow label Java does not keep; a Vec is a List, whose objects close once a call moves them in. The JNI reader keeps each array it reads until the call returns. The api transcript uses a UTF-8 path, which Kotlin can express, with the non-UTF-8 round trip checked from C and Kotlin's refusal of one as a broken case; moq.kt mirrors moq.c's foreign-type checks. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 4 ++ ffi/fixtures/api/examples/transcript.rs | 10 ++- ffi/fixtures/api/src/lib.rs | 12 ++++ ffi/moq/moq.kt | 33 +++++++++ ffi/runtime/jni.rs | 74 ++++++++++++++++++-- ffi/runtime/rt.kt | 33 +++++++++ ffi/src/abi.rs | 16 ++++- ffi/src/kotlin.rs | 90 +++++++++++++++++++++---- ffi/tests/api.c | 13 +++- ffi/tests/api.cpp | 4 +- ffi/tests/api.kt | 33 +++++++++ ffi/tests/fixtures.rs | 1 + 12 files changed, 293 insertions(+), 30 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 2961a3290..6e6714b10 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -226,6 +226,10 @@ kotlinx-coroutines. Each value crosses JNI as a scalar, a handle as a `Long`, by - **Bytes** are `ByteArray`, strings `String`, `Option` `T?`, a tuple `Pair`, `Triple`, or past them a generated `Tuple`, `Duration` `kotlin.time.Duration`, and an `Err` a thrown `Exception` holding it, whose message is its `Display`. +- A **path** is a `String`: one Rust returns that is not UTF-8 throws. A `SocketAddr` is a + `java.net.InetSocketAddress`, which must be resolved; one Rust returns with an IPv6 flow + label, which Java does not keep, throws. A `Vec` is a `List`, and passing it by + value closes each object in it once the call returns. - **Traits** are interfaces; once Rust drops an implementation, it is closed if `AutoCloseable`. - **Closures** lent for a call are function types, which Rust calls only during the call, diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index d4e02b2c0..54e5a0ca4 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -324,10 +324,7 @@ fn main() { } .next("c"); println!("peer {peer:?} {:?}", peer.aliases()); - let dir = std::path::PathBuf::from(::from_vec( - b"\xffdir".to_vec(), - )); - let file = api_lib::file(&dir, "f"); + let file = api_lib::file(std::path::Path::new("dir"), "f"); println!("file {}", file.as_os_str().as_encoded_bytes().escape_ascii()); let parents = api_lib::parents(vec![file, std::path::PathBuf::from("/")]); let parents: Vec = (parents.iter()) @@ -347,8 +344,9 @@ fn main() { println!("sentence {}|", api_lib::sentence(Some(Vec::new()))); println!("chunks {:?}", api_lib::chunks(b"abcde", 2)); let counters = api_lib::counters(vec!["x".to_string(), "y".to_string()]); - let counters: Vec = counters.iter().map(|c| format!("{} {}", c.name(), c.get())).collect(); - println!("counters {}", counters.join(", ")); + let names: Vec = counters.iter().map(|c| format!("{} {}", c.name(), c.get())).collect(); + println!("counters {}", names.join(", ")); + println!("worth {} {}", api_lib::worth(Some(counters)), api_lib::worth(None)); println!("sums {:?}", api_lib::sums(vec![vec![1, 2], Vec::new(), vec![3]])); 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 70bfe3f0a..a50880c6e 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -924,6 +924,13 @@ pub fn file(dir: &std::path::Path, name: &str) -> std::path::PathBuf { dir.join(name) } +/// A path whose bytes are not UTF-8, as a Unix path may be. +pub fn stray() -> std::path::PathBuf { + std::path::PathBuf::from(::from_vec(vec![ + 0xff, + ])) +} + /// Each path's parent, if it has one. pub fn parents(paths: Vec) -> Vec> { paths @@ -952,6 +959,11 @@ pub fn counters(names: Vec) -> Vec { names.iter().map(|name| Counter::new(name, 1)).collect() } +/// The counters' values summed, or 0 for none: each moves in. +pub fn worth(counters: Option>) -> u64 { + counters.map_or(0, |counters| counters.iter().map(Counter::get).sum()) +} + /// Each row's sum. pub fn sums(rows: Vec>) -> Vec { rows.iter().map(|row| row.iter().sum()).collect() diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 824de3eda..6a93a2e42 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -195,6 +195,39 @@ fun main() { expect(ConnectError.UNAUTHORIZED.isAuth(), "unauthorized is an auth error") expect(ConnectError.UNAUTHORIZED.toString() == "unauthorized", "a connect error prints as Rust's") + // Session config's foreign types. A URL is parsed from text, failing with why. + try { + Url.parse("relay.example").close() + expect(false, "a relative URL is an error") + } catch (e: StringException) { + expect(e.message == "relative URL without a base", "a URL's parse error is Rust's") + } + val url = Url.parse("https://relay.example:4443/demo") + val versions = Versions.default() + AuthTokens.new(emptyList()).use { tokens -> + tokens.dial(url, versions).use { dial -> + dial.url.use { dialed -> + expect(dialed == url, "a dial keeps its URL") + expect(dialed.toString() == "https://relay.example:4443/demo", "a URL prints as Rust's") + } + } + } + url.close() + // A `Vec`, as a List. + val alpns = versions.alpns() + expect(alpns.isNotEmpty() && alpns.all { it.isNotEmpty() }, "each version has an ALPN") + versions.close() + // A socket address, crossing in a listener's bind. + val loopback = ByteArray(16).also { it[15] = 1 } + val bind = ListenBind.Addr(java.net.InetSocketAddress(java.net.Inet6Address.getByAddress(null, loopback, null as java.net.NetworkInterface?), 4443)) + expect(bind.toString() == "[::1]:4443", "an IPv6 bind prints as Rust's") + val v4 = ListenBind.Addr(java.net.InetSocketAddress(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1)), 0)) + expect(v4.toString() == "127.0.0.1:0", "an IPv4 bind prints as Rust's") + // TLS files, as OS paths, and fingerprints, as strings: none by default. + TlsConnect.default().use { tls -> + expect(tls.root.isEmpty() && tls.fingerprint.isEmpty(), "a default TLS config lists none") + } + if (failures == 0) println("ok") kotlin.system.exitProcess(if (failures == 0) 0 else 1) } diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index ec6ccdcf1..0ed86b336 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -3,7 +3,8 @@ //! //! A value Kotlin passes is a JNI scalar, a handle as a `long`, its bytes as a `byte[]`, //! or anything else encoded into a `byte[]`: little-endian, field by field, as the C -//! struct holds it, with a buffer as its length (-1 for none) and its bytes. +//! struct holds it, with a buffer as its length (-1 for none) and its bytes, and an array +//! as its length (-1 for none) and its elements. use std::cell::Cell; use std::ffi::{CStr, c_void}; @@ -307,20 +308,24 @@ pub fn waker(env: *mut JNIEnv, waker: jobject) -> rt::Waker { lend(node(env, waker)) } -/// Encoded values, read in the order they were written. -pub struct Reader<'a>(&'a [u8]); +/// Encoded values, read in the order they were written, with the arrays read, which the +/// arrays' views point into until the reader drops. +pub struct Reader<'a> { + bytes: &'a [u8], + arrays: Vec>, +} impl<'a> Reader<'a> { pub fn new(bytes: &'a [u8]) -> Self { - Self(bytes) + Self { bytes, arrays: Vec::new() } } fn take(&mut self, n: usize) -> &'a [u8] { - if self.0.len() < n { + if self.bytes.len() < n { rt::fail("jni", "an encoded value is truncated"); } - let (head, rest) = self.0.split_at(n); - self.0 = rest; + let (head, rest) = self.bytes.split_at(n); + self.bytes = rest; head } @@ -456,6 +461,61 @@ impl Wire for rt::Duration { } } +impl Wire for rt::SocketAddr { + fn read(r: &mut Reader<'_>) -> Self { + Self { + ip: std::array::from_fn(|_| r.get()), + port: r.get(), + v6: r.get(), + flowinfo: r.get(), + scope_id: r.get(), + } + } + + fn write(self, w: &mut Writer) { + self.ip.into_iter().for_each(|b| w.put(b)); + w.put(self.port); + w.put(self.v6); + w.put(self.flowinfo); + w.put(self.scope_id); + } +} + +impl Wire for rt::List { + /// A view of the elements, as their length, or -1 for none, then each element, which + /// the reader keeps. + fn read(r: &mut Reader<'_>) -> Self { + let Ok(len) = usize::try_from(r.get::()) else { + return Self::NONE; + }; + let items: Box<[T]> = (0..len).map(|_| r.get()).collect(); + let list = Self { + ptr: items.as_ptr(), + len, + owner: std::ptr::null_mut(), + }; + r.arrays.push(Box::new(items)); + list + } + + fn write(self, w: &mut Writer) { + if self.is_none() { + w.put(-1i32); + return; + } + let Ok(len) = i32::try_from(self.len) else { + rt::fail("jni", "an array is too long for a List") + }; + w.put(len); + // SAFETY: an array Rust returned, whose elements move into the writer, so it is + // freed without them. + unsafe { + self.items("jni").iter().for_each(|&item| w.put(item)); + Self { len: 0, ..self }.free("jni", |_| {}); + } + } +} + impl Wire for rt::Range { fn read(r: &mut Reader<'_>) -> Self { Self { diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index d99eac5e8..666804fc4 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -59,6 +59,19 @@ class Writer { fun string(v: String?) = bytes(v?.encodeToByteArray()) + /** An address as Rust's `SocketAddr` holds it, which an unresolved one has no form of. */ + fun socketAddr(v: java.net.InetSocketAddress) { + val ip = kotlin.requireNotNull(v.address) { "an unresolved address has no Rust SocketAddr" } + room(16) + b.put(ip.address) + repeat(16 - ip.address.size) { b.put(0) } + i16(v.port.toShort()) + bool(ip is java.net.Inet6Address) + // Java keeps no flow label. + i32(0) + i32((ip as? java.net.Inet6Address)?.scopeId ?: 0) + } + fun duration(v: kotlin.time.Duration) { kotlin.require(!v.isNegative()) { "a negative duration has no Rust Duration" } v.toComponents { secs, nanos -> @@ -130,6 +143,26 @@ class Reader(bytes: ByteArray) { return v } + /** A path, which on Unix may hold bytes no String holds: those throw. */ + fun path(): String? = bytes()?.decodeToString(throwOnInvalidSequence = true) + + fun socketAddr(): java.net.InetSocketAddress { + val ip = ByteArray(16) + b.get(ip) + val port = b.getShort().toInt() and 0xffff + val v6 = bool() + val flowinfo = b.getInt() + val scope = b.getInt() + kotlin.check(flowinfo == 0) { "an IPv6 flow label has no Java form" } + // Built as Inet6Address directly, as InetAddress would read an IPv4-mapped one as IPv4. + val address = when { + !v6 -> java.net.InetAddress.getByAddress(ip.copyOf(4)) + scope == 0 -> java.net.Inet6Address.getByAddress(null, ip, null as java.net.NetworkInterface?) + else -> java.net.Inet6Address.getByAddress(null, ip, scope) + } + return java.net.InetSocketAddress(address, port) + } + fun duration(): kotlin.time.Duration { val secs = b.getLong() val nanos = b.getInt() diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index b37f4d8bb..576ab2562 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -529,8 +529,20 @@ impl<'a> Abi<'a> { // C's runtime, then what C++'s adds in the namespace its types share, and Kotlin's // in its package. for runtime in [ - "contract", "buf", "buf_free", "duration", "socket_addr", "waker", "poll", "view", "result", "task", "future", "poll_t", - "detail", "native", + "contract", + "buf", + "buf_free", + "duration", + "socket_addr", + "waker", + "poll", + "view", + "result", + "task", + "future", + "poll_t", + "detail", + "native", ] { self.claim(self.rt(runtime), "the runtime".to_string(), crate_span); } diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index afd16fd2b..e43b28c00 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -246,7 +246,10 @@ impl<'a> Gen<'a> { Type::Char => "Int".to_string(), Type::Str | Type::String => "String".to_string(), Type::Slice | Type::Vec | Type::Bytes => "ByteArray".to_string(), + Type::Path | Type::PathBuf => "String".to_string(), Type::Duration => "kotlin.time.Duration".to_string(), + Type::SocketAddr => "java.net.InetSocketAddress".to_string(), + Type::List(inner) => format!("kotlin.collections.List<{}>", self.kt(inner)), Type::Waker => "Waker".to_string(), Type::Option(inner) => format!("{}?", self.kt(inner)), Type::Box(inner) | Type::Ref(_, inner) => self.kt(inner), @@ -283,12 +286,24 @@ impl<'a> Gen<'a> { Type::F32 => format!("{w}.f32({e})"), Type::F64 => format!("{w}.f64({e})"), Type::Char => format!("{w}.i32({e})"), - Type::Str | Type::String => format!("{w}.string({e})"), + Type::Str | Type::String | Type::Path | Type::PathBuf => format!("{w}.string({e})"), Type::Slice | Type::Vec | Type::Bytes => format!("{w}.bytes({e})"), Type::Duration => format!("{w}.duration({e})"), + Type::SocketAddr => format!("{w}.socketAddr({e})"), + Type::List(inner) => { + let (l, v) = (self.fresh("l"), self.fresh("v")); + let item = self.write_value(inner, w, &v); + format!("{e}.let {{ {l} -> {w}.i32({l}.size); for ({v} in {l}) {{ {item} }} }}") + } Type::Option(inner) => match inner.base() { Type::Object(_) => format!("{w}.handle({e}?.handle)"), base if Abi::is_buf(base) => self.write_value(base, w, e), + // None is a length of -1. + Type::List(_) => { + let v = self.fresh("v"); + let some = self.write_value(inner, w, &v); + format!("{e}.let {{ {v} -> if ({v} == null) {w}.i32(-1) else {some} }}") + } _ => { let v = self.fresh("v"); let some = self.write_value(inner, w, &v); @@ -321,14 +336,28 @@ impl<'a> Gen<'a> { Type::F64 => format!("{r}.f64()"), Type::Char => format!("{r}.i32()"), Type::Str | Type::String => format!("{r}.string()!!"), + Type::Path | Type::PathBuf => format!("{r}.path()!!"), Type::Slice | Type::Vec | Type::Bytes => format!("{r}.bytes()!!"), Type::Duration => format!("{r}.duration()"), + Type::SocketAddr => format!("{r}.socketAddr()"), + Type::List(inner) => { + let item = self.read_value(inner, r); + format!("kotlin.collections.List({r}.i32()) {{ {item} }}") + } Type::Option(inner) => match inner.base() { Type::Object(i) => { let class = self.class(&abi.object(*i)); format!("{r}.i64().let {{ h -> if (h == 0L) null else {class}(h) }}") } Type::Str | Type::String => format!("{r}.string()"), + Type::Path | Type::PathBuf => format!("{r}.path()"), + Type::List(item) => { + let n = self.fresh("n"); + let item = self.read_value(item, r); + format!( + "{r}.i32().let {{ {n} -> if ({n} < 0) null else kotlin.collections.List({n}) {{ {item} }} }}" + ) + } base if Abi::is_buf(base) => format!("{r}.bytes()"), _ => format!("(if ({r}.bool()) {} else null)", self.read_value(inner, r)), }, @@ -372,11 +401,11 @@ impl<'a> Gen<'a> { _ => wrap(e), } } - Ty::Buf => match (string(ty), matches!(ty, Type::Option(_))) { - (true, true) => format!("{e}?.decodeToString()"), - (true, false) => format!("{e}!!.decodeToString()"), - (false, true) => e.to_string(), - (false, false) => format!("{e}!!"), + Ty::Buf => match (decode(ty), matches!(ty, Type::Option(_))) { + (Some(decode), true) => format!("{e}?.{decode}"), + (Some(decode), false) => format!("{e}!!.{decode}"), + (None, true) => e.to_string(), + (None, false) => format!("{e}!!"), }, _ => { let r = self.fresh("r"); @@ -459,14 +488,21 @@ impl<'a> Gen<'a> { "crate::{name} {{ vtable: &VTABLE_{name}, data: rt::global(env, {p}) }}" )); } - Ty::Duration | Ty::Struct(_) | Ty::In(_) => { + Ty::Duration | Ty::SocketAddr | Ty::Struct(_) | Ty::In(_) => { let w = self.fresh("w"); + // Taken by value, each object in a list moves out, as one passed alone does. + if let Type::List(_) | Type::Option(_) = ty + && moved(abi, ty) + { + let close = self.close(ty, e); + arg.after.push(close); + } arg.open = Some(format!("Native.Writer().use {{ {w} ->")); let value = self.write_value(ty, &w, e); arg.inside.push(value); arg.exprs.push(format!("{w}.finish()")); arg.rust_pre.push(format!( - "let b{p} = rt::bytes(env, {p}).unwrap_or_default();\n\t\tlet a{p} = rt::Reader::new(&b{p}).get();" + "let b{p} = rt::bytes(env, {p}).unwrap_or_default();\n\t\tlet mut r{p} = rt::Reader::new(&b{p});\n\t\tlet a{p} = r{p}.get();" )); arg.rust_args.push(match c { Ty::In(_) => format!("&a{p}"), @@ -1509,6 +1545,25 @@ impl<'a> Gen<'a> { Some(indent_by(&code, indent_level)) } + /// The Kotlin statement closing each object the value `e` of type `ty` holds, once a + /// call moved them out. + fn close(&mut self, ty: &Type, e: &str) -> String { + match ty { + Type::Option(inner) => { + let v = self.fresh("v"); + let close = self.close(inner, &v); + format!("{e}?.let {{ {v} -> {close} }}") + } + Type::Box(inner) => self.close(inner, e), + Type::List(inner) => { + let v = self.fresh("v"); + let close = self.close(inner, &v); + format!("{e}.forEach {{ {v} -> {close} }}") + } + _ => format!("{e}.close()"), + } + } + /// The glue's out values for `outs`, the pointers the C call takes after its /// parameters, and the statements writing them, `ok` saying whether it returned ok. fn glue_outs(&mut self, outs: &[(String, Ty)], result: bool) -> (String, Vec, String) { @@ -1554,16 +1609,26 @@ impl<'a> Gen<'a> { fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { Type::Object(i) => !abi.api.objects[*i].copy, - Type::Option(inner) | Type::Box(inner) => moved(abi, inner), + Type::Option(inner) | Type::Box(inner) | Type::List(inner) => moved(abi, inner), _ => false, } } /// Whether `ty` crosses as a buffer of UTF-8 text, or none. fn string(ty: &Type) -> bool { + decode(ty).is_some() +} + +/// How Kotlin decodes the buffer of text `ty` crosses as, if it is text: a path, which on +/// Unix may hold any bytes, throws on those no `String` holds. +fn decode(ty: &Type) -> Option<&'static str> { match ty { - Type::Option(inner) => string(inner), - ty => matches!(ty.base(), Type::Str | Type::String), + Type::Option(inner) => decode(inner), + ty => match ty.base() { + Type::Str | Type::String => Some("decodeToString()"), + Type::Path | Type::PathBuf => Some("decodeToString(throwOnInvalidSequence = true)"), + _ => None, + }, } } @@ -1589,7 +1654,7 @@ fn jni_type(c: &Ty, ty: &Type) -> (&'static str, &'static str) { Ty::Handle { .. } | Ty::Future(_) => ("jlong", "Long"), Ty::Buf => ("jbyteArray", "ByteArray?"), Ty::Struct(_) if matches!(ty.base(), Type::Callback(..)) => ("jobject", "Any"), - Ty::Duration | Ty::Struct(_) | Ty::In(_) => ("jbyteArray", "ByteArray"), + Ty::Duration | Ty::SocketAddr | Ty::Struct(_) | Ty::In(_) => ("jbyteArray", "ByteArray"), Ty::Fn(..) => ("jobject", "Any"), Ty::Waker => ("jobject", "Waker"), Ty::Poll | Ty::Out(_) | Ty::Data => unreachable!("{c:?} never crosses JNI"), @@ -1659,6 +1724,7 @@ fn rust_c(abi: &Abi<'_>, c: &Ty) -> String { Ty::Enum(_) => "i32".to_string(), Ty::Buf => "crate::rt::Buf".to_string(), Ty::Duration => "crate::rt::Duration".to_string(), + Ty::SocketAddr => "crate::rt::SocketAddr".to_string(), Ty::Struct(name) => format!("crate::{name}"), Ty::Handle { .. } => "*const c_void".to_string(), other => { diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 5cd43dbc2..bfe1336c3 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -540,8 +540,14 @@ static int script(void) { api_buf_free(debug); /* A path's bytes as the OS holds them, which need not be UTF-8. */ - api_buf dir = {.ptr = (const uint8_t *)"\xff" "dir", .len = 4}; - api_buf file = api_file(dir, str("f")); + api_buf stray = api_stray(); + api_buf in_stray = api_file(stray, str("f")); + if (in_stray.len != 3 || memcmp(in_stray.ptr, "\xff/f", 3) != 0) { + return 1; + } + api_buf_free(stray); + api_buf_free(in_stray); + api_buf file = api_file(str("dir"), str("f")); api_buf paths[] = {file, str("/")}; api_vec_option_path parents = api_parents((api_vec_path){.ptr = paths, .len = 2}); print_escaped("file ", file, "\n"); @@ -591,6 +597,9 @@ static int script(void) { printf("%llu", (unsigned long long)api_counter_get(counters.ptr[i])); } printf("\n"); + /* Each counter moves in, leaving its handle only to free. */ + uint64_t worth = api_worth(counters); + printf("worth %llu %llu\n", (unsigned long long)worth, (unsigned long long)api_worth((api_vec_counter){0})); api_vec_counter_free(counters); uint32_t row0[] = {1, 2}; diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 5ac90a995..b205306ac 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -440,7 +440,7 @@ int script() { std::printf("\n"); // A path's bytes as the OS holds them, which need not be UTF-8. - api::buf file = api::file(api::view("\xff" "dir"), "f"); + api::buf file = api::file("dir", "f"); auto parents = api::parents({file, "/"}); print_escaped("file ", file, "\n"); std::printf("parents"); @@ -479,6 +479,8 @@ int script() { std::printf("%llu", static_cast(counters[i].get())); } std::printf("\n"); + auto worth = api::worth(std::move(counters)); + std::printf("worth %llu %llu\n", ull(worth), ull(api::worth(std::nullopt))); auto sums = api::sums({{1, 2}, {}, {3}}); std::printf("sums [%u, %u, %u]\n", sums[0], sums[1], sums[2]); diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 4ad6c4a89..7d2a423da 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -249,6 +249,37 @@ fun script() { val limit = retry.limitSetting println("retry ${if (limit != null) 1 else 0} ${limit ?: 0u} ${retry.limit()} ${retry.delay()}") println("slot ${SlotU8.new(200u).value()}") + + // Foreign types: a URL parsed from text, socket addresses, OS paths, and `Vec`s. + val base = Url.parse("https://example.com/a/b") + println("url $base host ${host(base)?.let { "Some(\"$it\")" } ?: "None"}") + resolve(base.clone(), "c?d").use { url -> println("resolve $url ${url == base}") } + try { + Url.parse("no scheme").close() + } catch (e: StringException) { + println("parse err ${e.error}") + } + val v4 = java.net.InetSocketAddress(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1)), 8080) + val loopback = ByteArray(16).also { it[15] = 1 } + val v6 = java.net.InetSocketAddress(java.net.Inet6Address.getByAddress(null, loopback, 3), 443) + println("ports ${ports(listOf(v4, v6))}") + val peer = Peer(v6, listOf("a", "b")).next("c") + println("peer ${peer.debug()} ${peer.aliases().joinToString(", ", "[", "]") { "\"$it\"" }}") + val file = file("dir", "f") + println("file $file") + println("parents ${parents(listOf(file, "/")).joinToString(" ") { it ?: "none" }}") + val words = words(" one two three ") + println("words ${words.size} ${words.joinToString(", ", "[", "]") { "\"$it\"" }}") + println("sentence ${sentence(words)} ${sentence(null)}") + println("sentence ${sentence(emptyList())}|") + println("chunks ${chunks("abcde".encodeToByteArray(), 2uL).map { it.toList() }}") + val counters = counters(listOf("x", "y")) + println("counters ${counters.joinToString(", ") { "${it.name()} ${it.get()}" }}") + // Each counter moves in, so Kotlin closes it. + println("worth ${worth(counters)} ${worth(null)}") + println("sums ${sums(listOf(listOf(1u, 2u), emptyList(), listOf(3u)))}") + val site = Site.new(base) + site.home.use { a -> site.home.use { b -> println("site $a $b") } } } fun check(ok: Boolean, what: String) { @@ -380,6 +411,8 @@ fun broken(which: String) { } kept!!.get() } + // No String holds a path that is not UTF-8. + "stray" -> stray() "callback threw" -> emit( object : Log { override fun log(level: UByte, line: String): Boolean = throw IllegalStateException("a log failed") diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 3e9a59109..5fb8d3087 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -238,6 +238,7 @@ fn kotlin(out: &Path, lib: &Path, native: &str) { "api_counter_into_label: a lent handle's value cannot move out", ), ("callback threw", "api_fixture::Log::log: a call into Kotlin threw"), + ("stray", "MalformedInputException"), ] { let output = java(&[case]).output().unwrap(); assert!(!output.status.success(), "Kotlin {case} did not fail"); From b368920d90a74cf9cb1acc197c8396f10482b4c0 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:56:20 -0700 Subject: [PATCH 6/9] fix(ffi): close Kotlin objects moved in through a boxed Vec Gate the close on `moved` alone, which already recurses through Box, so a Box> parameter closes each object as Option> does. The api fixture's `worth` takes Option>>, and Kotlin checks a counter it moved in is closed. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 6 +++++- ffi/fixtures/api/src/lib.rs | 4 ++-- ffi/src/kotlin.rs | 4 +--- ffi/tests/api.kt | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 54e5a0ca4..e12eab8a2 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -346,7 +346,11 @@ fn main() { let counters = api_lib::counters(vec!["x".to_string(), "y".to_string()]); let names: Vec = counters.iter().map(|c| format!("{} {}", c.name(), c.get())).collect(); println!("counters {}", names.join(", ")); - println!("worth {} {}", api_lib::worth(Some(counters)), api_lib::worth(None)); + println!( + "worth {} {}", + api_lib::worth(Some(Box::new(counters))), + api_lib::worth(None) + ); println!("sums {:?}", api_lib::sums(vec![vec![1, 2], Vec::new(), vec![3]])); 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 a50880c6e..9beedc1de 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -959,8 +959,8 @@ pub fn counters(names: Vec) -> Vec { names.iter().map(|name| Counter::new(name, 1)).collect() } -/// The counters' values summed, or 0 for none: each moves in. -pub fn worth(counters: Option>) -> u64 { +/// The counters' values summed, or 0 for none: each moves in, through the `Box` too. +pub fn worth(counters: Option>>) -> u64 { counters.map_or(0, |counters| counters.iter().map(Counter::get).sum()) } diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index e43b28c00..81eaba48d 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -491,9 +491,7 @@ impl<'a> Gen<'a> { Ty::Duration | Ty::SocketAddr | Ty::Struct(_) | Ty::In(_) => { let w = self.fresh("w"); // Taken by value, each object in a list moves out, as one passed alone does. - if let Type::List(_) | Type::Option(_) = ty - && moved(abi, ty) - { + if moved(abi, ty) { let close = self.close(ty, e); arg.after.push(close); } diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 18bd1f048..0fdde02bd 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -284,6 +284,7 @@ fun script() { println("counters ${counters.joinToString(", ") { "${it.name()} ${it.get()}" }}") // Each counter moves in, so Kotlin closes it. println("worth ${worth(counters)} ${worth(null)}") + check(runCatching { counters[0].get() }.exceptionOrNull() is IllegalStateException, "a counter moved in is closed") println("sums ${sums(listOf(listOf(1u, 2u), emptyList(), listOf(3u)))}") val site = Site.new(base) site.home.use { a -> site.home.use { b -> println("site $a $b") } } From 0ae6fc92543c3167443ac538c0e941b03ce80b6a Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:02:51 -0700 Subject: [PATCH 7/9] chore(quest): plan slices and SocketAddrV4/V6 for the exporter The follow-ups #190 leaves: `&[T]` of any mapped type (12 refusals at the pinned moq) and the typed socket addresses moq-tokio's listen config holds. Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-export/README.md | 1 + quest/m1/ffi-export/slices.md | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 quest/m1/ffi-export/slices.md diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index 6422ae4f2..2406682f4 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -51,6 +51,7 @@ Every generator on the C ABI: - [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` - [Setters for an object's pub fields](/quest/m1/ffi-export/field-setters.md) - C fills in a session's config, as Rust assigns it - [Sessions on moq-tokio](/quest/m1/ffi-export/session-tokio.md) - bindings connect and accept with one call, the library running moq-tokio inside +- [Slices of any type, and SocketAddrV4/V6](/quest/m1/ffi-export/slices.md) - `&[T]` and the typed socket addresses cross the C ABI, as `Vec` and `SocketAddr` do - [Sessions on sans-IO QUIC](/quest/m1/ffi-export/session-quic.md) - bindings move UDP datagrams and drive the session's poll themselves - [C++ bindings on MSVC and Clang](/quest/m1/ffi-export/cpp.md) - the C++ header built and tested beyond GCC on Linux - [Kotlin bindings: zero-copy payloads and the read path](/quest/m1/ffi-export/kotlin.md) - the Kotlin lends bytes without a copy, and reads at JNI's cost diff --git a/quest/m1/ffi-export/slices.md b/quest/m1/ffi-export/slices.md new file mode 100644 index 000000000..506796089 --- /dev/null +++ b/quest/m1/ffi-export/slices.md @@ -0,0 +1,31 @@ +# [S] Slices of any type, and SocketAddrV4/V6 + +## Goal + +`&[T]` of any type the exporter maps, and `std::net::SocketAddrV4` and `SocketAddrV6`, +cross the C ABI, so the census stops refusing them. Proven by the api fixture's transcript +from C, C++, and Kotlin, and from C in `moq/moq.c`. + +## Plan + +Found by the census on 2026-10-28, after +[#190](https://github.com/kixelated/rust2/pull/190) mapped `Vec` as +`_vec_` and `SocketAddr` as `_socket_addr`. + +- A slice crosses as the same `_vec_`: a view passed in, and returned as an + owned copy, as a returned `&str` is. A slice of references follows `Vec<&T>`'s rules. + The 12 refusals at the pinned moq: `Pattern::segments`, `connect::Addr::addresses`, + `Addrs::as_slice`, `Hops::as_slice`, `container::fmp4::Muxer::fragment`, + `tls::PeerIdentity::chain`, `cli::check_env` (twice), and the constants `ALPNS`, + `stats::Failure::ALL`, `stats::RefusalKind::ALL`, and `accept::Failure::ALL`. +- `SocketAddrV4` and `SocketAddrV6` share `_socket_addr`, with `v6` fixed by the + type: one passed in of the other family aborts, as an IPv4 address with IPv6 bytes does. + Kotlin keeps `java.net.InetSocketAddress`. They refuse moq-tokio's + `listen::Config::preferred_v4` and `preferred_v6`. +- Report the census before and after. + +Public API: the C names the exporter emits. + +## Related + +- [Setters for an object's pub fields](/quest/m1/ffi-export/field-setters.md) - `preferred_v4` and `preferred_v6` are fields C sets From 555f0a07e7f3df1e854529ed3f0be12104c5ea4b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:09:01 -0700 Subject: [PATCH 8/9] test(ffi): move objects in through a bare Box> too `worth` takes an Option> and a Box> apart, so the fixture covers a boxed Vec without an Option around it, which the Kotlin close missed before b368920. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 7 ++++--- ffi/fixtures/api/src/lib.rs | 7 ++++--- ffi/tests/api.c | 5 +++-- ffi/tests/api.cpp | 7 +++++-- ffi/tests/api.kt | 6 ++++-- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index e12eab8a2..5485ed679 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -343,13 +343,14 @@ fn main() { ); println!("sentence {}|", api_lib::sentence(Some(Vec::new()))); println!("chunks {:?}", api_lib::chunks(b"abcde", 2)); - let counters = api_lib::counters(vec!["x".to_string(), "y".to_string()]); + let mut counters = api_lib::counters(vec!["x".to_string(), "y".to_string()]); let names: Vec = counters.iter().map(|c| format!("{} {}", c.name(), c.get())).collect(); println!("counters {}", names.join(", ")); + let boxed = counters.split_off(1); println!( "worth {} {}", - api_lib::worth(Some(Box::new(counters))), - api_lib::worth(None) + api_lib::worth(Some(counters), Box::new(boxed)), + api_lib::worth(None, Box::default()) ); println!("sums {:?}", api_lib::sums(vec![vec![1, 2], Vec::new(), vec![3]])); let site = api_lib::Site::new(base); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 9beedc1de..73d2aaebe 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -959,9 +959,10 @@ pub fn counters(names: Vec) -> Vec { names.iter().map(|name| Counter::new(name, 1)).collect() } -/// The counters' values summed, or 0 for none: each moves in, through the `Box` too. -pub fn worth(counters: Option>>) -> u64 { - counters.map_or(0, |counters| counters.iter().map(Counter::get).sum()) +/// The counters' values summed, none counting 0: each moves in, through an `Option` or a +/// `Box`. +pub fn worth(some: Option>, boxed: Box>) -> u64 { + some.into_iter().flatten().chain(*boxed).map(|c| c.get()).sum() } /// Each row's sum. diff --git a/ffi/tests/api.c b/ffi/tests/api.c index bfe1336c3..1b1a98df9 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -598,8 +598,9 @@ static int script(void) { } printf("\n"); /* Each counter moves in, leaving its handle only to free. */ - uint64_t worth = api_worth(counters); - printf("worth %llu %llu\n", (unsigned long long)worth, (unsigned long long)api_worth((api_vec_counter){0})); + uint64_t worth = api_worth((api_vec_counter){.ptr = counters.ptr, .len = 1}, (api_vec_counter){.ptr = counters.ptr + 1, .len = 1}); + uint64_t none = api_worth((api_vec_counter){0}, (api_vec_counter){.ptr = counters.ptr, .len = 0}); + printf("worth %llu %llu\n", (unsigned long long)worth, (unsigned long long)none); api_vec_counter_free(counters); uint32_t row0[] = {1, 2}; diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index b205306ac..a53f0bb61 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -479,8 +479,11 @@ int script() { std::printf("%llu", static_cast(counters[i].get())); } std::printf("\n"); - auto worth = api::worth(std::move(counters)); - std::printf("worth %llu %llu\n", ull(worth), ull(api::worth(std::nullopt))); + std::vector some, boxed; + some.push_back(std::move(counters[0])); + boxed.push_back(std::move(counters[1])); + auto worth = api::worth(std::move(some), std::move(boxed)); + std::printf("worth %llu %llu\n", ull(worth), ull(api::worth(std::nullopt, {}))); auto sums = api::sums({{1, 2}, {}, {3}}); std::printf("sums [%u, %u, %u]\n", sums[0], sums[1], sums[2]); diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 0fdde02bd..ba300e8c8 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -283,8 +283,10 @@ fun script() { val counters = counters(listOf("x", "y")) println("counters ${counters.joinToString(", ") { "${it.name()} ${it.get()}" }}") // Each counter moves in, so Kotlin closes it. - println("worth ${worth(counters)} ${worth(null)}") - check(runCatching { counters[0].get() }.exceptionOrNull() is IllegalStateException, "a counter moved in is closed") + println("worth ${worth(listOf(counters[0]), listOf(counters[1]))} ${worth(null, emptyList())}") + for (counter in counters) { + check(runCatching { counter.get() }.exceptionOrNull() is IllegalStateException, "a counter moved in is closed") + } println("sums ${sums(listOf(listOf(1u, 2u), emptyList(), listOf(3u)))}") val site = Site.new(base) site.home.use { a -> site.home.use { b -> println("site $a $b") } } From ef674d43fa9fed283f1f0572c95b018a9b9684b1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:35:36 -0700 Subject: [PATCH 9/9] test(ffi): set a session's URL, fingerprints, and bind from C, C++, and Kotlin With #189's setters and the foreign types mapped, `connect.url`, `tls.fingerprint`, and `listen.bind` generate setters. moq's tests set each and read it back, which closes field-setters' goal end to end. Co-Authored-By: Claude Opus 5.5 --- ffi/moq/moq.c | 62 +++++++++++++++++++++++++++++-------------------- ffi/moq/moq.cpp | 15 ++++++++++++ ffi/moq/moq.kt | 15 +++++++++++- 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 22904a5dd..aee3cd314 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -338,40 +338,54 @@ int main(void) { moq_tls_connect *tls = moq_connect_config_tls(connect); moq_tls_connect_set_insecure(tls, (moq_option_bool){.some = true, .value = true}); moq_tls_connect_set_host_name(tls, str("relay.example")); + /* A `Vec`, an array of buffers C keeps. */ + moq_buf pins[] = {str("ab:cd")}; + moq_tls_connect_set_fingerprint(tls, (moq_vec_string){.ptr = pins, .len = 1}); moq_connect_config_set_tls(connect, tls); moq_tls_connect_free(tls); moq_connect_config_set_implementation(connect, str("moq.c")); + /* A URL, parsed from text, failing with why, and moved in. */ + moq_url *url = NULL; + moq_buf parse_err; + CHECK(!moq_url_parse(str("relay.example"), &url, &parse_err)); + CHECK(is(parse_err, "relative URL without a base")); + CHECK(moq_url_parse(str("https://relay.example:4443/demo"), &url, &parse_err)); + moq_connect_config_set_url(connect, url); + moq_url_free(url); tls = moq_connect_config_tls(connect); moq_option_bool insecure = moq_tls_connect_insecure(tls); CHECK(insecure.some && insecure.value); CHECK(is(moq_tls_connect_host_name(tls), "relay.example")); + /* TLS files, as OS paths: none by default. */ + moq_vec_path roots = moq_tls_connect_root(tls); + moq_vec_string fingerprints = moq_tls_connect_fingerprint(tls); + CHECK(roots.ptr != NULL && roots.len == 0); + CHECK(fingerprints.len == 1 && fingerprints.ptr[0].len == 5 && memcmp(fingerprints.ptr[0].ptr, "ab:cd", 5) == 0); + moq_vec_path_free(roots); + moq_vec_string_free(fingerprints); moq_tls_connect_free(tls); CHECK(is(moq_connect_config_implementation(connect), "moq.c")); - moq_connect_config_free(connect); - - /* A backoff's fields are settings left unset, and the methods of their names resolve them. */ - moq_backoff *backoff = moq_backoff_default(); - CHECK(moq_backoff_initial_setting(backoff) == NULL); - moq_duration initial = moq_backoff_initial(backoff); - CHECK(initial.secs == 1 && initial.nanos == 0); - moq_backoff_free(backoff); - - /* Session config's foreign types. A URL is parsed from text, failing with why. */ - moq_url *url = NULL; - moq_buf parse_err; - CHECK(!moq_url_parse(str("relay.example"), &url, &parse_err)); - CHECK(is(parse_err, "relative URL without a base")); - CHECK(moq_url_parse(str("https://relay.example:4443/demo"), &url, &parse_err)); + url = moq_connect_config_url(connect); + CHECK(url != NULL && is(moq_url_to_string(url), "https://relay.example:4443/demo")); + /* The config's URL, dialed with no tokens. */ moq_auth_tokens *tokens = moq_auth_tokens_new((moq_vec_auth_token){0}); moq_versions *versions = moq_versions_default(); moq_auth_dial *dial = moq_auth_tokens_dial(tokens, url, versions); moq_url *dialed = moq_auth_dial_url(dial); CHECK(moq_url_eq(dialed, url)); - CHECK(is(moq_url_to_string(dialed), "https://relay.example:4443/demo")); moq_url_free(dialed); moq_auth_dial_free(dial); moq_auth_tokens_free(tokens); moq_url_free(url); + moq_connect_config_free(connect); + + /* A backoff's fields are settings left unset, and the methods of their names resolve them. */ + moq_backoff *backoff = moq_backoff_default(); + CHECK(moq_backoff_initial_setting(backoff) == NULL); + moq_duration initial = moq_backoff_initial(backoff); + CHECK(initial.secs == 1 && initial.nanos == 0); + moq_backoff_free(backoff); + /* A `Vec`, each element a buffer the array frees. */ moq_vec_string alpns = moq_versions_alpns(versions); CHECK(alpns.len > 0); @@ -380,20 +394,18 @@ int main(void) { } moq_vec_string_free(alpns); moq_versions_free(versions); - /* A socket address, crossing in a listener's bind. */ + /* A socket address, crossing in a listener's bind, set on its config and read back. */ moq_listen_bind bind = {.tag = MOQ_LISTEN_BIND_ADDR, .addr = {._0 = {.ip = {[15] = 1}, .port = 4443, .v6 = true}}}; CHECK(is(moq_listen_bind_to_string(&bind), "[::1]:4443")); moq_listen_bind v4 = {.tag = MOQ_LISTEN_BIND_ADDR, .addr = {._0 = {.ip = {127, 0, 0, 1}, .port = 0}}}; CHECK(is(moq_listen_bind_to_string(&v4), "127.0.0.1:0")); CHECK(!moq_listen_bind_eq(&bind, &v4)); - /* TLS files, as OS paths, and fingerprints, as strings: none by default. */ - moq_tls_connect *tls = moq_tls_connect_default(); - moq_vec_path roots = moq_tls_connect_root(tls); - moq_vec_string fingerprints = moq_tls_connect_fingerprint(tls); - CHECK(roots.ptr != NULL && roots.len == 0 && fingerprints.len == 0); - moq_vec_path_free(roots); - moq_vec_string_free(fingerprints); - moq_tls_connect_free(tls); + moq_listen_config *listen = moq_listen_config_default(); + moq_listen_config_set_bind(listen, (moq_option_listen_bind){.some = true, .value = bind}); + moq_option_listen_bind bound = moq_listen_config_bind(listen); + CHECK(bound.some && moq_listen_bind_eq(&bound.value, &bind)); + moq_listen_bind_free(&bound.value); + moq_listen_config_free(listen); if (failures == 0) { printf("ok\n"); diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index 04fc78eb6..1b7614334 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -204,11 +204,26 @@ int main() { moq::tls_connect tls = connect.tls(); tls.set_insecure(true); tls.set_host_name("relay.example"); + tls.set_fingerprint({"ab:cd"}); connect.set_tls(std::move(tls)); connect.set_implementation("moq.cpp"); + connect.set_url(moq::url::parse("https://relay.example:4443/demo").value()); CHECK(connect.tls().insecure() == std::optional(true)); CHECK(is(*connect.tls().host_name(), "relay.example")); + auto fingerprints = connect.tls().fingerprint(); + CHECK(fingerprints.size() == 1 && is(fingerprints[0], "ab:cd")); CHECK(is(*connect.implementation(), "moq.cpp")); + CHECK(is(connect.url()->to_string(), "https://relay.example:4443/demo")); + + // A listener's bind, a socket address. + moq::socket_addr addr{}; + addr.ip[15] = 1; + addr.port = 4443; + addr.v6 = true; + moq::listen_config listen = moq::listen_config::default_(); + listen.set_bind(moq::listen_bind(moq::listen_bind_addr{addr})); + auto bind = listen.bind(); + CHECK(bind && is(moq::listen_bind_to_string(*bind), "[::1]:4443")); } // moq-tokio's terminal connect errors, which moq-net's `Error` shares no names with. diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 6a93a2e42..d4bd9ab63 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -202,7 +202,16 @@ fun main() { } catch (e: StringException) { expect(e.message == "relative URL without a base", "a URL's parse error is Rust's") } - val url = Url.parse("https://relay.example:4443/demo") + // A session's config, its URL and pinned fingerprints set as Rust assigns them. + val connect = ConnectConfig.default() + connect.tls.use { tls -> + tls.setFingerprint(listOf("ab:cd")) + connect.setTls(tls) + } + connect.setUrl(Url.parse("https://relay.example:4443/demo")) + connect.tls.use { tls -> expect(tls.fingerprint == listOf("ab:cd"), "a TLS config keeps its fingerprints") } + val url = connect.url!! + connect.close() val versions = Versions.default() AuthTokens.new(emptyList()).use { tokens -> tokens.dial(url, versions).use { dial -> @@ -223,6 +232,10 @@ fun main() { expect(bind.toString() == "[::1]:4443", "an IPv6 bind prints as Rust's") val v4 = ListenBind.Addr(java.net.InetSocketAddress(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1)), 0)) expect(v4.toString() == "127.0.0.1:0", "an IPv4 bind prints as Rust's") + ListenConfig.default().use { listen -> + listen.setBind(bind) + expect(listen.bind == bind, "a listener's config keeps its bind") + } // TLS files, as OS paths, and fingerprints, as strings: none by default. TlsConnect.default().use { tls -> expect(tls.root.isEmpty() && tls.fingerprint.isEmpty(), "a default TLS config lists none")