diff --git a/bench/ffi/README.md b/bench/ffi/README.md index d1c9f1b54..656ecf0f0 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -27,7 +27,7 @@ the run's summary; nothing fails on a number. | u64 getter | `demand.sequence()`: one borrowed handle and a field read, so all but 0.4 ns is the binding's | `group.sequence()` | | record return | `track.largest()`: an `Option` of two `u64`s, copied across | `track.info()`, a record | | object return | `track.info()`: a new handle, freed | none | -| write | `group.write_frame(at, frame)` with one `Timestamp` handle and 100 B, copied into Rust | `write_frame` | +| write | `group.write_frame(at, frame)` with one `Timestamp` and 100 B, copied into Rust | `write_frame` | | stamped write | the same with a new `Timestamp` per frame, as a caller holding a `u64` must | none | | ready read | a 100 B frame written, then read through a new future ready at its first poll | `await noop_async()`, plus a write | | woken read | a read started before its frame is written, as the language starts work concurrently, so it suspends and the write's wake resumes it | `await yield_once()`, plus a write | @@ -243,10 +243,12 @@ From the CPU tables of 2026-11-10, measured at rust2 `7383790d` at a load averag 29 and since replaced by the tables above, read against the code each call runs. In order of what it costs moq's frame path: -1. **A handle per frame timestamp.** `Timestamp` is `Copy` in Rust, but its private fields - make it an object, so each stamped frame makes and frees a handle: 21 ns in C, 27 in - Swift, 140 in Python, 250 in Kotlin, 260 in Go, and 245 in Dart (stamped write minus - write). In Go, Dart, and Kotlin that doubles the write. +1. **A call per frame timestamp.** `Timestamp` is `Copy` with private fields, so it + crosses as its 16 bytes, and a stamped frame allocates and frees nothing in Rust. As + a handle, it cost 21 ns in C, 27 in Swift, 140 in Python, 250 in Kotlin, 260 in Go, + and 245 in Dart (stamped write minus write, in the tables above); as a value, see + [Copy values](#copy-values): what remains is each language's call and, in Kotlin and + Python, an object of its own. 2. **Ownership of a returned object.** An object return costs C 25 ns, but Go 263 and Dart 242, ten times C: each new Go object registers `runtime.AddCleanup` and each Dart one a `NativeFinalizer`, beside a handle allocation. Kotlin's 137 ns wall (210 CPU) is a phantom @@ -285,12 +287,33 @@ count since the change below. Kotlin: 24 wall, Go 38, Dart 28, Python 49, each w transition besides. Swift has since made each object a noncopyable struct and dropped its hold count, but for -a class: a `Copy` object or one thrown as an error. Measured 2026-11-11 at that change, 5 +a class: one thrown as an error. A `Copy` type is a value since [Copy values](#copy-values). Measured 2026-11-11 at that change, 5 rounds interleaved with C at a load average of 7 to 12, its getter costs 10 ns, as C's does, and `track.is_closed()`, on `TrackProducer`, still a class since moq throws it, 26 against C's 18, as in the table. A change to the accounting moves this row and the handle call's by the same amount; measure both, CPU first, at the load beside them. +### Copy values + +A `Copy` type with private fields, such as `Timestamp`, crosses by value: a struct of its +bytes in C, C++, Swift, Go, and Dart, a class over a `LongArray` in Kotlin (a word value +class when it fits one), and a class of the runtime's `Value` holding its bytes in Python. +Measured 2026-11-13 at rust2 `7766768f`, moq `ffcb7cd9`, on an AMD Ryzen 9 9950X (32 threads), +Linux, at a load average of 13 to 15, the median of 3 rounds, in CPU time: + +| | C | C++ | Swift | Go | Dart | Kotlin | Python | +|---|---|---|---|---|---|---|---| +| write | 163 | 165 | 173 | 260 | 251 | 280 | 224 | +| stamped write | 161 | 168 | 174 | 293 | 296 | 430 | 384 | +| stamped minus write | 0 | 3 | 1 | 33 | 45 | 150 (35 wall) | 160 | + +C, C++, and Swift pay nothing for the timestamp; Go and Dart pay about their getter's call, +the `from_micros` itself. Kotlin's 35 ns wall is its call, and its CPU column adds the +collector's work on each `LongArray`. Python's is a call and a new object. The handle's cost +in the tables above came from another load, so Python's was also measured against main's +build on the same host, interleaved over 5 rounds at a load of 10 to 12: stamped minus write +fell from 205 ns to 166, and the stamped write from 441 to 391. + ### Against the targets Where the cases match, on wall time: Kotlin's getter costs 24 ns against BoltFFI's 5.8, diff --git a/ffi/README.md b/ffi/README.md index 041f10992..f03f24cfa 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -92,12 +92,13 @@ public path names, or an error taken by value, is held by value as an opaque han | --- | --- | | a struct whose fields are all `pub`, not `#[non_exhaustive]`, which map, with no `Drop` of its own | a record: a struct, copied across | | an enum with variants whose fields map, with no `Drop` of its own | a C enum without fields, or a struct of a tag and a union | -| any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free` | +| any other type that is `Copy` and `Send`, with no `pub` field, no `pub` method taking `&mut self`, and no union or enum whose variants initialize different bytes, such as an `Option`, in it, such as moq's `Timestamp` or std's `Instant` | an opaque value: a struct of its bytes, copied across, which only Rust makes or reads, with its padding zeroed; one no export returns is left out, since C could never make one | +| any other type, if `Send` | an object: an opaque handle to an `Arc`, freed with `_free`; one that is `Copy`, since C may change it in place, is copied out of its handle where Rust takes it by value, and a closure neither takes nor returns one by value | | a type whose only generic parameters are lifetimes, such as `Path<'a>` | an object holding its `'static` form | | a generic type, at each instance it infers | that instance as any other type, with each method whose impl and own bounds it meets; any other instance is refused | | a `pub` field of an object | a getter returning a copy, and a setter assigning in place, as `x.field = value` does | | a `pub` trait | a callback interface: a table of C functions and their data; a trait C cannot implement, such as one with a blanket impl, an associated type, or a supertrait but a marker, is left out, since C never passes one, and a signature taking one from C is refused | -| a type parameter or `impl` bounded by `Fn`, `FnMut`, or `FnOnce` alone, taking what a callback takes, objects by reference, or `Copy` objects, and returning a scalar, a `String`, a `Copy` object, an `Option` of one, or a `Result` of one and a `String` | a C function and its `void *` data; one a lifetime bound or the return lets outlive the call, as kio's `clock::drive` and a spawned task's are, adds a `free` Rust calls once it drops it; a closure of any other signature is refused | +| a type parameter or `impl` bounded by `Fn`, `FnMut`, or `FnOnce` alone, taking what a callback takes, objects by reference, or opaque values, and returning a scalar, a `String`, an opaque value, an `Option` of one, or a `Result` of one and a `String` | a C function and its `void *` data; one a lifetime bound or the return lets outlive the call, as kio's `clock::drive` and a spawned task's are, adds a `free` Rust calls once it drops it; a closure of any other signature is refused | | a type parameter or `impl` taken by value and bounded by `Future` or `IntoFuture`, whose every bound `Pin + Send>>` meets, as kio's `clock::timeout` takes, or taken as `Pin<&mut F>` by a poll, as kio's `Waiter::poll_future` takes | `_foreign_future`: a future of `()` C implements, which Rust takes as that type, or which the future the poll makes owns and pins; a future of another output is refused | | a type parameter or `impl` bounded by `Read` and `Seek` alone, behind `&mut`, which no lifetime bound or return lets outlive the call, as moq-mux's `container::mp4::repair` takes | `_foreign_reader`: a `read` and a `seek` C implements and their data, lent for the call; any other reader is refused | | `fn` and inherent `pub` methods | functions named through their type | @@ -252,7 +253,9 @@ where it was while its file is unchanged. - **Handles** are `Arc`s, and a call borrows the value as Rust's receiver or parameter does, checked at run time: shared for `&T`, exclusive for `&mut T`, and moved out for - `T`, after which the handle is only freed. A `Copy` value is copied out instead, and a + `T`, after which the handle is only freed. A `Copy` object, one with a `pub` field, a + `pub` method taking `&mut self`, or bytes its value decides are initialized, as an + `Option` in it does, is copied out instead, leaving its handle as it was. A value that is not `Sync` is shared on one thread at a time, and held exclusively by a 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: @@ -261,6 +264,14 @@ where it was while its file is unchanged. while a getter's copy is C's own: a nested field is got, set, and set back. A type whose C++ copies share one value has `_borrowed`, true while a call or future borrows it, so moving it out would abort. +- **Opaque values** are `typedef struct { uintN_t opaque[n]; } ;`, the bytes of a + `Copy` type with private fields, in units of its alignment up to 8 bytes: C copies one + as any struct, and passes `&self` as a `const *`, but never makes or changes one, + and frees none. Its padding is zero, from a table of each target's padding rustc's + layout gives, so no byte Rust left uninitialized reaches a language, which may + serialize what it holds. Its size and alignment are the largest of the export's targets, so one + header serves them all, a few bytes past a 32-bit target's own. A change of size is an + ABI change, as a record's field change is. - **In and out**: a `&mut` of a value is two parameters, the value passed in as one taken by value is, and a `_out` pointer, through which Rust writes what the call left in it on every path, its errors included. C keeps what it passed, and frees what Rust wrote @@ -308,13 +319,12 @@ where it was while its file is unchanged. - **Closures** are a function and its data, which Rust calls from any thread, one call at a time. One lent for a call Rust calls only during the call; one Rust keeps comes with a `free`, which Rust calls once it drops the closure, done or not, maybe before - any call. One takes what a callback takes, and objects by reference or `Copy` objects, - each lent as a handle to the value where it is for that call: C borrows it no further - than Rust lent it, and moving out of it, freeing it, or keeping a borrow past the call - aborts. What it returns is Rust's to own: a handle C made or was given, one - `_copy` made of a `Copy` object C keeps, which copies it as Rust does without - running its `Clone`, or a buffer `_buf_copy` made; a `Result` returns false and - writes its error's buffer. + any call. One takes what a callback takes, objects by reference, each lent as a handle + to the value where it is for that call, and opaque values, copied: C borrows a lent + handle no further than Rust lent it, and moving out of it, freeing it, or keeping a + borrow past the call aborts. What it returns is Rust's to own: an opaque value, copied + as Rust copies it without running its `Clone`, or a buffer `_buf_copy` made; a + `Result` returns false and writes its error's buffer. - **Readers** are a `read`, a `seek`, and their data, lent for the call, which Rust calls from any thread, one call at a time. `read` writes at most the length it was given, `seek` takes `SEEK_SET`, whose offset's bits are a `uint64_t` that may pass @@ -416,7 +426,8 @@ packages place it in. - **Records** are structs, and **enums** an `enum class`, or for one with data a `std::variant` of a struct per variant, `_`. An enum's methods and traits are free functions taking it first. `PartialEq` is `==`, `Display` `to_string()`, - `Debug` `debug()`, and `Default` `default_()`. + `Debug` `debug()`, and `Default` `default_()`. An **opaque value** is a trivially + copyable class holding its C struct, with no public constructor, so only Rust makes one. - **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, so it outlives the object it came from. From C++20, either converts to `std::span`. A `view` or span @@ -430,8 +441,7 @@ packages place it in. - **Closures** are any callable, taken by a template parameter, which Rust calls from any thread, one call at a time. One lent for a call Rust calls only during the call; one Rust keeps it moves or copies into a copy of its own, which it destroys once done with it, - maybe before any call. An object it lends as `&T`, or a `Copy` one, is a `const` - reference to its class, and as `&mut T` an `api::borrow`, which reaches its methods + maybe before any call. An object it lends as `&T` is a `const` reference to its class, and as `&mut T` an `api::borrow`, which reaches its methods through `->` but neither moves nor copies. Moving out of either, or keeping a copy past the call, aborts. It returns bytes as anything a `view` takes, copied for Rust, and a `Result` as a `result`, or anything with its `operator bool`, `operator*`, and @@ -492,7 +502,13 @@ out, with only its other fields encoded. assignment would hide. - **Records** are data classes, compared by their fields, and **enums** an `enum class`, or for one with data a sealed class of a data class per variant. `Display` is - `toString()`, `Debug` `debug()`, and `Default` a companion's `default()`. + `toString()`, `Debug` `debug()`, and `Default` a companion's `default()`. An **opaque + value** is a class with an internal constructor, so only Rust makes one: of no more than + 8 bytes on every target, with no padding, fields of integers, `bool`s, and `char`s alone, nested or not, and a + derived or std `PartialEq`, a value class over a `Long`, compared by its bits, which + allocates nothing unless Kotlin boxes it, as in a `List` or a nullable; otherwise one over + a `LongArray`, whose `==` is Rust's, and whose `hashCode` is 0, since none Kotlin + computes agrees with it. - **Bytes** are a `Bytes`. Passed in, Rust's own are shared with no copy, and Kotlin's, which `Bytes(ByteArray)` holds without a copy, are copied once into a buffer Rust takes, at a call's top level or in a record; those Rust lends a closure or trait method are @@ -532,8 +548,7 @@ out, with only its other fields encoded. - **Closures** are function types, which Rust calls from any thread: one lent for a call only during the call, and one Rust keeps, as `Spawned.new` does, until it drops it, when it deletes its global reference and the GC may collect it. An object it lends is - closed once the call returns. A `Copy` object it returns crosses as a new handle to a - clone, so Kotlin keeps its own. One returning a `Result` returns its value and fails by + closed once the call returns. One returning a `Result` returns its value and fails by throwing the `Exception` a function's `Err` throws, as `StringException("…")`; any other exception aborts, as a callback's does. - A **reader** is a `java.nio.channels.SeekableByteChannel`, which a `FileChannel` or @@ -596,7 +611,8 @@ caller imports it with no ignores. of upper-case members, or for one with data a class of a dataclass per variant, `Event.Moved(p)`. `Display` is `str()`, `Debug` `repr()`, and `Default` a static `default()`. Constants are module or class attributes, made anew on each read unless a - literal. + literal. An **opaque value** is a class of the runtime's `Value`, which holds its bytes + in place, so only Rust makes or reads one: making one raises `TypeError`. - **Bytes** passed in are any buffer, `bytes | bytearray | memoryview`: `bytes` is read in place, and others, which Python may change while Rust reads, are copied. Bytes Rust returns, or a record holds, are a read-only `memoryview` lending Rust's buffer until it @@ -625,9 +641,9 @@ caller imports it with no ignores. `close()` is called if it has one. - **Closures** are any callable, which Rust calls from any thread, one call at a time, taking the GIL: one lent for a call only during the call, and one Rust keeps until it - drops it, when it lets go of its reference. An object it lends, `Copy` ones included, - is closed once that call of it returns. What it returns Rust owns: a `str` is copied, - and an object's handle cloned, so the caller's stays its own. One returning a `Result` + drops it, when it lets go of its reference. An object it lends is closed once that call + of it returns. What it returns Rust owns: a `str` is copied, and an opaque value's + bytes, so the caller's stays its own. One returning a `Result` fails by raising the exception of its error, such as `StringException(message)`, whose `error` Rust takes. Any other exception a closure or trait method raises prints its traceback and aborts, since Rust has nowhere to unwind it to. @@ -686,15 +702,17 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod `clamped()`, `clamped(low:)`, `clamped(high:)` and `clamped(low:high:)`, up to two per function; `Option<&mut T>` is `inout T?`. `Clone` is `clone()`, and `PartialEq` is `==`, which Swift 6.4 makes `Equatable`; `Display` is `description` and `Debug` `debugDescription`, which Swift - conforms no noncopyable type to yet. An object that is `Copy`, or thrown as an error, - which `Swift.Error` requires be copyable, is a final class instead, whose handle a call + conforms no noncopyable type to yet. A `Copy` object, which Rust copies where it takes + one by value, and an object thrown as an error, which `Swift.Error` requires be + copyable, are final classes instead, whose handle a call holds until it returns, and moving its value out closes it, after which a call traps. A field is a computed property, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in, which an assignment would hide. An object is `Sendable`, since Rust checks each borrow at run time. - **Records** are structs and **enums** Swift enums, with a variant's data as associated values, all `Sendable`. `PartialEq` is `==`, through Rust's; `Display` is `description`, - `Debug` `debugDescription`, and `Default` a static `default()`. + `Debug` `debugDescription`, and `Default` a static `default()`. An **opaque value** is + a struct holding its C struct, with no public initializer, so only Rust makes one. - **Bytes** are the runtime's `Bytes`, a `RandomAccessCollection` of `UInt8` whose slices are `Bytes` too. Bytes Rust returns lend its buffer until Swift releases the last `Bytes` or slice reading it, and pass back to Rust as a second owner of it, with no copy, except @@ -721,7 +739,7 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod at a time. One lent for a call Rust calls only during the call; one Rust keeps is `@escaping`, boxed until Rust drops it. An object it lends is `borrowing`, or `inout` for `&mut T`, so it cannot outlive the call; assigning another to the `inout` one traps, - since Rust's stays its own. A `Copy` object it takes is a copy of its own. One returning + since Rust's stays its own. One returning `Result` `throws`: Rust reads its error's `description`, so a thrown `StringError` returns its string as it was. - A **reader** is `inout some SeekableReader`, the runtime's protocol of @@ -801,7 +819,10 @@ reads a go.mod. `Debug` `GoString()`, `PartialEq` `Equal(other)`, all through Rust's, and `Default` a function of the package, `PointDefault()`. An object's exported `Handle_` field, and a trait's `Vtable_` function, are how one generated package reaches another's, - since cgo gives each its own C types: callers use neither. + since cgo gives each its own C types: callers use neither. An **opaque value** is a + struct of its bytes, `Bytes_`, and `Made_`, true once Rust made it: a zero value panics + where Rust would read it, and `==` does not compile, since bytes alike need not be + equal, so `Equal`, if it has one, is Rust's. - **Bytes** a call takes are a `[]byte`, and text a `string`, each read in place: Go's memory, pinned where C reads it through other memory. Bytes Rust passes a callback are copied into a `[]byte`. Bytes anywhere else, such as those Rust returns or a record @@ -839,10 +860,10 @@ reads a go.mod. - **Closures** are funcs, which Rust calls from any thread: one lent for a call only during the call, and one Rust keeps, as moq's `clock::drive` does, until Rust drops it, which releases it to the GC. An object it lends by reference is closed once the func - returns; a `Copy` one is its own clone, which it may keep. A `Result` returns an `error` + returns. A `Result` returns an `error` last, whose `Error()` crosses as Rust's `String`. What it returns crosses as Rust's: a - `string` copied, and an object as a clone of its handle, so the func keeps its own; a - closed one panics. + `string` copied, and an opaque value as its bytes, so the func keeps its own; a zero one + panics. - **Readers**, `Read + Seek` behind `&mut`, are an `io.ReadSeeker`, such as an `*os.File`, which Rust reads and seeks only during the call, one call at a time, and never closes. A read of bytes and an error hands Rust the bytes, then the error on the next read, as @@ -902,7 +923,9 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass with data a sealed class with a final class per variant (`EventMoved`), so a `switch` over it is exhaustive. `Display` is `toString()`, `Debug` `debug()`, `PartialEq` `==`, all through Rust's, with a `hashCode` that throws an `UnsupportedError`, since no hash Dart - computes agrees with Rust's `==`, and `Default` a constructor, `default_()`. A plain enum's `==` is Dart's, by its value. + computes agrees with Rust's `==`, and `Default` a constructor, `default_()`. An + **opaque value** is a final class of its bytes as `int`s, with a private constructor, + so only Rust makes one. A plain enum's `==` is Dart's, by its value. - **Integers** are `int`. A narrower one passed in throws a `RangeError` when out of its range, and a `u64` passes its 64 bits, so one past `i64`'s range is negative in Dart. A `char` is a `String` of one Unicode scalar. @@ -936,9 +959,8 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass A call that throws before Rust sees it gives Rust none of its implementations. - **Closures** are Dart functions, which Rust calls on the isolate's thread: one lent for a call only during the call, and one Rust keeps until it drops it, which then lets go of - it. An object it lends, by reference or a `Copy` one by value, lives only for that call - of the closure. What it returns Rust takes: a `String` copied, and a `Copy` object as a - new handle to a clone. One whose Rust returns `Result` fails by throwing + it. An object it lends by reference lives only for that call of the closure. What it + returns Rust takes: a `String` copied, and an opaque value copied. One whose Rust returns `Result` fails by throwing `StringException`, the binding's own form of that error; anything else it throws aborts. - A **reader** Rust reads and seeks, a `Read + Seek` behind `&mut`, is a `SeekableReader`: `readInto` and `seek`, which Rust calls only during the call, on the isolate's thread. diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index c10132fd2..77d8d3e7d 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -299,11 +299,13 @@ fn main() { println!("secs {}", stamp.secs()); println!("later {}", Stamp::from_millis(3000).later(stamp)); println!("secs {}", stamp.secs()); + let whole: Stamp = stamp.whole().into(); + println!("whole {whole:?}"); let mut message = Message::new(Some(stamp), Bytes::from_static(b"body")); println!("payload {}", String::from_utf8_lossy(&message.payload)); println!("stamp {:?}", message.stamp.unwrap()); println!("sent {}", message.sent()); - // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. + // Setters assign in place: the bytes are copied in, and a `Copy` value copied. let reset = Stamp::from_millis(4000); message.stamp = Some(reset); message.payload = Bytes::from_static(b"edited"); @@ -313,6 +315,32 @@ fn main() { message.stamp = None; println!("stamp {}", message.stamp.is_some()); + // A value past a word, copied across: a closure takes and returns one by value. + let moment = api::Moment::new(1500, 1000).unwrap(); + println!("moment {} {}", moment.millis(), api::Moment::new(1, 0).is_none()); + let shifted = api::shifted(moment, |m| m.after(500)); + println!("shifted {} {}", shifted.millis(), shifted == moment.after(500)); + println!("wrapped {} {}", moment.after(u64::MAX).millis(), moment == shifted); + + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + let gap = api::Gap::new(7, 3); + println!("gap {} {}", gap.sum(), gap == api::Gap::new(7, 3)); + let berth = api::Berth::new(Some(4)); + println!("berth {:?} {:?}", berth.at(), api::Berth::new(None).at()); + + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + let mut score = api::Score::of(2); + let step = api::Score::of(3); + score.add(step); + score.add(step); + println!("score {} {} {}", score.count(), step.doubled().count(), step.count()); + let weighted = api::Weighted { score: step, weight: 4 }; + println!( + "weighted {} {}", + weighted.total(), + weighted.score.count() + step.count() + ); + println!("kept {}", api::emit(Print, vec![1, 2, 3, 4])); let logger = Logger::new(Box::new(Print)); println!("write {}", logger.write("hi")); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index f7e2ed8a9..ada9b8843 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -111,6 +111,20 @@ pub fn reading_echo(reading: Reading) -> Reading { reading } +/// Declares `crowded`, whose parameters take 243 of a JVM method's 255 slots, so flattening +/// its `Reading` would take 15 more: a binding that counts a `Stamp` as one slot, not the +/// two of the `long` it crosses as, flattens it past the limit. +macro_rules! crowded { + ($($n:ident)*) => { + /// The sum of the reading's count, both stamps' millis, and every number. + pub fn crowded(reading: Reading, a: Stamp, b: Stamp, $($n: u64),*) -> u64 { + reading.count.wrapping_add(a.millis).wrapping_add(b.millis)$(.wrapping_add($n))* + } + }; +} + +crowded!(n0 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13 n14 n15 n16 n17 n18 n19 n20 n21 n22 n23 n24 n25 n26 n27 n28 n29 n30 n31 n32 n33 n34 n35 n36 n37 n38 n39 n40 n41 n42 n43 n44 n45 n46 n47 n48 n49 n50 n51 n52 n53 n54 n55 n56 n57 n58 n59 n60 n61 n62 n63 n64 n65 n66 n67 n68 n69 n70 n71 n72 n73 n74 n75 n76 n77 n78 n79 n80 n81 n82 n83 n84 n85 n86 n87 n88 n89 n90 n91 n92 n93 n94 n95 n96 n97 n98 n99 n100 n101 n102 n103 n104 n105 n106 n107 n108 n109 n110 n111 n112 n113 n114 n115 n116 n117 n118); + /// A plain enumeration. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Shape { @@ -751,7 +765,7 @@ pub fn made(n: u32, make: impl FnOnce(u32) -> R) -> R { /// How many times a [`Ticket`]'s `Clone` ran. static TICKET_CLONES: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); -/// A `Copy` object whose `Clone` counts its calls, which a closure taking or returning one +/// A `Copy` value whose `Clone` counts its calls, which a closure taking or returning one /// never runs, as Rust copies it instead. #[derive(Copy, Debug, PartialEq)] pub struct Ticket { @@ -1015,8 +1029,7 @@ impl Future for Within { } } -/// A time: an object, since its field is private, but `Copy`, so passing it by value -/// copies it rather than moving it out of its handle. +/// A time: `Copy` with a private field, so a value only Rust makes, copied across. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Stamp { millis: u64, @@ -1031,12 +1044,150 @@ impl Stamp { self.millis / 1000 } + /// It rounded down to whole seconds, through an `impl Trait`, so it crosses as its bytes. + pub fn whole(self) -> impl Into + use<> { + Self { + millis: self.millis / 1000 * 1000, + } + } + pub fn later(self, other: Stamp) -> bool { self.millis > other.millis } } -/// A stamp and a count: a record holding an object, whose method reads it. +/// A time on a clock of its own rate, as moq's `Timestamp`: `Copy` with private fields, past +/// a word on every target, and never all zero bytes, so only a value Rust made may cross. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Moment { + ticks: u64, + rate: std::num::NonZero, +} + +impl Moment { + /// `ticks` of `rate` a second, or none at a rate of zero. + pub fn new(ticks: u64, rate: u64) -> Option { + std::num::NonZero::new(rate).map(|rate| Self { ticks, rate }) + } + + /// Whole milliseconds since zero. + pub fn millis(&self) -> u64 { + (u128::from(self.ticks) * 1000 / u128::from(self.rate.get())) as u64 + } + + /// The moment `ticks` later, at the same rate, wrapping as `u64` does. + pub fn after(self, ticks: u64) -> Self { + Self { + ticks: self.ticks.wrapping_add(ticks), + ..self + } + } +} + +/// The moment `shift` returns for `moment`, which C copies in and out. +pub fn shifted(moment: Moment, shift: impl Fn(Moment) -> Moment) -> Moment { + shift(moment) +} + +/// A value with padding, after its `u8`: made from bytes all `0xff`, so its padding likely +/// holds them, which the export zeroes before any language sees it. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Gap { + wide: u64, + narrow: u8, +} + +impl Gap { + pub fn new(wide: u64, narrow: u8) -> Self { + // SAFETY: every byte pattern is a `Gap`, whose fields are integers. + let mut gap: Gap = unsafe { std::mem::transmute([0xffu8; std::mem::size_of::()]) }; + gap.wide = wide; + gap.narrow = narrow; + gap + } + + pub fn sum(&self) -> u64 { + self.wide.wrapping_add(u64::from(self.narrow)) + } +} + +/// A value named as Kotlin's `LongArray`, which holds a value's words, with a method named +/// as C++'s member holding its bytes: each generator keeps its own apart. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct LongArray { + words: [u64; 2], +} + +impl LongArray { + pub fn new(low: u64, high: u64) -> Self { + LongArray { words: [low, high] } + } + + pub fn c_(&self) -> u64 { + self.words[0].wrapping_add(self.words[1]) + } +} + +/// A `Copy` type whose `None` leaves its payload's bytes uninitialized, so no padding table +/// covers it: a handle, rather than a value. +#[derive(Clone, Copy, Debug)] +pub struct Berth { + at: Option, +} + +impl Berth { + pub fn new(at: Option) -> Self { + Self { at } + } + + pub fn at(&self) -> Option { + self.at + } +} + +/// A running count, as moq's `stats::Traffic`: `Copy` with private fields, but changed in +/// place through `&mut self`, which a copy could not write back, so a handle. A call taking +/// one by value copies it out of its handle, leaving the caller's as it was. +#[derive(Clone, Copy, Debug)] +pub struct Score { + count: u64, +} + +impl Score { + pub fn of(count: u64) -> Self { + Self { count } + } + + pub fn count(&self) -> u64 { + self.count + } + + /// Adds `other`'s count to this one, as Rust copies `other`. + pub fn add(&mut self, other: Score) { + self.count = self.count.wrapping_add(other.count); + } + + /// Twice the count, as Rust copies `self`. + pub fn doubled(self) -> Score { + Self { + count: self.count.wrapping_mul(2), + } + } +} + +/// A score and its weight: a record holding a `Copy` handle, copied in and out. +pub struct Weighted { + pub score: Score, + pub weight: u32, +} + +impl Weighted { + pub fn total(&self) -> u64 { + self.score.count.wrapping_mul(u64::from(self.weight)) + } +} + +/// A stamp and a count: a record holding a value only Rust makes, whose method reads it. pub struct Stamped { pub at: Stamp, pub count: u32, @@ -1049,7 +1200,8 @@ impl Stamped { } } -/// A message: an object, since one field is private, whose `pub` fields read as getters. +/// A message: an object, since one field is private and it is not `Copy`, whose `pub` +/// fields read as getters. #[derive(Clone, Debug)] pub struct Message { pub stamp: Option, diff --git a/ffi/fixtures/lock/expected.txt b/ffi/fixtures/lock/expected.txt index 97260a82d..5ac7bfb49 100644 --- a/ffi/fixtures/lock/expected.txt +++ b/ffi/fixtures/lock/expected.txt @@ -5,9 +5,11 @@ fixtures/lock/src/lib.rs:135:1: rust2-ffi: the API breaks the baseline at enum ` fixtures/lock/src/lib.rs:176:1: rust2-ffi: the API breaks the baseline at const `lock::DEPTH`: its type: it was `u32`, and is now `u64` fixtures/lock/src/lib.rs:182:1: rust2-ffi: the API breaks the baseline at const `lock::PAGES`: its type: it was `u16`, and is now `i16` fixtures/lock/src/lib.rs:191:1: rust2-ffi: the API breaks the baseline at tuple `((u32,), u64, u8)`: its C name `lock_tuple_tuple_u32_u64_u8` is now `lock_tuple_tuple_u32_u64_u8_`, and `lock_tuple_tuple_u32_u64_u8` names tuple `((u32, u64), u8)` +fixtures/lock/src/lib.rs:202:1: rust2-ffi: the API breaks the baseline at struct `lock::Tick`: its bytes: 4 aligned to 4, and now 8 aligned to 4 fixtures/lock/src/lib.rs:21:2: rust2-ffi: the API breaks the baseline at const `lock::limits_max::RATE`: its C name `LOCK_LIMITS_MAX_RATE` is now `LOCK_LIMITS_MAX_RATE_`, and `LOCK_LIMITS_MAX_RATE` names const `lock::limits::MAX_RATE` -fixtures/lock/src/lib.rs:308:1: rust2-ffi: the API breaks the baseline at enum `lock::Speed`: variant `Fast` is removed; variant `Quick` is added to an exhaustive enum +fixtures/lock/src/lib.rs:231:1: rust2-ffi: the API breaks the baseline at struct `lock::Beat`: how it is held: a word compared by its bits, and now bytes compared by Rust fixtures/lock/src/lib.rs:30:1: rust2-ffi: the API breaks the baseline at fn `lock::removed`: it is removed +fixtures/lock/src/lib.rs:356:1: rust2-ffi: the API breaks the baseline at enum `lock::Speed`: variant `Fast` is removed; variant `Quick` is added to an exhaustive enum fixtures/lock/src/lib.rs:36:1: rust2-ffi: the API breaks the baseline at fn `lock::portable`: it is no longer on wasm32-unknown-unknown fixtures/lock/src/lib.rs:46:1: rust2-ffi: the API breaks the baseline at fn `lock::scale`: its parameter `x`'s type; its return type fixtures/lock/src/lib.rs:66:2: rust2-ffi: the API breaks the baseline at fn `lock::Meter::add`: its parameter `n`'s type diff --git a/ffi/fixtures/lock/src/lib.rs b/ffi/fixtures/lock/src/lib.rs index 76c5be6d3..ceeca900d 100644 --- a/ffi/fixtures/lock/src/lib.rs +++ b/ffi/fixtures/lock/src/lib.rs @@ -197,6 +197,54 @@ pub fn flat(x: ((u32, u64), u8)) -> u32 { x.0.0 } +/// A `Copy` value C holds as its bytes, which `next` lengthens. +#[derive(Clone, Copy)] +pub struct Tick { + at: u32, + #[cfg(feature = "next")] + late: u32, +} + +impl Tick { + pub fn new(at: u32) -> Self { + Self { + at, + #[cfg(feature = "next")] + late: 0, + } + } + + pub fn at(&self) -> u32 { + self.at + } + + #[cfg(feature = "next")] + pub fn late(&self) -> u32 { + self.late + } +} + +/// A `Copy` value in a word, whose `PartialEq` `next` writes by hand, so Kotlin can no +/// longer compare its bits. +#[derive(Clone, Copy)] +#[cfg_attr(not(feature = "next"), derive(PartialEq))] +pub struct Beat { + at: u32, +} + +#[cfg(feature = "next")] +impl PartialEq for Beat { + fn eq(&self, other: &Self) -> bool { + self.at / 2 == other.at / 2 + } +} + +impl Beat { + pub fn new(at: u32) -> Self { + Self { at } + } +} + /// Kept class whose Pascal name collides only in the bindings. pub mod graphics { pub mod encoder { diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index 9ba588290..ac7449ee9 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -24,10 +24,14 @@ fixtures/reject/src/lib.rs:1119:1: rust2-ffi: cannot export fn `hazard`: trait o fixtures/reject/src/lib.rs:1109:1: note: C cannot implement `Hazard`: unsafe fn `risk` fixtures/reject/src/lib.rs:111:1: rust2-ffi: cannot export fn `boxed`: reference inside a parameter `std::boxed::Box<&str>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:1124:1: rust2-ffi: cannot export fn `wide_non_zero`: NonZero `u128`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1129:1: rust2-ffi: cannot export fn `hidden_answer`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1134:1: rust2-ffi: cannot export fn `hidden_items`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1139:1: rust2-ffi: cannot export fn `hidden_option`: opaque type `impl std::clone::Clone`; exclude it in the config to leave it out -fixtures/reject/src/lib.rs:1144:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1146:1: rust2-ffi: cannot export fn `wrap`: returned object that borrows `View<'_>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1151:1: rust2-ffi: cannot export fn `hidden_wrap`: returned object that borrows `impl std::clone::Clone + '_`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1167:1: rust2-ffi: cannot export fn `picked`: closure signature `&'static Mark`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1172:1: rust2-ffi: cannot export fn `maybe_picked`: closure signature `std::option::Option<&'static Mark>`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1177:1: rust2-ffi: cannot export fn `hidden_answer`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1182:1: rust2-ffi: cannot export fn `hidden_items`: opaque type `impl std::marker::Copy`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1187:1: rust2-ffi: cannot export fn `hidden_option`: opaque type `impl std::clone::Clone`; exclude it in the config to leave it out +fixtures/reject/src/lib.rs:1192:1: rust2-ffi: cannot export fn `boxed_bytes`: boxed slice of bytes `std::boxed::Box<[u8]>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:11: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:122:1: rust2-ffi: cannot export fn `parse_later`: returned object that borrows `impl std::future::Future>>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:136:1: rust2-ffi: cannot export fn `poll_ready`: Poll with no Context or waiter parameter `std::task::Poll`; 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 6e6b20a1b..a9791f914 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -1125,6 +1125,54 @@ pub fn wide_non_zero(n: std::num::NonZero) -> u128 { n.get() } +/// A `Copy` view of bytes it borrows, held across as its bytes. +#[derive(Clone, Copy)] +pub struct View<'a> { + bytes: &'a [u8], +} + +impl View<'_> { + pub fn len(&self) -> usize { + self.bytes.len() + } +} + +/// A view of nothing, which borrows nothing, so an export returns a `View`. +pub fn empty_view() -> View<'static> { + View { bytes: &[] } +} + +/// Returns a value that borrows its parameter, which C could keep past the borrow. +pub fn wrap(bytes: &[u8]) -> View<'_> { + View { bytes } +} + +/// The same, through an `impl Trait` hiding the value, which holds what it captures. +pub fn hidden_wrap(bytes: &[u8]) -> impl Clone + '_ { + View { bytes } +} + +/// A `Copy` mark held across as its bytes. +#[derive(Clone, Copy)] +pub struct Mark { + at: u64, +} + +/// A mark at `at`, so an export returns one. +pub fn mark(at: u64) -> Mark { + Mark { at } +} + +/// A closure returning a borrowed value, which C has no reference to hand back. +pub fn picked(pick: impl Fn() -> &'static Mark) -> u64 { + pick().at +} + +/// The same, in an `Option`. +pub fn maybe_picked(pick: impl Fn() -> Option<&'static Mark>) -> u64 { + pick().map_or(0, |mark| mark.at) +} + /// A future whose output is an `impl Trait` hiding no object, which the glue cannot name. pub async fn hidden_answer() -> impl Copy { 1_u32 diff --git a/ffi/moq/bench/export.c b/ffi/moq/bench/export.c index 268c54a95..5c91991a5 100644 --- a/ffi/moq/bench/export.c +++ b/ffi/moq/bench/export.c @@ -85,9 +85,9 @@ static moq_group_Producer *append_group(moq_track_Producer *track) { return group; } -static void write_frame(moq_group_Producer *group, const moq_Timestamp *at, moq_buf frame) { +static void write_frame(moq_group_Producer *group, moq_Timestamp at, moq_buf frame) { moq_Error *err = NULL; - if (!moq_group_Producer_write_frame(group, at, frame, &err)) { + if (!moq_group_Producer_write_frame(group, (moq_option_Timestamp){.some = true, .value = at}, frame, &err)) { fail("write_frame"); } } @@ -102,7 +102,7 @@ static void finish(moq_group_Producer *group) { /* Groups of 100 frames of `size` bytes written and each read back, its payload's first byte * touched, in about 64 MiB and at least 300 frames: megabytes a wall and a CPU second. */ -static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscriber, const moq_Timestamp *at, size_t size, unsigned *sum) { +static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscriber, moq_Timestamp at, size_t size, unsigned *sum) { size_t groups = (64u << 20) / (size * FRAMES); groups = groups < 3 ? 3 : groups > 2000 ? 2000 : groups; uint8_t *payload = calloc(size, 1); @@ -131,7 +131,7 @@ static void round_(int print) { moq_broadcast_Info *binfo = moq_broadcast_Info_new(); moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(binfo); moq_broadcast_Info_free(binfo); - moq_Timescale *micro = moq_Timescale_MICRO(); + moq_option_Timescale micro = {.some = true, .value = moq_Timescale_MICRO()}; /* Builders take the handle's value, leaving the handle to free. */ moq_track_Info *defaults = moq_track_Info_default(); moq_track_Info *info = moq_track_Info_with_timescale(defaults, micro); @@ -176,26 +176,25 @@ static void round_(int print) { } span object = per(start, N); - /* Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. */ + /* Each frame stamped through a fresh timestamp, as a caller holding a u64 must. */ uint64_t micros = 0; start = now(); for (int g = 0; g < GROUPS; g++) { moq_group_Producer *group = append_group(track); for (int f = 0; f < FRAMES; f++) { - moq_Timestamp *at = NULL; - moq_TimeOverflow *overflow = NULL; + moq_Timestamp at; + moq_TimeOverflow overflow; if (!moq_Timestamp_from_micros(micros++, &at, &overflow)) { fail("from_micros"); } write_frame(group, at, frame); - moq_Timestamp_free(at); } finish(group); } span stamped = per(start, N); - moq_Timestamp *at = NULL; - moq_TimeOverflow *overflow = NULL; + moq_Timestamp at; + moq_TimeOverflow overflow; if (!moq_Timestamp_from_micros(micros, &at, &overflow)) { fail("from_micros"); } @@ -275,10 +274,8 @@ static void round_(int print) { } sink = sum; - moq_Timestamp_free(at); moq_track_Subscriber_free(subscriber); moq_track_Producer_free(track); - moq_Timescale_free(micro); moq_broadcast_Producer_free(broadcast); if (print) { diff --git a/ffi/moq/bench/export.cpp b/ffi/moq/bench/export.cpp index bba2680ee..ba37a0727 100644 --- a/ffi/moq/bench/export.cpp +++ b/ffi/moq/bench/export.cpp @@ -82,7 +82,7 @@ auto ready(F future, const char *what) { // Groups of 100 frames of `size` bytes written and each read back, its payload's first byte // touched, in about 64 MiB and at least 300 frames: megabytes a wall and a CPU second. -span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, const moq::Timestamp &at, std::size_t size, unsigned &sum) { +span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, moq::Timestamp at, std::size_t size, unsigned &sum) { std::size_t count = std::clamp((64u << 20) / (size * frames), 3, 2000); std::vector payload(size); auto start = now(); @@ -90,7 +90,7 @@ span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, c auto group = must(track.append_group(), "append_group"); auto reader = ready(subscriber.recv_group(), "recv_group"); for (int f = 0; f < frames; f++) { - must(group.write_frame(&at, payload), "write_frame"); + must(group.write_frame(at, payload), "write_frame"); auto read = ready(reader.read_frame(), "read_frame"); sum += read.payload_get().data()[0]; } @@ -103,7 +103,7 @@ span throughput(moq::track_Producer &track, moq::track_Subscriber &subscriber, c void round(bool print) { moq::broadcast_Producer broadcast = moq::broadcast_Info::new_().produce(); moq::Timescale micro = moq::Timescale::MICRO(); - auto track = must(broadcast.create_track("bench", moq::track_Info::default_().with_timescale(µ)), "create_track"); + auto track = must(broadcast.create_track("bench", moq::track_Info::default_().with_timescale(micro)), "create_track"); std::uint8_t bytes[100] = {0}; moq::view frame(bytes, sizeof bytes); @@ -139,14 +139,14 @@ void round(bool print) { } span object = per(start); - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. std::uint64_t micros = 0; start = now(); for (int g = 0; g < groups; g++) { auto group = must(track.append_group(), "append_group"); for (int f = 0; f < frames; f++) { auto at = must(moq::Timestamp::from_micros(micros++), "from_micros"); - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); } must(std::move(group).finish(), "finish"); } @@ -157,7 +157,7 @@ void round(bool print) { for (int g = 0; g < groups; g++) { auto group = must(track.append_group(), "append_group"); for (int f = 0; f < frames; f++) { - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); } must(std::move(group).finish(), "finish"); } @@ -172,7 +172,7 @@ void round(bool print) { auto group = must(track.append_group(), "append_group"); auto reader = ready(subscriber.recv_group(), "recv_group"); for (int f = 0; f < frames; f++) { - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); last = ready(reader.read_frame(), "read_frame"); } must(std::move(group).finish(), "finish"); @@ -190,7 +190,7 @@ void round(bool print) { std::fprintf(stderr, "bench: woken read_frame ready\n"); std::exit(1); } - must(group.write_frame(&at, frame), "write_frame"); + must(group.write_frame(at, frame), "write_frame"); (void)must(must(must(reading.poll(noop()), "woken read_frame"), "woken read_frame"), "woken read_frame"); } must(std::move(group).finish(), "finish"); diff --git a/ffi/moq/bench/export.dart b/ffi/moq/bench/export.dart index 0b9d85185..456fedc08 100644 --- a/ffi/moq/bench/export.dart +++ b/ffi/moq/bench/export.dart @@ -74,9 +74,7 @@ Future throughput(moq.TrackProducer track, moq.TrackSubscriber subscriber, Future> round() async { final out = {}; final broadcast = moq.BroadcastInfo().produce(); - final micro = moq.Timescale.MICRO; - final track = broadcast.createTrack('bench', moq.TrackInfo.default_().withTimescale(micro)); - micro.close(); + final track = broadcast.createTrack('bench', moq.TrackInfo.default_().withTimescale(moq.Timescale.MICRO)); final frame = Uint8List(100); var start = now(); @@ -108,15 +106,13 @@ Future> round() async { } out['object'] = per(start); - // Each frame stamped through a fresh timestamp object, as a caller holding an int must. + // Each frame stamped through a fresh timestamp, as a caller holding an int must. var micros = 0; start = now(); for (var g = 0; g < groups; g++) { final group = track.appendGroup(); for (var f = 0; f < frames; f++) { - final at = moq.Timestamp.fromMicros(micros++); - group.writeFrame(at, frame); - at.close(); + group.writeFrame(moq.Timestamp.fromMicros(micros++), frame); } group.finish(); group.close(); @@ -217,7 +213,6 @@ Future> round() async { } out['payload'] = per(start); last!.close(); - at.close(); track.close(); broadcast.close(); return out; diff --git a/ffi/moq/bench/export.go b/ffi/moq/bench/export.go index 360139300..5caa77e27 100644 --- a/ffi/moq/bench/export.go +++ b/ffi/moq/bench/export.go @@ -111,8 +111,7 @@ func round() []result { var out []result broadcast := must(net.NewBroadcastInfo().Produce()) micro := net.TimescaleMICRO() - track := must(broadcast.CreateTrack("bench", must(net.TrackInfoDefault().WithTimescale(micro)))) - micro.Close() + track := must(broadcast.CreateTrack("bench", must(net.TrackInfoDefault().WithTimescale(µ)))) frame := rt.NewBytes(make([]byte, 100)) start := now() @@ -148,7 +147,7 @@ func round() []result { } out = append(out, result{"object", per(start, n)}) - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. var micros uint64 start = now() for range groups { @@ -156,8 +155,7 @@ func round() []result { for range frames { at := must(net.TimestampFromMicros(micros)) micros++ - check(group.WriteFrame(at, frame)) - at.Close() + check(group.WriteFrame(&at, frame)) } check(group.Finish()) group.Close() @@ -169,7 +167,7 @@ func round() []result { for range groups { group := must(track.AppendGroup()) for range frames { - check(group.WriteFrame(at, frame)) + check(group.WriteFrame(&at, frame)) } check(group.Finish()) group.Close() @@ -189,7 +187,7 @@ func round() []result { group := must(track.AppendGroup()) reader := must(subscriber.RecvGroup(ctx)) for range frames { - check(group.WriteFrame(at, frame)) + check(group.WriteFrame(&at, frame)) if last != nil { last.Close() } @@ -211,7 +209,7 @@ func round() []result { for range frames { go func() { read <- must(reader.ReadFrame(ctx)) }() runtime.Gosched() - check(group.WriteFrame(at, frame)) + check(group.WriteFrame(&at, frame)) (<-read).Close() } reader.Close() @@ -254,7 +252,7 @@ func round() []result { out = append(out, result{"loop", per(start, n)}) for _, size := range sizes { - out = append(out, result{fmt.Sprintf("mbps:%d", size), throughput(ctx, track, subscriber, at, size)}) + out = append(out, result{fmt.Sprintf("mbps:%d", size), throughput(ctx, track, subscriber, &at, size)}) } subscriber.Close() @@ -265,7 +263,6 @@ func round() []result { } out = append(out, result{"payload", per(start, n)}) last.Close() - at.Close() track.Close() broadcast.Close() return out diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index 85fdc2f61..8bc5f4f13 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -58,9 +58,7 @@ suspend fun throughput(track: TrackProducer, subscriber: TrackSubscriber, at: Ti fun round(): Map { val out = linkedMapOf() val broadcast = BroadcastInfo.new().produce() - val track = Timescale.MICRO.use { micro -> - broadcast.createTrack("bench", TrackInfo.default().withTimescale(micro)) - } + val track = broadcast.createTrack("bench", TrackInfo.default().withTimescale(Timescale.MICRO)) val frame = Bytes(ByteArray(100)) var sum = 0 @@ -94,13 +92,13 @@ fun round(): Map { failures.peek()?.let { throw it } out["objects"] = per(start) - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. var micros = 0uL start = now() repeat(GROUPS) { track.appendGroup().use { group -> repeat(FRAMES) { - Timestamp.fromMicros(micros++).use { at -> group.writeFrame(at, frame) } + group.writeFrame(Timestamp.fromMicros(micros++), frame) } group.finish() } @@ -193,7 +191,6 @@ fun round(): Map { repeat(N) { sum += read.payloadGet[0] } } out["payload"] = per(start) - at.close() track.close() broadcast.close() sink += sum diff --git a/ffi/moq/bench/export.py b/ffi/moq/bench/export.py index 0605ad1d7..0b1910a24 100644 --- a/ffi/moq/bench/export.py +++ b/ffi/moq/bench/export.py @@ -77,7 +77,7 @@ def round(loop): track.info() out["object"] = per(start) - # Each frame stamped through a fresh timestamp object, as a caller holding an int must. + # Each frame stamped through a fresh timestamp, as a caller holding an int must. micros = 0 start = now() for _ in range(GROUPS): diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index 37667ef0b..da349fe04 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -136,7 +136,7 @@ func round() throws -> ([(String, Span)], Int) { } out.append(("object", per(start))) - // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. + // Each frame stamped through a fresh timestamp, as a caller holding a u64 must. var micros: UInt64 = 0 start = now() for _ in 0.. before); now = moq_clock_now(); - deadline = NULL; + deadline = (moq_option_clock_Instant){0}; err = NULL; CHECK(!moq_origin_Driver_poll(driver, now, waiter, &deadline, &err)); - CHECK(deadline == NULL && err != NULL && is(moq_Error_to_string(err), "closed")); + CHECK(!deadline.some && err != NULL && is(moq_Error_to_string(err), "closed")); moq_Error_free(err); err = NULL; - moq_clock_Instant_free(now); moq_Waiter_free(waiter); moq_origin_Driver_free(driver); @@ -422,9 +416,8 @@ int main(void) { /* A JSON track the catalog lists, published through moq-mux's generic `json_snapshot` * at a JSON document, and a section C parses from text, set in the lent catalog and * read back as text. Its track has a timescale, since every write is stamped. */ - moq_Timescale *milli = moq_Timescale_MILLI(); - moq_track_Info *status_info = moq_track_Info_with_timescale(moq_track_Info_default(), milli); - moq_Timescale_free(milli); + moq_track_Info *status_info = + moq_track_Info_with_timescale(moq_track_Info_default(), (moq_option_Timescale){.some = true, .value = moq_Timescale_MILLI()}); moq_track_Producer *status = NULL; CHECK(moq_broadcast_Producer_create_track(publisher, str("status"), status_info, &status, &err)); moq_mux_json_Config *json_config = moq_mux_json_Config_default(); @@ -442,9 +435,7 @@ int main(void) { moq_Json *latest = NULL; moq_buf json_err; CHECK(moq_Json_parse(str("{\"on\": true}"), &latest, &json_err)); - moq_Timestamp *stamp = moq_Timestamp_now(); - CHECK(moq_mux_json_Snapshot_Json_update(status_snapshot, latest, stamp, &mux_err)); - moq_Timestamp_free(stamp); + CHECK(moq_mux_json_Snapshot_Json_update(status_snapshot, latest, moq_Timestamp_now(), &mux_err)); moq_Json_free(latest); moq_track_Subscriber *status_subscriber = moq_mux_json_Snapshot_Json_consume(status_snapshot); moq_track_Subscriber_recv_group_future *status_receiving = moq_track_Subscriber_recv_group(status_subscriber); @@ -551,7 +542,7 @@ int main(void) { /* A backoff's fields are settings left unset, and the methods of their names resolve them. */ moq_tokio_Backoff *backoff = moq_tokio_Backoff_default(); - CHECK(moq_tokio_Backoff_initial_get(backoff) == NULL); + CHECK(!moq_tokio_Backoff_initial_get(backoff).some); moq_duration initial = moq_tokio_Backoff_initial(backoff); CHECK(initial.secs == 1 && initial.nanos == 0); moq_tokio_Backoff_free(backoff); @@ -601,7 +592,7 @@ int main(void) { CHECK(moq_broadcast_Producer_create_track(live_broadcast, str("video"), NULL, &live_track, &err)); moq_group_Producer *live_group = NULL; CHECK(moq_track_Producer_append_group(live_track, &live_group, &err)); - CHECK(moq_group_Producer_write_frame(live_group, NULL, str("hello"), &err)); + CHECK(moq_group_Producer_write_frame(live_group, (moq_option_Timestamp){0}, str("hello"), &err)); CHECK(moq_group_Producer_finish(live_group, &err)); moq_group_Producer_free(live_group); diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index 2f948eaf8..7d699d24a 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -149,7 +149,7 @@ int main() { { auto reading = group.read_frame(); CHECK(!reading.poll(waker)); - CHECK(writer->write_frame(nullptr, "hello")); + CHECK(writer->write_frame(std::nullopt, "hello")); CHECK(wakes == 2); auto frame = reading.poll(waker); CHECK(frame && *frame && frame->value()); @@ -160,17 +160,17 @@ int main() { CHECK(moq::clock_now().checked_duration_since(hello.arrived()).has_value()); } - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. auto at = moq::Timestamp::from_millis(1500); CHECK(at && at->as_secs() == 1); - auto late = writer->write_frame(&*at, "late"); + auto late = writer->write_frame(*at, "late"); CHECK(!late && is(late.error().to_string(), "frame timestamp doesn't fit its track")); CHECK(at->as_secs() == 1); // The last frame, bytes that are not UTF-8, then the group's end, which reads as None, // both blocking. - CHECK(writer->write_frame(nullptr, "\xffworld")); + CHECK(writer->write_frame(std::nullopt, "\xffworld")); CHECK(std::move(*writer).finish()); auto world = group.read_frame().get(); CHECK(world && world.value() && is(world.value()->payload_get(), "\xffworld")); @@ -300,7 +300,7 @@ int main() { // lent catalog and read back as text. // Its track has a timescale, since every write is stamped. auto milli = moq::Timescale::MILLI(); - auto status = publisher.create_track("status", moq::track_Info::default_().with_timescale(&milli)); + auto status = publisher.create_track("status", moq::track_Info::default_().with_timescale(milli)); CHECK(status); auto status_snapshot = catalog->json_snapshot_Config(std::move(*status), moq::mux_json_Config::default_(), moq::mux_json_Options::default_()); @@ -389,7 +389,7 @@ int main() { auto live_track = live_broadcast.create_track("video", std::nullopt); CHECK(live_track); auto live_group = live_track->append_group(); - CHECK(live_group && live_group->write_frame(nullptr, "hello")); + CHECK(live_group && live_group->write_frame(std::nullopt, "hello")); CHECK(std::move(*live_group).finish()); moq::tokio_server_Config server_config = moq::tokio_server_Config::default_(); diff --git a/ffi/moq/moq.dart b/ffi/moq/moq.dart index cd00edf57..621588ed7 100644 --- a/ffi/moq/moq.dart +++ b/ffi/moq/moq.dart @@ -57,7 +57,6 @@ Future drive(moq.OriginDriver driver) { final next = driver.poll(moq.clockNow(), waiter); if (next != null) { deadline = Timer(next.saturatingDurationSince(moq.clockNow()), poll); - next.close(); } } on moq.Error catch (e) { waiter.close(); @@ -116,7 +115,7 @@ Future local() async { // When it arrived: kio's clock's `Instant`, from kio's own export. expect(moq.clockNow().checkedDurationSince(hello.arrived()) != null, 'a frame arrived before now'); - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. final at = moq.Timestamp.fromMillis(1500); expect(at.asSecs() == 1, '1500 ms is 1 s'); diff --git a/ffi/moq/moq.go b/ffi/moq/moq.go index b697452bf..1a062c8bd 100644 --- a/ffi/moq/moq.go +++ b/ffi/moq/moq.go @@ -114,8 +114,7 @@ func drive(driver *net.OriginDriver) <-chan error { } var deadline <-chan time.Time if next != nil { - deadline = time.After(must(next.SaturatingDurationSince(kio.ClockNow()))) - next.Close() + deadline = time.After(next.SaturatingDurationSince(kio.ClockNow())) } select { case <-woken: @@ -157,15 +156,15 @@ func script() { expect(string(must(hello.PayloadGet()).Copy()) == "hello", "a frame has its payload") expect(must(hello.TimestampGet()) == nil, "an untimed frame has no timestamp") // When it arrived: kio's clock's `Instant`, from kio's own export. - expect(must(kio.ClockNow().CheckedDurationSince(must(hello.Arrived()))) != nil, "a frame arrived before now") + expect(kio.ClockNow().CheckedDurationSince(must(hello.Arrived())) != nil, "a frame arrived before now") - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value Go copies, which allocates nothing. The track is // untimed, so a timestamped frame is refused. at := must(net.TimestampFromMillis(1500)) - expect(must(at.AsSecs()) == 1, "1500 ms is 1 s") - err = writer.WriteFrame(at, rt.NewBytes([]byte("late"))) + expect(at.AsSecs() == 1, "1500 ms is 1 s") + err = writer.WriteFrame(&at, rt.NewBytes([]byte("late"))) expect(errors.As(err, &merr) && err.Error() == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") - expect(must(at.AsSecs()) == 1, "a copied timestamp stays usable") + expect(at.AsSecs() == 1, "a copied timestamp stays usable") // The last frame, bytes that are not UTF-8, then the group's end, which reads as nil. world := append([]byte{0xff}, "world"...) @@ -252,7 +251,7 @@ func script() { }) ranges := []net.TrackRange{net.TrackRangeLive(), groups} ranged := must(must(net.TrackSubscriptionDefault().WithRanges(ranges)).RangesGet()) - expect(slices.EqualFunc(ranged, ranges, func(a, b net.TrackRange) bool { return must(a.Equal(b)) }), "a subscription keeps its ranges") + expect(slices.EqualFunc(ranged, ranges, func(a, b net.TrackRange) bool { return a.Equal(b) }), "a subscription keeps its ranges") expect(groups.Start == net.TrackAnchorAbsolute{V0: 2}, "groups start at their first") expect(groups.End != nil && *groups.End == net.TrackAnchorAbsolute{V0: 6}, "groups end before their end") @@ -274,7 +273,8 @@ func script() { // A JSON track the catalog lists, published through moq-mux's generic `json_snapshot` at // a JSON document, and a section Go parses from text, set in the lent catalog and read // back as text. - status := must(catalog.JsonSnapshotConfig(must(publisher.CreateTrack("status", must(net.TrackInfoDefault().WithTimescale(net.TimescaleMILLI())))), mux.JsonConfigDefault(), mux.JsonOptionsDefault())) + milli := net.TimescaleMILLI() + status := must(catalog.JsonSnapshotConfig(must(publisher.CreateTrack("status", must(net.TrackInfoDefault().WithTimescale(&milli)))), mux.JsonConfigDefault(), mux.JsonOptionsDefault())) expect(must(status.Name()) == "status", "a JSON snapshot has its track's name") expect(must(catalog.IsClaimedJsonConfig("status")), "the JSON snapshot claims its name") // A value Go parses is published as the track's latest, which a subscriber reads back diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 1b95b6a37..fcc1e23e8 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -89,7 +89,7 @@ fun main() { // When it arrived: kio's clock's `Instant`, from kio's own export. expect(clockNow().checkedDurationSince(hello.arrived()) != null, "a frame arrived before now") - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. val at = Timestamp.fromMillis(1500u) expect(at.asSecs() == 1uL, "1500 ms is 1 s") diff --git a/ffi/moq/moq.py b/ffi/moq/moq.py index d23540460..5162ca2c4 100644 --- a/ffi/moq/moq.py +++ b/ffi/moq/moq.py @@ -83,7 +83,7 @@ def local(loop): # When it arrived: kio's clock's `Instant`, from kio's own export. expect(moq.clock_now().checked_duration_since(hello.arrived()) is not None, "a frame arrived before now") - # A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + # A timestamp is a `Copy` value copied across, which allocates nothing. The track is # untimed, so a timestamped frame is refused. at = moq.Timestamp.from_millis(1500) expect(at.as_secs() == 1, "1500 ms is 1 s") diff --git a/ffi/moq/moq.swift b/ffi/moq/moq.swift index 0c233844c..2ebd8279e 100644 --- a/ffi/moq/moq.swift +++ b/ffi/moq/moq.swift @@ -252,7 +252,7 @@ func script() throws { // When it arrived: kio's clock's `Instant`, from kio's own export. expect(clockNow().checkedDurationSince(hello.arrived()) != nil, "a frame arrived before now") - // A timestamp is a `Copy` value behind a handle: passing it copies it out. The track is + // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. let at = try Timestamp.fromMillis(1500) expect(at.asSecs() == 1, "1500 ms is 1 s") diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index 4554a5d26..4559ff4eb 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -13,6 +13,7 @@ use std::cell::Cell; use std::ffi::{CStr, c_void}; +use std::mem::MaybeUninit; use std::sync::OnceLock; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -327,16 +328,26 @@ pub unsafe fn passed(env: *mut JNIEnv, held: jobject, owner: jlong, start: jint, /// A new `byte[]` holding `bytes`. pub fn array(env: *mut JNIEnv, bytes: &[u8]) -> jbyteArray { - let Ok(len) = jsize::try_from(bytes.len()) else { + // SAFETY: `bytes` holds its length in bytes. + unsafe { raw_array(env, bytes.as_ptr(), bytes.len()) } +} + +/// A new `byte[]` of the `len` bytes at `bytes`, which JNI copies as they are, so any may +/// be uninitialized. +/// +/// # Safety +/// `bytes` reaches `len` bytes. +unsafe fn raw_array(env: *mut JNIEnv, bytes: *const u8, len: usize) -> jbyteArray { + let Ok(len) = jsize::try_from(len) else { rt::fail("jni", "a buffer is too long for a byte[]") }; - // SAFETY: `env` is this thread's, and `bytes` holds `len` bytes. + // SAFETY: `env` is this thread's, and `bytes` reaches `len` bytes. unsafe { let array = ((**env).v1_2.NewByteArray)(env, len); if array.is_null() { fail(env, "jni", "a byte[] is out of memory"); } - ((**env).v1_2.SetByteArrayRegion)(env, array, 0, len, bytes.as_ptr().cast()); + ((**env).v1_2.SetByteArrayRegion)(env, array, 0, len, bytes.cast()); array } } @@ -912,11 +923,12 @@ impl<'a> Reader<'a> { thread_local! { /// The native codec buffer, kept between calls and lent only while a return factory runs. - static SCRATCH: Cell> = const { Cell::new(Vec::new()) }; + static SCRATCH: Cell>> = const { Cell::new(Vec::new()) }; } -/// Values encoded into a `byte[]` for Kotlin. -pub struct Writer(Vec); +/// Values encoded into a `byte[]` for Kotlin. Its bytes may be uninitialized, as an opaque +/// value's padding is, so it holds them as such and hands JNI them by pointer. +pub struct Writer(Vec>); impl Default for Writer { fn default() -> Self { @@ -941,7 +953,13 @@ impl Writer { } pub fn array(self, env: *mut JNIEnv) -> jbyteArray { - array(env, &self.0) + // SAFETY: the vector holds its length in bytes, which JNI copies as they are. + unsafe { raw_array(env, self.0.as_ptr().cast(), self.0.len()) } + } + + fn extend(&mut self, bytes: &[u8]) { + // SAFETY: a `u8` is a `MaybeUninit`. + self.0.extend_from_slice(unsafe { &*(std::ptr::from_ref(bytes) as *const [MaybeUninit]) }); } /// Construct a Kotlin value while this writer's native buffer is alive. The generated @@ -966,6 +984,93 @@ impl Writer { } } +/// An opaque value's C struct from the `long` Kotlin holds it in: its bytes, at the word's +/// lowest addresses, as [`to_word`] put them. +pub fn from_word(word: jlong) -> T { + const { assert!(size_of::() <= size_of::()) }; + let mut value = std::mem::MaybeUninit::::zeroed(); + // SAFETY: `T` is a struct of `MaybeUninit` units, any bytes of which are one. + unsafe { + std::ptr::copy_nonoverlapping((&raw const word).cast::(), value.as_mut_ptr().cast::(), size_of::()); + value.assume_init() + } +} + +/// The `long` Kotlin holds an opaque value's C struct in, which has no padding: Kotlin +/// compares two by it. +pub fn to_word(value: T) -> jlong { + const { assert!(size_of::() <= size_of::()) }; + let mut word: jlong = 0; + // SAFETY: every byte of a value with no padding is initialized, and the rest are zero. + unsafe { std::ptr::copy_nonoverlapping((&raw const value).cast::(), (&raw mut word).cast::(), size_of::()) }; + word +} + +/// An opaque value's C struct from the `W` words of the `long[]` Kotlin holds it in. +/// +/// # Safety +/// `array` is a live `long[]`. +pub unsafe fn from_words(env: *mut JNIEnv, array: jlongArray) -> T { + const { assert!(size_of::() <= W * size_of::()) }; + let mut words = [std::mem::MaybeUninit::::zeroed(); W]; + // SAFETY: `array` is live, and `T` is a struct of `MaybeUninit` units. + unsafe { + if array.is_null() || ((**env).v1_2.GetArrayLength)(env, array) != W as jsize { + fail(env, "jni", "an opaque value's words are not its own"); + } + ((**env).v1_2.GetLongArrayRegion)(env, array, 0, W as jsize, words.as_mut_ptr().cast()); + let mut value = std::mem::MaybeUninit::::zeroed(); + std::ptr::copy_nonoverlapping(words.as_ptr().cast::(), value.as_mut_ptr().cast::(), size_of::()); + value.assume_init() + } +} + +/// A new `long[]` of the `W` words an opaque value's C struct is held in: its bytes, then +/// zeros. Its padding crosses as the bytes it holds, which Kotlin never reads. +pub fn to_words(env: *mut JNIEnv, value: T) -> jlongArray { + const { assert!(size_of::() <= W * size_of::()) }; + let mut words = [std::mem::MaybeUninit::::zeroed(); W]; + // SAFETY: `env` is this thread's; JNI copies the words as bytes. + unsafe { + std::ptr::copy_nonoverlapping((&raw const value).cast::(), words.as_mut_ptr().cast::(), size_of::()); + let array = ((**env).v1_2.NewLongArray)(env, W as jsize); + if array.is_null() { + fail(env, "jni", "a long[] is out of memory"); + } + ((**env).v1_2.SetLongArrayRegion)(env, array, 0, W as jsize, words.as_ptr().cast()); + array + } +} + +/// An opaque value's C struct as it crosses encoded: the `W` words Kotlin holds it in, +/// whose little-endian bytes are its own on a little-endian target. +pub fn read_words(r: &mut Reader<'_>) -> T { + const { assert!(size_of::() <= W * size_of::() && cfg!(target_endian = "little")) }; + let bytes = r.take(W * size_of::()); + let mut value = std::mem::MaybeUninit::::zeroed(); + // SAFETY: `T` is a struct of `MaybeUninit` units, any bytes of which are one. + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr(), value.as_mut_ptr().cast::(), size_of::()); + value.assume_init() + } +} + +/// Encode an opaque value's C struct as the `W` words Kotlin holds it in: its bytes, then +/// zeros. Its padding stays uninitialized, as the writer allows, and Kotlin never reads it. +pub fn write_words(w: &mut Writer, value: T) { + const { assert!(size_of::() <= W * size_of::() && cfg!(target_endian = "little")) }; + let n = W * size_of::(); + w.0.reserve(n); + let start = w.0.len(); + // SAFETY: the vector has room for `n` more bytes, each written before its length grows. + unsafe { + let at = w.0.as_mut_ptr().add(start).cast::(); + std::ptr::write_bytes(at, 0, n); + std::ptr::copy_nonoverlapping((&raw const value).cast::(), at, size_of::()); + w.0.set_len(start + n); + } +} + /// A C value as it crosses encoded: Kotlin writes one Rust reads, and Rust writes one it /// owns, freeing what it holds once copied. pub trait Wire: Sized { @@ -981,7 +1086,7 @@ macro_rules! scalar { } fn write(self, w: &mut Writer) { - w.0.extend_from_slice(&self.to_le_bytes()); + w.extend(&self.to_le_bytes()); } } )*}; @@ -995,7 +1100,7 @@ impl Wire for [u8; N] { } fn write(self, w: &mut Writer) { - w.0.extend_from_slice(&self); + w.extend(&self); } } @@ -1120,7 +1225,7 @@ impl Wire for rt::Buf { rt::fail("jni", "a buffer is too long for a byte[]") }; w.put(len); - w.0.extend_from_slice(bytes); + w.extend(bytes); // SAFETY: as above. unsafe { self.free() }; } @@ -1163,7 +1268,7 @@ impl Wire for Owned { w.put(len); // SAFETY: a buffer Rust returned or lent, freed here once copied. unsafe { - w.0.extend_from_slice(buf.bytes("jni")); + w.extend(buf.bytes("jni")); buf.free(); } } diff --git a/ffi/runtime/native.py b/ffi/runtime/native.py index 019e708c3..4194d37fe 100644 --- a/ffi/runtime/native.py +++ b/ffi/runtime/native.py @@ -9,6 +9,9 @@ class _n: class Object: __slots__ = () + class Value: + __slots__ = () + class Future: __slots__ = () diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 2dcc8ab5e..02be9650e 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -1015,6 +1015,46 @@ pub fn array_out(items: &[u8], index: usize) -> R { call_py(ty(index), vec![bytes]) } +/// A `Value`'s header, before its bytes. +#[repr(C)] +struct ValueHead { + ob_base: PyVarObject, + /// The class registered at this index made it: Python can reassign `__class__`, to + /// any class of `Value`'s layout, whose bytes may be fewer or another type's. + index: usize, +} + +/// The `Copy` value a `Value` of the class registered at `index` holds, which only Rust +/// made: Python makes none, as `Value` disallows it. +pub fn value_in(obj: *mut PyObject, index: usize) -> R { + if !is(obj, index) { + return expected(obj, index); + } + // SAFETY: the GIL is held, and `obj` a `Value`; once its index matches, one + // `value_out` made of a `T`, which its class always holds; `T` is a struct of + // `MaybeUninit` units. + unsafe { + if (*obj.cast::()).index != index { + return expected(obj, index); + } + Ok(obj.cast::().add(size_of::()).cast::().read_unaligned()) + } +} + +/// A new `Value` of the class registered at `index`, holding `value` in place. +pub fn value_out(index: usize, value: T) -> R { + let class = ty(index); + // SAFETY: the GIL is held, `class` subclasses `Value`, whose items are its bytes, and + // the object is `size_of::()` of them past its header. + unsafe { + let alloc: allocfunc = std::mem::transmute(PyType_GetSlot(class.cast(), Py_tp_alloc)); + let obj = Obj::new(alloc(class.cast(), size_of::() as Py_ssize_t))?; + (*obj.ptr().cast::()).index = index; + obj.ptr().cast::().add(size_of::()).cast::().write_unaligned(value); + Ok(obj) + } +} + /// Bytes Rust lends a callback for its call, copied into `bytes`. pub fn lent_bytes(buf: rt::Buf) -> R { // SAFETY: the GIL is held, and a buffer Rust lent for the call. @@ -1616,6 +1656,8 @@ pub unsafe extern "C" fn free_callback(data: *mut c_void) { #[repr(C)] pub struct Handle { ob_base: PyObject, + /// The class registered at this index made it: Python can reassign `__class__`. + index: usize, raw: *const c_void, /// What frees the handle: none for one Rust lends, or once freed. free: Option, @@ -1658,6 +1700,7 @@ fn handle(raw: *const c_void, index: usize, free: Option(); + (*handle).index = index; (*handle).raw = raw; (*handle).free = free; (*handle).holds = 0; @@ -1673,6 +1716,9 @@ pub fn hold(obj: *mut PyObject, index: usize, s: &mut Scope, moved: bool) -> R<* let handle = obj.cast::(); // SAFETY: an object of a class that subclasses `Object`, and the GIL is held. unsafe { + if (*handle).index != index { + return expected(obj, index); + } if (*handle).closed { return value_error(&format!("{} is closed", type_name(ty(index)))); } @@ -2554,6 +2600,7 @@ fn is_type(obj: *mut PyObject, slot: &std::sync::OnceLock) -> bool { } static OBJECT: std::sync::OnceLock = std::sync::OnceLock::new(); +static VALUE_BASE: std::sync::OnceLock = std::sync::OnceLock::new(); static FUTURE: std::sync::OnceLock = std::sync::OnceLock::new(); static QUEUE: std::sync::OnceLock = std::sync::OnceLock::new(); static NODE: std::sync::OnceLock = std::sync::OnceLock::new(); @@ -2677,6 +2724,23 @@ unsafe fn add_type( size: usize, flags: u64, slots: &mut [PyType_Slot], +) -> R<()> { + // SAFETY: the caller's contract. + unsafe { add_sized(module, cell, name, size, 0, flags, slots) } +} + +/// [`add_type`], of objects each `itemsize` bytes longer per item. +/// +/// # Safety +/// As [`add_type`]'s. +unsafe fn add_sized( + module: *mut PyObject, + cell: &std::sync::OnceLock, + name: &CStr, + size: usize, + itemsize: usize, + flags: u64, + slots: &mut [PyType_Slot], ) -> R<()> { let qualified = format!("{}.{}\0", module_name(module), name.to_str().expect("ASCII")); // Python keeps the name for the type's life. @@ -2684,7 +2748,7 @@ unsafe fn add_type( let mut spec = PyType_Spec { name: qualified.as_ptr().cast(), basicsize: size as c_int, - itemsize: 0, + itemsize: itemsize as c_int, flags: (Py_TPFLAGS_DEFAULT as u64 | flags) as _, slots: slots.as_mut_ptr(), }; @@ -2741,6 +2805,14 @@ unsafe fn runtime(module: *mut PyObject) -> R<()> { ), slot(0, null_mut()), ])?; + // Its items are a value's bytes, past the header. + add_sized(module, &VALUE_BASE, c"Value", size_of::(), 1, base, &mut [ + slot( + Py_tp_doc, + c"A Rust `Copy` value's bytes, which only Rust makes or reads.".as_ptr().cast_mut().cast(), + ), + slot(0, null_mut()), + ])?; add_type(module, &FUTURE, c"Future", size_of::(), base, &mut [ slot(Py_tp_dealloc, future_dealloc as *mut c_void), slot(Py_tp_methods, FUTURE_METHODS.ptr().cast()), diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 2136476c7..19aee9769 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -795,20 +795,6 @@ final class _Native { dst.ref.owner = src.owner; } - /// A handle Rust takes, of a clone of `object`'s value: what a closure returns of a - /// `Copy` object, which Rust copies out of it and frees. - ffi.Pointer handOver( - _Object object, - ffi.Pointer Function(ffi.Pointer, ffi.Pointer) clone, - ) { - final raw = object._hold(); - try { - return clone(home, raw); - } finally { - object._release(); - } - } - /// Abort with what a call into Dart threw, which Rust has nowhere to unwind to. Never abort(String what, Object error, StackTrace stack) => fail('$what: a call into Dart threw $error\n$stack'); diff --git a/ffi/runtime/rt.hpp b/ffi/runtime/rt.hpp index 3b00b84c7..ad5d4c192 100644 --- a/ffi/runtime/rt.hpp +++ b/ffi/runtime/rt.hpp @@ -750,6 +750,16 @@ struct access { static auto own(T &object) { return object.raw_.own(); } + // A `Copy` value's C struct, which only Rust reads. + template + static auto bits(const T &value) noexcept { + return value.c_; + } + // The `Copy` value of the C struct `c` Rust made. + template + static T make(const C &c) noexcept { + return T(c); + } // `raw` owned by a `T`. template static T wrap(R *raw) { diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 6760ec7c3..386c720a6 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -12,7 +12,7 @@ //! `moq_audio_encoder_finish_`. A `_` doubled at the split would read better, but C++ //! reserves every name containing `__`. Only the names that clash change. -use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::collections::{BTreeMap, HashMap}; use rustc_span::Span; @@ -161,9 +161,6 @@ pub struct Abi<'a> { pub tuples: BTreeMap>, /// Each `[u8; N]` struct by C name, with `N`. pub arrays: BTreeMap, - /// Each `Copy` object a closure takes or returns by value, which `_copy` copies into - /// a new handle as Rust copies it, never running its `Clone`. - pub copies: BTreeSet, /// Every C name claimed. claims: Vec, /// The C name of each item whose own clashed. @@ -183,7 +180,6 @@ impl<'a> Abi<'a> { lists: BTreeMap::new(), tuples: BTreeMap::new(), arrays: BTreeMap::new(), - copies: BTreeSet::new(), claims: Vec::new(), renames: HashMap::new(), clashes: Vec::new(), @@ -677,19 +673,11 @@ impl<'a> Abi<'a> { } /// The C function a closure is: after its data, each value it takes, lent for its - /// call, a `Copy` object as a handle; then for a `Result`, an out pointer for its value - /// and one for its error, returning whether it succeeded, or else its value. A - /// value it returns is Rust's: a buffer one `_buf_copy` made, or a new handle. + /// call; then for a `Result`, an out pointer for its value and one for its error, + /// returning whether it succeeded, or else its value. A buffer it returns is Rust's, + /// one `_buf_copy` made. pub fn closure(&self, closure: &Closure) -> Ty { - let mut params: Vec = (closure.params.iter()) - .map(|ty| match ty { - Type::Object(i) => Ty::Handle { - name: self.object(*i), - constant: true, - }, - ty => self.ty(ty), - }) - .collect(); + let mut params: Vec = closure.params.iter().map(|ty| self.ty(ty)).collect(); let ok = self.ty(&closure.ok); match &closure.err { None => Ty::Fn(params, Box::new(ok)), @@ -740,7 +728,7 @@ impl<'a> Abi<'a> { _ => None, }; let ty = match copied { - // Taken by value, a `Copy` value is copied out, leaving its handle as it was. + // Taken by value, a `Copy` object is copied out, leaving its handle as it was. Some(i) if self.api.objects[i].copy => Ty::Handle { name: self.object(i), constant: true, @@ -1124,7 +1112,6 @@ impl<'a> Abi<'a> { let mut lists = BTreeMap::new(); let mut tuples: BTreeMap> = BTreeMap::new(); let mut arrays = BTreeMap::new(); - let mut copies = BTreeSet::new(); let mut mixed = Vec::new(); let mut stack = types; while let Some(ty) = stack.pop() { @@ -1157,17 +1144,6 @@ impl<'a> Abi<'a> { stack.extend(elements); } Type::Closure(closure) => { - for ty in closure.params.iter().chain([&closure.ok]) { - let ty = match ty { - Type::Option(inner) => inner, - ty => ty, - }; - if let Type::Object(i) = ty - && self.api.objects[*i].copy - { - copies.insert(*i); - } - } stack.extend(&closure.params); stack.push(&closure.ok); stack.extend(&closure.err); @@ -1195,13 +1171,6 @@ impl<'a> Abi<'a> { for name in arrays.keys() { self.claim(name.clone(), "an array".to_string(), crate_span, None); } - for &i in &copies { - let object = &self.api.objects[i]; - let item = Item::Object(i); - let name = self.object(i); - let what = format!("type `{}`", self.api.rust(&object.path, &object.args)); - self.claim(format!("{name}_copy"), what, object.span, Self::made(&item, &name)); - } for (name, elements) in tuples .iter() .chain(mixed.iter().map(|(name, elements)| (name, elements))) @@ -1232,7 +1201,6 @@ impl<'a> Abi<'a> { self.lists = lists; self.tuples = tuples; self.arrays = arrays; - self.copies = copies; } /// Claim the functions a type's std traits export. diff --git a/ffi/src/c.rs b/ffi/src/c.rs index 38cb2b8f0..840680c41 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -205,7 +205,7 @@ impl Header<'_> { for (i, record) in api.records.iter().enumerate() { let name = abi.record(i); self.line(format!("/* {} */", abi.api.rust(&record.path, &record.args)).trim_end()); - self.value_functions(&name, Ty::Struct(name.clone()), record.traits, true); + self.value_functions(&name, Ty::Struct(name.clone()), record.traits, record.opaque.is_none()); let owner = Ty::Struct(name.clone()); for method in &record.methods { self.function(method, Some(&owner)); @@ -237,16 +237,6 @@ impl Header<'_> { if t.clone { self.line(format!("{name} *{name}_clone(const {name} *self);")); } - if abi.copies.contains(&i) { - self.line( - comment( - "A new handle to a copy of the value, made as Rust copies it, never running its `Clone`: a closure hands Rust one it takes or returns.", - "", - ) - .trim_end(), - ); - self.line(format!("{name} *{name}_copy(const {name} *self);")); - } if t.eq { self.line(format!("bool {name}_eq(const {name} *a, const {name} *b);")); } @@ -333,6 +323,15 @@ impl Header<'_> { } if let Some(i) = (0..abi.api.records.len()).find(|&i| &abi.record(i) == name) { let record = &abi.api.records[i]; + if let Some(opaque) = &record.opaque { + self.line(format!( + "{}typedef struct {name} {{\n\t/* Its bytes, which only Rust reads: copy it, but never make or change one. */\n\t{} opaque[{}];\n}} {name};\n", + comment(&record.docs, ""), + c_unit(opaque.unit()), + opaque.units() + )); + return; + } let fields: Vec = record.fields.iter().map(|f| abi.ty(&f.ty)).collect(); for field in &fields { self.value(field); @@ -473,12 +472,9 @@ impl Header<'_> { "\nA handle it lends lives only for that call of `{name}`: free none, nor move out of one." )); } - let ok = matches!(closure.ok, Type::String) - || matches!(closure.ok.base(), Type::Object(_)) - || matches!(&closure.ok, Type::Option(inner) if matches!(**inner, Type::Object(_))); - if ok || closure.err.is_some() { + if closure.ok == Type::String || closure.err.is_some() { docs.push_str(&format!( - "\nWhat it returns is Rust's: a handle, or a buffer {}_buf_copy made.", + "\nA buffer it returns is Rust's, one {}_buf_copy made.", self.abi.prefix )); } @@ -576,6 +572,16 @@ fn has(traits: Traits) -> bool { traits.eq || traits.display || traits.debug || traits.default } +/// The C type of each unit an opaque value's struct holds, of `bytes` bytes. +pub fn c_unit(bytes: u64) -> &'static str { + match bytes { + 1 => "uint8_t", + 2 => "uint16_t", + 4 => "uint32_t", + _ => "uint64_t", + } +} + fn c_int(int: Int) -> &'static str { match int { Int::U8 => "uint8_t", diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 46a9e237f..1c5500595 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -46,8 +46,8 @@ pub struct Why { /// Types of crates that are no export, by crate and name, each exported as an object where /// some crate builds with it, and named at the facade's root by its role: `url::Url` as /// `Url` and `serde_json::Value`, which a `Serialize` or `Deserialize` parameter takes, as -/// `Json`, each made by parsing its text; and `std::time::Instant` as `Instant`, which -/// only Rust makes, and which C copies and compares. +/// `Json`, each made by parsing its text; and `std::time::Instant` as `Instant`, a value +/// only Rust makes, which C holds as its bytes and compares. const FOREIGN: [(&str, &str, &str); 3] = [ ("url", "Url", "Url"), ("serde_json", "Value", "Json"), @@ -63,8 +63,9 @@ pub const NOT_INFERRED: &str = "instance not inferred"; /// What C can never hold: left out and listed in the census rather than refused, since /// exporting it would need constructors or returns the crate has no use for. -const UNHELD: [&str; 3] = [ +const UNHELD: [&str; 4] = [ "object no export returns", + "value no export returns", "generic type no export instantiates", "type C never holds", ]; @@ -185,6 +186,8 @@ pub struct Report { #[derive(Clone)] enum Decl { Record(usize), + /// A `Copy` type C cannot see inside: a record of its bytes, numbered among records. + Value(usize), Enum(usize), Object(usize), Callback(usize), @@ -422,7 +425,7 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>, facade: &Facade<'_>) -> Collected { constants: Vec::new(), crates: Vec::new(), reached: Vec::new(), - targets: vec![tcx.sess.opts.target_triple.tuple().to_string()], + targets: vec![target(tcx)], }, }; c.walk(); @@ -989,6 +992,13 @@ impl<'tcx> Collector<'tcx> { .any(|&(k, n, _)| k == krate.as_str() && n == name.as_str()) } + /// Whether the foreign type `did` is a value C holds as its bytes, as a `Copy` one is. + fn foreign_value(&self, did: DefId) -> bool { + let tcx = self.tcx; + let ty = tcx.type_of(did).instantiate_identity().skip_norm_wip(); + !self.opaque(did) && implements(tcx, ty, did, sym::Copy) && padding(tcx, ty).is_some() + } + /// Each type a signature the export maps names, bounds included, at every choice /// [`Self::instantiate`] takes while inferring: each function's, constant's, and trait /// method's, and each type's `pub` fields and the inherent methods and constants that @@ -1546,6 +1556,8 @@ impl<'tcx> Collector<'tcx> { tuple: false, methods: Vec::new(), traits: Traits::default(), + opaque: None, + padding: Default::default(), }); } Decl::Enum(i) => { @@ -1565,6 +1577,28 @@ impl<'tcx> Collector<'tcx> { foreign: false, }); } + Decl::Value(i) => { + *i = api.records.len(); + let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); + let lifetimes = tcx.generics_of(def_id).own_counts().lifetimes; + let opaque = opaque(tcx, &self.sysroot, ty, lifetimes, false); + let padding = padding(tcx, ty).expect("a value's bytes do not depend on it"); + api.records.push(Record { + path, + home: home.clone(), + shown, + args: Vec::new(), + span, + on: On::ALL, + docs, + fields: Vec::new(), + tuple: false, + methods: Vec::new(), + traits: Traits::default(), + opaque: Some(opaque), + padding: [(target(tcx), padding)].into(), + }); + } Decl::Object(i) => { *i = api.objects.len(); let ty = tcx.type_of(def_id).instantiate(tcx, site.args).skip_norm_wip(); @@ -1606,11 +1640,37 @@ impl<'tcx> Collector<'tcx> { } // After every declared object, so `prune` can drop each that no export names. Until // here, each maps only to decide what does, never to an index kept. + // A foreign value goes after every declared record, for the same reason. for n in 0..self.foreign.len() { - let index = self.api.objects.len(); - self.foreign[n].1 = index; - let object = self.foreign_object(self.foreign[n].0, index); - self.api.objects.push(object); + let def_id = self.foreign[n].0; + let object = self.foreign_object(def_id, self.api.objects.len()); + match self.foreign_value(def_id) { + true => { + self.foreign[n].1 = self.api.records.len(); + let ty = tcx.type_of(def_id).instantiate_identity().skip_norm_wip(); + let opaque = opaque(tcx, &self.sysroot, ty, 0, true); + let padding = padding(tcx, ty).expect("a value's bytes do not depend on it"); + self.api.records.push(Record { + path: object.path, + home: object.home, + shown: object.shown, + args: object.args, + span: object.span, + on: object.on, + docs: object.docs, + fields: Vec::new(), + tuple: false, + methods: object.methods, + traits: object.traits, + opaque: Some(opaque), + padding: [(target(tcx), padding)].into(), + }); + } + false => { + self.foreign[n].1 = self.api.objects.len(); + self.api.objects.push(object); + } + } } // After every declared enum, so `prune` can drop it if no export names it. if let Some(level) = &mut self.level { @@ -1901,18 +1961,46 @@ impl<'tcx> Collector<'tcx> { } } - /// An object, if the type can move across threads. Shared borrows of one that is not - /// `Sync` are exclusive. + /// An object, if the type can move across threads, or if it is also `Copy`, a value C + /// holds as its bytes, unless C changes it in place, which a copy could not write back, + /// or which of its bytes it initializes depends on its value, so its padding could not + /// be zeroed. Shared borrows of an object that is not `Sync` are exclusive. fn object(&self, site: &Site<'tcx>) -> Decl { + let (tcx, ty, def_id) = (self.tcx, self.site_ty(site), site.key.def_id); + let copy = implements(tcx, ty, def_id, sym::Copy) && !self.changed(def_id) && padding(tcx, ty).is_some(); if self.orphans.contains(&site.key) { - return Decl::Refused(why("object no export returns", "")); + let construct = match copy { + true => "value no export returns", + false => "object no export returns", + }; + return Decl::Refused(why(construct, "")); } - match implements(self.tcx, self.site_ty(site), site.key.def_id, sym::Send) { - true => Decl::Object(0), - false => Decl::Refused(why("object not Send", "")), + match (implements(tcx, ty, def_id, sym::Send), copy) { + (true, true) => Decl::Value(0), + (true, false) => Decl::Object(0), + (false, _) => Decl::Refused(why("object not Send", "")), } } + /// Whether C may change the type `def_id` in place: through a `pub` field's setter, or + /// an inherent `pub` method taking `&mut self`. + fn changed(&self, def_id: DefId) -> bool { + let tcx = self.tcx; + let fields = matches!(tcx.def_kind(def_id), DefKind::Struct) + && tcx.adt_def(def_id).all_fields().any(|field| field.vis.is_public()); + fields + || (tcx.inherent_impls(def_id).iter()).any(|&imp| { + (tcx.associated_items(imp).in_definition_order()).any(|item| { + item.is_method() + && tcx.visibility(item.def_id).is_public() + && matches!( + tcx.fn_sig(item.def_id).skip_binder().inputs().skip_binder().first().map(|input| input.kind()), + Some(ty::Ref(_, _, mutability)) if mutability.is_mut() + ) + }) + }) + } + /// The objects C can never hold a handle to: no export it can call returns one. fn orphans(&self) -> Vec> { let api = &self.api; @@ -1928,7 +2016,8 @@ impl<'tcx> Collector<'tcx> { defaults.push(ty); } } - let mut held = vec![false; api.objects.len()]; + // Each object and opaque value C can hold, which only Rust makes. + let mut held: std::collections::HashSet = std::collections::HashSet::new(); loop { let mut reached = Vec::new(); for ty in &defaults { @@ -1952,21 +2041,23 @@ impl<'tcx> Collector<'tcx> { } } } - reached.retain(|&i| !held[i]); + reached.retain(|ty| !held.contains(ty)); if reached.is_empty() { break; } - for i in reached { - held[i] = true; - } + held.extend(reached); } - // Another crate's type an opaque handle holds, as each object of the crate's. + // Another crate's type an opaque handle holds, as each object of the crate's; a + // foreign value's index is a record's. let opaque = (self.foreign.iter()) - .filter(|&&(_, i)| api.objects[i].opaque.is_some() && !held[i]) + .filter(|&&(did, i)| { + !self.foreign_value(did) && api.objects[i].opaque.is_some() && !held.contains(&Type::Object(i)) + }) .map(|&(def_id, _)| Key { def_id, args: None }); (self.decls.iter()) .filter_map(|(&key, decl)| match decl { - Decl::Object(i) if !held[*i] => Some(key), + Decl::Object(i) if !held.contains(&Type::Object(*i)) => Some(key), + Decl::Value(i) if !held.contains(&Type::Record(*i)) => Some(key), _ => None, }) .chain(opaque) @@ -2171,6 +2262,14 @@ impl<'tcx> Collector<'tcx> { e.methods = methods; e.traits = traits; } + // One with no `pub` field, nor a `&mut self` method, as `object` decides. + Some(Decl::Value(index)) => { + let (methods, traits) = self.methods(&site); + let record = &mut self.api.records[index]; + record.args = args; + record.methods = methods; + record.traits = traits; + } Some(Decl::Object(index)) => { let (mut methods, traits) = self.methods(&site); if kind == DefKind::Struct { @@ -2220,8 +2319,12 @@ impl<'tcx> Collector<'tcx> { } for n in (0..self.foreign.len()).rev() { let gone = self.foreign[n].1; + let value = self.foreign_value(self.foreign[n].0); let api = &self.api; - let object = Type::Object(gone); + let object = match value { + true => Type::Record(gone), + false => Type::Object(gone), + }; let signature = |f: &Function| { (f.params.iter().map(|p| &p.ty)) .chain([&f.ok]) @@ -2232,20 +2335,31 @@ impl<'tcx> Collector<'tcx> { let fields = |fields: &[Field]| fields.iter().any(|f| names(&f.ty, &object)); let args = |args: &[Type]| args.iter().any(|ty| names(ty, &object)); let used = api.functions.iter().any(signature) - || (api.records.iter()).any(|r| args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) + || (api.records.iter().enumerate()).any(|(i, r)| { + (!value || i != gone) && (args(&r.args) || fields(&r.fields) || r.methods.iter().any(signature)) + }) || (api.enums.iter()).any(|e| { args(&e.args) || e.variants.iter().any(|v| fields(&v.fields)) || e.methods.iter().any(signature) }) || (api.objects.iter().enumerate()) - .any(|(i, o)| i != gone && (args(&o.args) || o.methods.iter().any(signature))) + .any(|(i, o)| (value || i != gone) && (args(&o.args) || o.methods.iter().any(signature))) || (api.callbacks.iter()).any(|c| c.methods.iter().any(signature)); if used { continue; } - self.api.objects.remove(gone); + // A foreign value is among the last records, as an object among the last objects. + match value { + true => drop(self.api.records.remove(gone)), + false => drop(self.api.objects.remove(gone)), + } self.foreign.remove(n); - for later in &mut self.foreign[n..] { - later.1 -= 1; + let values: Vec = (self.foreign[n..].iter()) + .map(|&(did, _)| self.foreign_value(did)) + .collect(); + for (later, later_value) in self.foreign[n..].iter_mut().zip(values) { + if later_value == value { + later.1 -= 1; + } } let api = &mut self.api; let functions = (api.functions.iter_mut()) @@ -2259,12 +2373,12 @@ impl<'tcx> Collector<'tcx> { .chain([&mut f.ok]) .chain(&mut f.err) .chain(instance) - .for_each(|ty| renumber(ty, gone)); + .for_each(|ty| renumber(ty, gone, value)); } for r in &mut api.records { (r.args.iter_mut()) .chain(r.fields.iter_mut().map(|f| &mut f.ty)) - .for_each(|ty| renumber(ty, gone)); + .for_each(|ty| renumber(ty, gone, value)); } for e in &mut api.enums { (e.args.iter_mut()) @@ -2273,10 +2387,10 @@ impl<'tcx> Collector<'tcx> { .iter_mut() .flat_map(|v| v.fields.iter_mut().map(|f| &mut f.ty)), ) - .for_each(|ty| renumber(ty, gone)); + .for_each(|ty| renumber(ty, gone, value)); } for o in &mut api.objects { - o.args.iter_mut().for_each(|ty| renumber(ty, gone)); + o.args.iter_mut().for_each(|ty| renumber(ty, gone, value)); } } } @@ -3022,9 +3136,9 @@ impl<'tcx> Collector<'tcx> { _ => output, }; // The glue cannot name an `impl Trait`'s hidden type, so one in a return that maps - // crosses only as what the glue passes on whole, an object's handle. A future and a - // collected iterator are taken apart instead, so it is what their output or items - // name that crosses. + // crosses only as what the glue passes on whole: an object's handle, or a value's + // bytes. A future and a collected iterator are taken apart instead, so it is what + // their output or items name that crosses. let unrevealed = sig.output(); let mut named = if ok.is_ok() { vec![unrevealed] } else { Vec::new() }; while let Some(ty) = named.pop() { @@ -3050,7 +3164,7 @@ impl<'tcx> Collector<'tcx> { } let hidden = normalize(ty); if !matches!(hidden.kind(), ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))) + if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))) { errs.push(why("opaque type", ty.to_string())); } @@ -3659,7 +3773,7 @@ impl<'tcx> Collector<'tcx> { true => self.instances.iter().any(|site| site.key == key), false => matches!( self.decls.get(&key), - Some(Decl::Record(_) | Decl::Enum(_) | Decl::Object(_)) + Some(Decl::Record(_) | Decl::Value(_) | Decl::Enum(_) | Decl::Object(_)) ), } }); @@ -3709,7 +3823,7 @@ impl<'tcx> Collector<'tcx> { true => self.instances.iter().any(|site| site.key == key), false => matches!( self.decls.get(&key), - Some(Decl::Record(_) | Decl::Enum(_) | Decl::Object(_)) + Some(Decl::Record(_) | Decl::Value(_) | Decl::Enum(_) | Decl::Object(_)) ), } }); @@ -3792,13 +3906,16 @@ impl<'tcx> Collector<'tcx> { let sig = tcx.fn_sig(def_id).instantiate(tcx, tcx.mk_args(base)).skip_norm_wip(); // An `async fn`'s future, or a returned `impl Trait`, captures it. kept |= sig.skip_binder().output().walk().any(|arg| arg == param.into()); - // A `Copy` object, which C lends as its handle and hands back as a new one; not one - // an opaque handle holds, which the closure would take as the handle's `rt::Owned`. - let env = TypingEnv::post_analysis(tcx, def_id); + // An opaque value, or a reference to one, which C copies. Another crate's value an + // opaque handle holds is an object, never a value, so it never matches. let copied = |ty: Ty<'tcx>, mapped: &Type| { - matches!(mapped, Type::Object(_)) - && self.opaque_value(ty).is_none() - && tcx.type_is_copy_modulo_regions(env, ty) + let base = match mapped { + Type::Ref(Mutability::Not, inner) => &**inner, + mapped => mapped, + }; + matches!(base, Type::Record(_)) + && matches!(ty.peel_refs().kind(), ty::Adt(adt, args) + if matches!(self.decl(adt.did(), *args), Some(Decl::Value(_))) || self.foreign_value(adt.did())) }; // Each parameter makes its own closure, which one type parameter cannot name twice. if sig @@ -3827,8 +3944,7 @@ impl<'tcx> Collector<'tcx> { } match self.ty(input, Pos::Param) { // An object is lent as a handle for the call, but an opaque one, whose - // handle holds its value erased, has none to lend; so is a `Copy` one taken by value, which C - // copies to keep. + // handle holds its value erased, has none to lend; an opaque value is copied, which C keeps. Ok(ty) if foreign_arg(&ty) || matches!(&ty, Type::Ref(_, inner) if matches!(**inner, Type::Object(_))) @@ -3851,14 +3967,16 @@ impl<'tcx> Collector<'tcx> { } _ => (ret, None), }; - // What C hands Rust: a value, a `String`, or a `Copy` object or an `Option` of one. + // What C hands Rust: a scalar, a `String`, or an opaque value or an `Option` of one, + // owned, as C has no reference to hand back. + let owned = |ty: Ty<'tcx>, mapped: &Type| !matches!(mapped, Type::Ref(..)) && copied(ty, mapped); let ok = match self.ty(ok, Pos::Return) { Ok(mapped) if foreign_return(&mapped) || mapped == Type::String || match (&mapped, ok.kind()) { - (Type::Option(inner), ty::Adt(_, args)) => copied(args.type_at(0), inner), - (mapped, _) => copied(ok, mapped), + (Type::Option(inner), ty::Adt(_, args)) => owned(args.type_at(0), inner), + (mapped, _) => owned(ok, mapped), } => { mapped @@ -4364,8 +4482,10 @@ impl<'tcx> Collector<'tcx> { for ty in ty.walk().filter_map(|arg| arg.as_type()) { match ty.kind() { ty::Ref(region, ..) => lent.push(*region), + // An object, an opaque value, or another crate's value an opaque handle holds, holds + // what its lifetime parameters name. ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_))) + if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_))) || self.opaque_value(ty).is_some() => { held.extend(args.regions()) @@ -4405,13 +4525,14 @@ impl<'tcx> Collector<'tcx> { hidden = tcx.type_of(*def_id).instantiate(tcx, args).skip_norm_wip(); } // One whose hidden type is an object crosses as its handle, which the glue - // keeps as the opaque type itself, so it holds every lifetime it captures, - // even one its hidden type does not borrow; one collected at the call holds - // nothing past it, and any other is refused. Looked up with regions erased, - // since they may be the signature's bound ones, which a key never names. + // keeps as the opaque type itself, and one whose hidden type is a value as + // its bytes, so each holds every lifetime it captures, even one its hidden + // type does not borrow; one collected at the call holds nothing past it, + // and any other is refused. Looked up with regions erased, since they may + // be the signature's bound ones, which a key never names. let erased = ty::fold_regions(tcx, hidden, |_, _| tcx.lifetimes.re_erased); let object = matches!(erased.kind(), ty::Adt(adt, args) - if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_)))); + if matches!(self.decl(adt.did(), *args), Some(Decl::Object(_) | Decl::Value(_)))); if object { held.extend(args.iter().flat_map(|arg| arg.walk()).filter_map(|arg| arg.as_region())); } @@ -4896,8 +5017,9 @@ impl<'tcx> Collector<'tcx> { { Type::SocketAddr(family) } else if let Some(&(_, i)) = (self.foreign.iter()).find(|&&(ty, _)| ty == did && !self.opaque(did)) { - // A value holds only values, and handles it can copy. + // A value holds only values. match pos { + _ if self.foreign_value(did) => Type::Record(i), Pos::Field => return Err(why("object in a value", tcx.def_path_str(did))), _ => Type::Object(i), } @@ -5024,7 +5146,7 @@ impl<'tcx> Collector<'tcx> { None => tcx.def_path_str(did), }; match self.decls.get(&key) { - Some(Decl::Record(i)) => Type::Record(*i), + Some(Decl::Record(i) | Decl::Value(i)) => Type::Record(*i), Some(Decl::Enum(i)) => Type::Enum(*i), // A value holds only values, and handles it can copy. Some(Decl::Object(_)) if pos == Pos::Field && !implements(tcx, ty, did, sym::Copy) => { @@ -5163,9 +5285,10 @@ fn foreign_arg(ty: &Type) -> bool { } /// Whether C can make a `ty` to pass, holding handles only to the objects in `held`. -fn buildable(api: &Api, ty: &Type, held: &[bool]) -> bool { +fn buildable(api: &Api, ty: &Type, held: &std::collections::HashSet) -> bool { match ty { - Type::Object(i) => held[*i], + Type::Object(_) => held.contains(ty), + Type::Record(i) if api.records[*i].opaque.is_some() => held.contains(ty), Type::Ref(_, inner) | Type::Box(inner) => buildable(api, inner, held), Type::Record(i) => api.records[*i].fields.iter().all(|f| buildable(api, &f.ty, held)), Type::Tuple(elements) => elements.iter().all(|ty| buildable(api, ty, held)), @@ -5177,10 +5300,11 @@ fn buildable(api: &Api, ty: &Type, held: &[bool]) -> bool { } } -/// The objects a returned `ty` may hand C a handle to. -fn returns(api: &Api, ty: &Type, objects: &mut Vec) { +/// The objects a returned `ty` may hand C a handle to, and the opaque values it may hand C. +fn returns(api: &Api, ty: &Type, objects: &mut Vec) { match ty { - Type::Object(i) => objects.push(*i), + Type::Object(_) => objects.push(ty.clone()), + Type::Record(i) if api.records[*i].opaque.is_some() => objects.push(ty.clone()), Type::Ref(_, inner) | Type::Box(inner) | Type::Option(inner) | Type::List(_, inner) => { returns(api, inner, objects) } @@ -5205,18 +5329,21 @@ fn serde(tcx: TyCtxt<'_>, tr: DefId) -> bool { ) } -/// Renumber each object after `gone`, which is dropped, in `ty`. -fn renumber(ty: &mut Type, gone: usize) { +/// Renumber each object after `gone`, which is dropped, in `ty`, or with `value`, each record. +fn renumber(ty: &mut Type, gone: usize, value: bool) { match ty { - Type::Object(o) if *o > gone => *o -= 1, - Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => renumber(inner, gone), - Type::Tuple(elements) => elements.iter_mut().for_each(|ty| renumber(ty, gone)), + Type::Object(o) if !value && *o > gone => *o -= 1, + Type::Record(r) if value && *r > gone => *r -= 1, + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => { + renumber(inner, gone, value) + } + Type::Tuple(elements) => elements.iter_mut().for_each(|ty| renumber(ty, gone, value)), Type::Closure(closure) => (closure .params .iter_mut() .chain([&mut closure.ok]) .chain(&mut closure.err)) - .for_each(|ty| renumber(ty, gone)), + .for_each(|ty| renumber(ty, gone, value)), _ => {} } } @@ -5425,6 +5552,203 @@ fn marker(tcx: TyCtxt<'_>, tr: DefId) -> bool { .contains(&Some(tr)) } +/// How C holds the `Copy` type `ty`, with `lifetimes` lifetime parameters, as its bytes. +fn opaque<'tcx>( + tcx: TyCtxt<'tcx>, + sysroot: &[CrateNum], + ty: Ty<'tcx>, + lifetimes: usize, + foreign: bool, +) -> crate::ir::Opaque { + let env = TypingEnv::fully_monomorphized(); + let ty = tcx.erase_and_anonymize_regions(ty); + // A type that has a layout on its target, as every exported `Copy` type does. + let layout = |ty: Ty<'tcx>| tcx.layout_of(env.as_query_input(ty)).ok().map(|l| l.layout); + let own = layout(ty).expect("an exported type has a layout"); + let eq = (tcx.get_diagnostic_item(sym::PartialEq)) + .and_then(|tr| (tcx.associated_item_def_ids(tr).iter().copied()).find(|&f| tcx.item_name(f) == sym::eq)); + // Whether every byte of `ty` belongs to an integer, `bool`, or `char` in it, and its + // `PartialEq`, if any, is derived or std's, so equal values have equal bytes. + fn dense<'tcx>( + tcx: TyCtxt<'tcx>, + sysroot: &[CrateNum], + env: TypingEnv<'tcx>, + eq: Option, + ty: Ty<'tcx>, + ) -> bool { + match ty.kind() { + ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char => true, + ty::Pat(base, _) => dense(tcx, sysroot, env, eq, *base), + ty::Array(element, _) => dense(tcx, sysroot, env, eq, *element), + ty::Adt(adt, args) if adt.is_struct() => { + let std = sysroot.contains(&adt.did().krate); + let derived = || { + let Some(eq) = eq else { return true }; + match ty::Instance::try_resolve(tcx, env, eq, tcx.mk_args(&[ty.into(), ty.into()])) { + Ok(Some(instance)) => { + (tcx.impl_of_assoc(instance.def_id())).is_none_or(|imp| tcx.is_automatically_derived(imp)) + } + _ => true, + } + }; + if !std && !derived() { + return false; + } + let Ok(own) = tcx.layout_of(env.as_query_input(ty)) else { + return false; + }; + let mut covered = 0; + for field in adt.all_fields() { + let Ok(field) = tcx.try_normalize_erasing_regions( + env, + ty::Unnormalized::new_wip(field.ty(tcx, *args).skip_norm_wip()), + ) else { + return false; + }; + let Ok(layout) = tcx.layout_of(env.as_query_input(field)) else { + return false; + }; + if !dense(tcx, sysroot, env, eq, field) { + return false; + } + covered += layout.size.bytes(); + } + covered == own.size.bytes() + } + _ => false, + } + } + crate::ir::Opaque { + size: own.size().bytes(), + align: own.align().bytes(), + dense: dense(tcx, sysroot, env, eq, ty), + lifetimes, + foreign, + } +} + +/// The target the export is collected for. +fn target(tcx: TyCtxt<'_>) -> String { + tcx.sess.opts.target_triple.tuple().to_string() +} + +/// The padding of `ty` on this target, as `(offset, length)` ranges, which the glue zeroes +/// so no byte Rust leaves uninitialized reaches a language; or none when which bytes it +/// initializes depends on its value: a union, such as `MaybeUninit`, or an enum whose +/// variants initialize different bytes, such as `Option`, whose `None` leaves its +/// payload uninitialized. +fn padding<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option> { + use rustc_abi::{FieldsShape, Variants}; + use rustc_middle::ty::layout::{LayoutCx, TyAndLayout}; + let env = TypingEnv::fully_monomorphized(); + let cx = LayoutCx::new(tcx, env); + let ty = tcx.erase_and_anonymize_regions(ty); + let layout = tcx.layout_of(env.as_query_input(ty)).ok()?; + // The spans `(offset, length)` of each byte `layout` initializes, sorted and merged, so + // what they hold grows with the type's shape, never with its size. + fn init<'tcx>(cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>) -> Option> { + match layout.ty.kind() { + ty::Adt(adt, _) if adt.is_union() => return None, + ty::Int(_) + | ty::Uint(_) + | ty::Float(_) + | ty::Bool + | ty::Char + | ty::RawPtr(..) + | ty::Ref(..) + | ty::FnPtr(..) => return Some(merged(vec![(0, layout.size.bytes())])), + _ => {} + } + match &layout.variants { + Variants::Empty => Some(Vec::new()), + Variants::Single { .. } => fields(cx, layout), + Variants::Multiple { + tag, + tag_field, + variants, + .. + } => { + let tag = (layout.fields.offset(tag_field.as_usize()).bytes(), tag.size(cx).bytes()); + let mut each: Option> = None; + for variant in variants.indices() { + let mut mine = fields(cx, layout.for_variant(cx, variant))?; + mine.push(tag); + let mine = merged(mine); + match &each { + Some(first) if *first != mine => return None, + Some(_) => {} + None => each = Some(mine), + } + } + Some(each.unwrap_or_default()) + } + } + } + fn fields<'tcx>(cx: &LayoutCx<'tcx>, layout: TyAndLayout<'tcx>) -> Option> { + match &layout.fields { + FieldsShape::Primitive => Some(merged(vec![(0, layout.size.bytes())])), + FieldsShape::Union(_) => None, + FieldsShape::Array { stride, count } => { + // Every element initializes the same bytes, so one is read and stamped at + // each stride: walking each would never end for `[(); usize::MAX]`. + let element = layout.field(cx, 0); + let (size, stride) = (element.size.bytes(), stride.bytes()); + if *count == 0 || size == 0 { + return Some(Vec::new()); + } + let mine = init(cx, element)?; + // A whole element tiling the array, as each of `[u8; 1 << 40]`'s does, is one span. + if mine == [(0, size)] && stride == size { + return Some(vec![(0, size * count)]); + } + Some(merged( + (0..*count) + .flat_map(|n| mine.iter().map(move |&(at, len)| (n * stride + at, len))) + .collect(), + )) + } + FieldsShape::Arbitrary { .. } => { + let mut spans = Vec::new(); + for n in 0..layout.fields.count() { + let at = layout.fields.offset(n).bytes(); + spans.extend( + init(cx, layout.field(cx, n))? + .into_iter() + .map(|(start, len)| (at + start, len)), + ); + } + Some(merged(spans)) + } + } + } + // `spans` sorted, without empty ones, and each overlapping or adjacent pair joined. + fn merged(mut spans: Vec<(u64, u64)>) -> Vec<(u64, u64)> { + spans.retain(|&(_, len)| len > 0); + spans.sort_unstable(); + let mut out: Vec<(u64, u64)> = Vec::with_capacity(spans.len()); + for (at, len) in spans { + match out.last_mut() { + Some((start, n)) if at <= *start + *n => *n = (*n).max(at + len - *start), + _ => out.push((at, len)), + } + } + out + } + // The padding is what the initialized spans leave between them and after the last. + let mut ranges = Vec::new(); + let mut end = 0; + for (at, len) in init(&cx, layout)? { + if at > end { + ranges.push((end, at - end)); + } + end = at + len; + } + if layout.size.bytes() > end { + ranges.push((end, layout.size.bytes() - end)); + } + Some(ranges) +} + /// Whether `ty`, the type `def_id` at some arguments, implements the std trait `item`. fn implements<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, def_id: DefId, item: rustc_span::Symbol) -> bool { let Some(tr) = tcx.get_diagnostic_item(item) else { diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 8d8dabf9f..661b989ff 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -8,7 +8,8 @@ //! member or as anything in the namespace, which it would hide. An object is a class owning //! its handle through a `std::unique_ptr`, so it moves, or where `Clone` makes a value no //! call tells apart but by an `Arc`'s or `Rc`'s count, a `std::shared_ptr` its copies -//! share; a record a struct; an enum an +//! share; a record a struct, and a `Copy` type with private fields a class over its C +//! struct's bytes, which only Rust makes or reads; an enum an //! `enum class`, or for one with data a `std::variant` of a struct per variant, with its //! methods free functions; a closure, a future, or a reader C++ implements a template //! parameter. The runtime they share, [`runtime/rt.hpp`](../runtime/rt.hpp), holds the @@ -554,9 +555,16 @@ impl<'a> Header<'a> { let name = self.global(&self.name(&abi.enumeration(i))); self.line(format!("enum class {name};")); } - for i in 0..api.records.len() { + + // Every class and struct, declared so any signature may name it. + for (i, record) in api.records.iter().enumerate() { let name = self.global(&self.name(&abi.record(i))); - self.line(format!("struct {name};")); + // The key its definition uses, which MSVC's C4099 and Clang's + // `-Wmismatched-tags` require match. + match record.opaque { + Some(_) => self.line(format!("class {name};")), + None => self.line(format!("struct {name};")), + } } for i in 0..api.objects.len() { let name = self.global(&self.name(&abi.object(i))); @@ -1104,6 +1112,11 @@ impl<'a> Header<'a> { let owner = Ty::Struct(c.clone()); let mut taken = fields.clone(); taken.push(name.clone()); + // An opaque value's bytes are its private member `c_`. + if record.opaque.is_some() { + taken.push("c_".to_string()); + members.push("c_".to_string()); + } for method in &record.methods { let (decl, member) = self.function(method, Some(&owner), Some((&taken, Owner::Member))); body.push(decl); @@ -1113,11 +1126,27 @@ impl<'a> Header<'a> { while body.last().is_some_and(String::is_empty) { body.pop(); } - self.line(format!( - "{}struct {name} {{\n{}\n}};\n", - docs(&record.docs, ""), - body.join("\n") - )); + match record.opaque { + // Its bytes, which only Rust makes or reads: C++ copies them, but makes + // none, as it has no public constructor. + Some(_) => { + let text = format!( + "{}\n\nIts bytes, which only Rust makes or reads: copy it, but never make one.", + record.docs + ); + self.line(format!( + "{}class {name} {{\npublic:\n{}\n\nprivate:\n\tfriend struct {d}::access;\n\n\texplicit {name}(const ::{c} &c) noexcept : c_(c) {{}}\n\n\t::{c} c_;\n}};\n", + docs(text.trim(), ""), + body.join("\n"), + d = self.d() + )); + } + None => self.line(format!( + "{}struct {name} {{\n{}\n}};\n", + docs(&record.docs, ""), + body.join("\n") + )), + } } Type::Enum(i) => { let e = &abi.api.enums[*i]; @@ -1224,6 +1253,16 @@ impl<'a> Header<'a> { let record = &api.records[i]; let c = abi.record(i); let name = self.name(&c); + if record.opaque.is_some() { + let d = self.d(); + self.line(format!( + "inline ::{c} lower(const ::{ns}::{name} &v) {{\n\treturn {d}::access::bits(v);\n}}\n" + )); + self.line(format!( + "inline ::{ns}::{name} lift(const ::{c} &c) {{\n\treturn {d}::access::make<::{ns}::{name}>(c);\n}}\n" + )); + continue; + } let lowered: Vec = record .fields .iter() @@ -1699,18 +1738,13 @@ impl<'a> Header<'a> { } /// The C value a closure returns Rust of the C++ value `e` of type `ty`, which Rust then - /// owns: bytes as a buffer `_buf_copy` made, and a `Copy` object as a new handle to - /// a copy, made without its `Clone`. `e` is named, so it may be read more than once. + /// owns: bytes as a buffer `_buf_copy` made. `e` is named, so it may be read more + /// than once. fn returned(&self, ty: &Type, e: &str) -> String { - let abi = self.abi; let d = self.d(); match ty { ty if Abi::is_buf(ty) => format!("{d}::copy_bytes({e})"), Type::Option(inner) if Abi::is_buf(inner) => format!("({e} ? {d}::copy_bytes(*{e}) : {d}::none())"), - Type::Option(inner) if matches!(**inner, Type::Object(_)) => { - format!("({e} ? {} : nullptr)", self.returned(inner, &format!("(*{e})"))) - } - Type::Object(i) => format!("::{}_copy({d}::access::raw({e}))", abi.object(*i)), ty => self.lower(ty, e), } } diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 922cfefb2..1eb2014f8 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -683,6 +683,15 @@ impl<'a> Gen<'a> { fields.push((format!("_{n}"), abi.ty(element))); } } else if let Some(i) = (0..abi.api.records.len()).find(|&i| abi.record(i) == name) { + if let Some(opaque) = &abi.api.records[i].opaque { + let code = format!( + "final class {class} extends ffi.Struct {{\n\t@ffi.Array({})\n\texternal ffi.Array $opaque;\n}}\n\n", + opaque.units(), + opaque.unit() * 8 + ); + self.structs.insert(name, code); + return class; + } for field in &abi.api.records[i].fields { fields.push((abi::escape(&field.name), abi.ty(&field.ty))); } @@ -971,6 +980,11 @@ impl<'a> Gen<'a> { let dt = self.dt(ty); let abi = self.abi; let body = match ty { + Type::Record(i) if abi.api.records[*i].opaque.is_some() => { + let units = abi.api.records[*i].opaque.as_ref().map_or(0, |o| o.units()); + let lines: Vec = (0..units).map(|k| format!("c.$opaque[{k}] = v._{k};")).collect(); + lines.join("\n\t") + } Type::Record(i) => { let record = &abi.api.records[*i]; let mut lines = Vec::new(); @@ -1088,6 +1102,12 @@ impl<'a> Gen<'a> { let abi = self.abi; let dt = self.dt(ty); let (param, body) = match ty { + Type::Record(i) if abi.api.records[*i].opaque.is_some() => { + let class = self.struct_class(&Ty::Struct(c.to_string())); + let units = abi.api.records[*i].opaque.as_ref().map_or(0, |o| o.units()); + let words: Vec = (0..units).map(|k| format!("c.$opaque[{k}]")).collect(); + (class, format!("{dt}._({})", words.join(", "))) + } Type::Record(i) => { let class = self.struct_class(&Ty::Struct(c.to_string())); let record = &abi.api.records[*i]; @@ -1483,6 +1503,30 @@ impl<'a> Gen<'a> { let name = self.class(&c); let rust = abi.api.rust(&record.path, &record.args); let ty = Type::Record(i); + if let Some(opaque) = &record.opaque { + // Its bytes, in the units of its C struct, each an `int` of the same bits. + let units: Vec = (0..opaque.units()).map(|k| format!("_{k}")).collect(); + let mut members = Vec::new(); + let mut body = vec![format!( + "\tconst {name}._({});\n\n{}", + units.iter().map(|u| format!("this.{u}")).collect::>().join(", "), + units.iter().map(|u| format!("\tfinal int {u};\n")).collect::() + )]; + self.traits(&ty, &c, record.traits, &mut body, &mut members); + self.methods(&record.methods, &Ty::Struct(c.clone()), &ty, &mut body, &mut members); + self.check_members(&rust, &mut members); + let text = format!( + "{rust}\n\nIts bytes, which only Rust makes or reads: copy it, but never make one.\n\n{}", + record.docs + ); + let _ = write!( + self.types, + "{}final class {name} {{\n{}}}\n\n", + docs(text.trim(), ""), + body.join("\n") + ); + return; + } let fields: Vec<(String, String)> = (record.fields.iter()) .map(|f| (field_name(&f.name), self.dt(&f.ty))) .collect(); @@ -1834,11 +1878,7 @@ impl<'a> Gen<'a> { let mut lends = Vec::new(); for (n, ty) in closure.params.iter().enumerate() { match ty { - // An object, by reference or a `Copy` one by value, lent for the call. - Type::Object(_) => { - lends.push(format!("{}._lent(p{n})", self.dt(ty))); - args.push(format!("l{}", lends.len() - 1)); - } + // An object, by reference, lent for the call. Type::Ref(_, object) if matches!(**object, Type::Object(_)) => { lends.push(format!("{}._lent(p{n})", self.dt(object))); args.push(format!("l{}", lends.len() - 1)); @@ -1863,9 +1903,10 @@ impl<'a> Gen<'a> { Type::Unit => format!("{call};"), ok => { let value = self.returned(ok, "r_"); - let write = match Self::scalar(&abi.ty(ok)) { - true => format!("p{at}.value = {value};"), - false => format!("_n.setBuf(p{at}, {value});"), + let write = match abi.ty(ok) { + c if Self::scalar(&c) => format!("p{at}.value = {value};"), + Ty::Buf => format!("_n.setBuf(p{at}, {value});"), + _ => format!("p{at}.ref = {value};"), }; format!("final r_ = {call};\n\t{write}") } @@ -1886,24 +1927,25 @@ impl<'a> Gen<'a> { } /// The C value of the Dart value `e` of type `ty` a closure returns, which Rust then - /// owns: text as a buffer `buf_copy` made, and a `Copy` object as a new handle to a - /// copy, made without its `Clone`, which Rust copies out and frees. + /// owns: text as a buffer `buf_copy` made, and a value as a struct it writes. fn returned(&mut self, ty: &Type, e: &str) -> String { - let abi = self.abi; match ty { Type::String => format!("_n.copied({e})"), - Type::Object(i) => { - let c = abi.object(*i); - let handle = Ty::Handle { - name: c.clone(), - constant: true, - }; - let clone = self.served_fn(&format!("{c}_copy"), vec![handle.clone()], handle); - format!("_n.handOver({e}, {clone})") - } - Type::Option(inner) if matches!(**inner, Type::Object(_)) => { - let given = self.returned(inner, "v"); - format!("switch ({e}) {{ final v? => {given}, null => ffi.nullptr }}") + Type::Record(_) | Type::Option(_) => { + let c = self.c_name(ty); + let name = format!("_ret_{c}"); + if !self.helpers.contains_key(&name) { + let class = self.struct_class(&Ty::Struct(c.clone())); + let w = self.writer(&c, ty); + let dt = self.dt(ty); + self.helpers.insert( + name.clone(), + format!( + "{class} {name}({dt} v) {{\n\tfinal a_ = _Arena();\n\ttry {{\n\t\tfinal c = ffi.Struct.create<{class}>();\n\t\t{w}(v, c, a_, true);\n\t\treturn c;\n\t}} finally {{\n\t\ta_.end();\n\t}}\n}}\n\n" + ), + ); + } + format!("{name}({e})") } ty => self.lower(ty, e), } @@ -2290,10 +2332,15 @@ impl<'a> Gen<'a> { } // A function of a type returning it is a constructor, which, unlike a static // member, may share its name with an instance member, as a Rust function may - // with a field. An object's `new` is its unnamed one, which a record's fields - // take. + // with a field. An object's or opaque value's `new` is its unnamed one, which a + // record's fields take. _ if is_static && !function.future && Some(&ok) == owner_ty => { - if snake == "new" && matches!(owner_ty, Some(Type::Object(_))) { + let unnamed = match owner_ty { + Some(Type::Object(_)) => true, + Some(Type::Record(i)) => self.abi.api.records[*i].opaque.is_some(), + _ => false, + }; + if snake == "new" && unnamed { (format!("factory {ret_type}({})", params.join(", ")), "new ".to_string()) } else { ( diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 485fede70..209255496 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -15,8 +15,8 @@ use rustc_data_structures::stable_hash::StableHasher; use crate::abi::{self, Abi, CFG, Ty}; use crate::config::Package; use crate::ir::{ - Access, Closure, Collection, Family, Function, Held, Literal, Mutability, On, Receiver, Task, TaskKind, Traits, - Type, + Access, Closure, Collection, Family, Function, Held, Literal, Mutability, On, Opaque, Receiver, Task, TaskKind, + Traits, Type, }; const RUNTIME: &str = include_str!("../runtime/rt.rs"); @@ -633,7 +633,11 @@ impl Gen<'_> { 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)), - Type::Record(i) => generic(&api.records[*i].path, 0, &api.records[*i].args), + Type::Record(i) => match &api.records[*i].opaque { + Some(opaque) if opaque.foreign => foreign_path(&api.records[*i].path), + Some(opaque) => generic(&api.records[*i].path, opaque.lifetimes, &api.records[*i].args), + None => generic(&api.records[*i].path, 0, &api.records[*i].args), + }, Type::Enum(i) if api.enums[*i].foreign => foreign_path(&api.enums[*i].path), Type::Enum(i) => generic(&api.enums[*i].path, 0, &api.enums[*i].args), Type::Object(i) if let Some(held) = api.objects[*i].opaque => match held { @@ -754,16 +758,14 @@ impl Gen<'_> { let typed: Vec = (closure.params.iter().enumerate()) .map(|(n, ty)| format!("c{n}: {}", self.rust(ty))) .collect(); - // Each object the closure takes, and how it is lent: a `Copy` one by value too. + // Each object the closure borrows, and how it is lent. let lent = |ty: &Type| match ty { Type::Ref(m, inner) => match (&**inner, m) { - (&Type::Object(i), Mutability::Mut) => Some((i, "lend_mut", "")), - (&Type::Object(i), Mutability::Not) if self.abi.api.objects[i].sync => Some((i, "lend", "")), - (&Type::Object(i), Mutability::Not) => Some((i, "lend_local", "")), + (&Type::Object(i), Mutability::Mut) => Some((i, "lend_mut")), + (&Type::Object(i), Mutability::Not) if self.abi.api.objects[i].sync => Some((i, "lend")), + (&Type::Object(i), Mutability::Not) => Some((i, "lend_local")), _ => None, }, - &Type::Object(i) if self.abi.api.objects[i].sync => Some((i, "lend", "&")), - &Type::Object(i) => Some((i, "lend_local", "&")), _ => None, }; let mut args: Vec = std::iter::once("data.get()".to_string()) @@ -790,11 +792,8 @@ impl Gen<'_> { } let mut call = format!("unsafe {{ f({}) }}", args.join(", ")); for (n, ty) in closure.params.iter().enumerate().rev() { - if let Some((i, lend, by)) = lent(ty) { - call = format!( - "rt::Object::<{}>::{lend}({by}c{n}, w, |h{n}| {call})", - self.object_path(i) - ); + if let Some((i, lend)) = lent(ty) { + call = format!("rt::Object::<{}>::{lend}(c{n}, w, |h{n}| {call})", self.object_path(i)); } } let ret = self.rust(&closure.ok); @@ -825,16 +824,10 @@ impl Gen<'_> { } /// The Rust value a closure C implements returned as the C value `e`, which Rust then - /// owns: a buffer `_buf_copy` made, or a handle, out of which it copies a - /// `Copy` object before freeing it. + /// owns, as a buffer `_buf_copy` made. fn returned(&self, ty: &Type, e: &str) -> String { match ty { Type::String => format!("unsafe {{ {e}.take_string(w) }}"), - Type::Object(i) => format!("unsafe {{ rt::Object::<{}>::returned({e}, w) }}", self.object_path(*i)), - Type::Option(inner) => format!( - "{{ let r = {e}; if r.is_null() {{ None }} else {{ Some({}) }} }}", - self.returned(inner, "r") - ), ty => self.lift(ty, e, "true"), } } @@ -1095,11 +1088,6 @@ impl Gen<'_> { self.traits(&name, &path, object.traits, |e| { format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}") }); - if self.abi.copies.contains(&i) { - self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_copy(this: *const ::std::ffi::c_void) -> *const ::std::ffi::c_void {{\n\tlet w = \"{name}_copy\";\n\trt::Object::new(*unsafe {{ rt::Object::<{path}>::{borrow}(this, w) }})\n}}\n" - )); - } let owner = Ty::Handle { name: name.clone(), constant: false, @@ -1177,7 +1165,9 @@ impl Gen<'_> { self.lower(ty, &format!("<{path} as ::std::default::Default>::default()")) )); } - if !matches!(self.abi.ty(ty), Ty::Enum(_)) { + // An opaque value is `Copy`, with nothing to free. + let opaque = matches!(ty, Type::Record(i) if self.abi.api.records[*i].opaque.is_some()); + if !matches!(self.abi.ty(ty), Ty::Enum(_)) && !opaque { let free = self.free(ty, "unsafe { this.read() }").unwrap_or_default(); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: *mut {c}) {{\n\tif this.is_null() {{\n\t\treturn;\n\t}}\n\t{free}\n}}\n" @@ -1191,6 +1181,10 @@ impl Gen<'_> { let name = self.abi.record(i); let path = self.rust(&Type::Record(i)); let ty = Type::Record(i); + if let Some(opaque) = record.opaque { + self.opaque(i, &opaque); + return; + } let mut fields = String::new(); for (n, field) in record.fields.iter().enumerate() { let _ = writeln!(fields, "\tpub f{n}: {},", rs(&self.abi.ty(&field.ty))); @@ -1263,6 +1257,51 @@ impl Gen<'_> { } } + /// An opaque value: its bytes in a struct of `MaybeUninit` units, as its padding may + /// be, read and written unaligned, as a unit's alignment may be less than its own. + fn opaque(&mut self, i: usize, opaque: &Opaque) { + let record = &self.abi.api.records[i]; + let name = self.abi.record(i); + let path = self.rust(&Type::Record(i)); + let ty = Type::Record(i); + let (unit, units) = (format!("u{}", opaque.unit() * 8), opaque.units()); + self.line(format!( + "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n\tpub opaque: [::std::mem::MaybeUninit<{unit}>; {units}],\n}}\n" + )); + self.item(format!( + "const _: () = assert!(::std::mem::size_of::<{path}>() <= ::std::mem::size_of::<{name}>(), \"{name} is smaller than its Rust type\");\n" + )); + // A layout check alone passes a type that lost `Copy`, which `lift_` would then drop twice. + self.item(format!( + "const _: fn() = {{\n\tfn copy() {{}}\n\tcopy::<{path}>\n}};\n" + )); + self.item(format!( + "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {path} {{\n\tlet _ = (w, take);\n\tunsafe {{ c.opaque.as_ptr().cast::<{path}>().read_unaligned() }}\n}}\n" + )); + // Its padding on each target, which the write below leaves uninitialized, so no + // stale byte of Rust's reaches a language, which may serialize what it holds. + let tables: Vec = (record.padding.iter()) + .map(|(target, ranges)| { + let ranges: Vec = ranges.iter().map(|(at, len)| format!("({at}, {len})")).collect(); + format!( + "\t#[cfg({CFG} = {target:?})]\n\tconst PADDING: &[(usize, usize)] = &[{}];\n", + ranges.join(", ") + ) + }) + .collect(); + self.item(format!("impl {name} {{\n{}}}\n", tables.join("\n"))); + // Zeroed past its own bytes, and its padding zeroed after the write. Generic, since + // an `impl Trait` return hides the type it writes, which the collector checked is this. + self.item(format!( + "fn lower_{name}(v: T) -> {name} {{\n\tconst {{ assert!(::std::mem::size_of::() == ::std::mem::size_of::<{path}>() && ::std::mem::align_of::() == ::std::mem::align_of::<{path}>()) }};\n\tlet mut c = {name} {{ opaque: [::std::mem::MaybeUninit::zeroed(); {units}] }};\n\tlet at = c.opaque.as_mut_ptr();\n\tunsafe {{ at.cast::().write_unaligned(v) }};\n\tfor &(start, len) in {name}::PADDING {{\n\t\tunsafe {{ at.cast::().add(start).write_bytes(0, len) }};\n\t}}\n\tc\n}}\n" + )); + self.value_traits(&name, &path, &ty, record.traits); + let owner = Ty::Struct(name.clone()); + for method in &record.methods { + self.function(method, Some((&owner, ty.clone()))); + } + } + fn enumeration(&mut self, i: usize) { let e = &self.abi.api.enums[i]; self.on = e.on; @@ -1498,6 +1537,8 @@ impl Gen<'_> { fn view(&self, ty: &Type, e: &str) -> String { match ty { Type::Ref(_, inner) => match &**inner { + // An opaque value is `Copy`, lent as a copy. + Type::Record(_) => self.lower(inner, &format!("*{e}")), 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})"), diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 8c6812512..f8dbc2862 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -19,7 +19,9 @@ //! at any depth, returns an `error`; a record a struct; a plain enum an integer type and its //! constants; an enum with data an interface each variant's struct implements; a trait an //! interface; a closure a function; a reader an `io.ReadSeeker`; a future a call taking a -//! `context.Context` that blocks until the future is ready; a `Result` an `error`. The +//! `context.Context` that blocks until the future is ready; a `Result` an `error`. A `Copy` +//! type with private fields is a struct over its bytes, which only Rust makes or reads, +//! and a zero one panics where Rust would read it. The //! runtime they share, //! [`runtime/rt.go`](../runtime/rt.go), holds the handles and the bytes Rust returns, and //! each package's copy of [`runtime/ffi_cgo.go`](../runtime/ffi_cgo.go) the arena a call lowers @@ -1553,6 +1555,32 @@ impl<'a> Gen<'a> { let ty = Type::Record(i); let name = self.named(&ty).1; let rust = abi.api.rust(&record.path, &record.args); + if let Some(opaque) = &record.opaque { + let text = format!( + "{name} is `{rust}`, its bytes, which only Rust makes or reads: copy it, but never\nmake or change one. A zero {name} panics where Rust would read it, and == is a compile\nerror, since bytes alike need not be equal: Equal, if it has one, is Rust's.\n\n{}", + record.docs + ); + let _ = write!( + self.types, + "{}type {name} struct {{\n\t_ [0]func()\n\t// Bytes_ is its bytes, for the generated packages: use none of it.\n\tBytes_ [{}]uint{}\n\t// Made_ is whether Rust made it, rather than it being Go's zero value.\n\tMade_ bool\n}}\n\n", + docs(text.trim(), ""), + opaque.units(), + opaque.unit() * 8 + ); + let mut members = vec!["Bytes_".to_string(), "Made_".to_string()]; + let fields = members.clone(); + members.extend(self.methods_of( + &ty, + &c, + &rust, + record.traits, + &record.methods, + &Ty::Struct(c.clone()), + &fields, + )); + self.distinct(&rust, &members); + return; + } let fields: Vec<(String, String)> = (self.record_fields(i).into_iter().zip(&record.fields)) .map(|(name, f)| (name, self.go(&f.ty))) .collect(); @@ -1617,6 +1645,16 @@ impl<'a> Gen<'a> { let c = abi.record(i); let ty = Type::Record(i); let name = self.qualified(&ty); + if let Some(opaque) = &record.opaque { + let (local, bytes) = ( + self.named(&ty).1, + format!("[{}]uint{}", opaque.units(), opaque.unit() * 8), + ); + return format!( + "func lift_{c}(c C.{c}) {name} {{\n\treturn {name}{{Bytes_: *(*{bytes})(unsafe.Pointer(&c)), Made_: true}}\n}}\n\n\ + func lower_{c}(v {name}) C.{c} {{\n\tif !v.Made_ {{\n\t\tpanic(\"a zero {local}, which only Rust makes\")\n\t}}\n\treturn *(*C.{c})(unsafe.Pointer(&v.Bytes_))\n}}\n\n" + ); + } let names = self.record_fields(i); let lifted: Vec = (record.fields.iter().zip(&names)) .map(|(f, field)| { @@ -2062,8 +2100,7 @@ impl<'a> Gen<'a> { /// The trampoline C calls for a closure of type `ty`, which Rust lends for a call or /// keeps: it lends each object it takes by reference as its type, ended once it returns, - /// owns a clone of each `Copy` object, and hands Rust what the closure returns, writing - /// a `Result` through its outs. + /// and hands Rust what the closure returns, writing a `Result` through its outs. fn trampoline(&mut self, ty: &Type, closure: &Closure) -> String { let go = self.go(ty); // Keyed by the Rust closure, since Go spells a borrowed and a copied object alike. @@ -2091,13 +2128,6 @@ impl<'a> Gen<'a> { let _ = writeln!(body, "\tdefer {l}.Handle_.End()"); args.push(l); } - // A copy is the callee's, which Go keeps as long as it likes: its own copy of - // the value Rust lends for the call, made as Rust copies it. - Type::Object(i) => { - self.convert(ty); - let c = self.abi.object(*i); - args.push(format!("own_{c}(C.{c}_copy({p}))")); - } ty => args.push(self.lift_lent(ty, p)), } } @@ -2149,42 +2179,15 @@ impl<'a> Gen<'a> { } /// The C value Rust takes from the Go value `e` of type `ty` a closure returned, which - /// Rust then owns: a string's copy, or a clone of a `Copy` object's handle. + /// Rust then owns: a string's copy, and a value as it lowers. fn returned(&mut self, ty: &Type, e: &str) -> String { match ty { Type::String => format!("cBufCopy({e})"), - Type::Object(i) => { - self.returns(*i); - format!("returned_{}({e})", self.abi.object(*i)) - } - Type::Option(inner) => { - let Type::Object(i) = **inner else { - unreachable!("a closure returns an Option only of a Copy object, not {inner:?}") - }; - self.returns(i); - format!("returnedOption_{}({e})", self.abi.object(i)) - } + Type::Record(_) | Type::Option(_) => self.lower(ty, e), ty => self.lower_scalar(ty, e), } } - /// The helpers handing Rust a clone of the handle of object `i` a closure returned, or - /// nil for none. - fn returns(&mut self, i: usize) { - let ty = Type::Object(i); - self.convert(&ty); - let c = self.abi.object(i); - let name = self.qualified(&ty); - let local = self.named(&ty).1; - self.helper(&format!("returned_{c}"), |_| { - format!( - "// returned_{c} is a new handle to a copy of v, which Rust frees once it copies v out of it.\n\ - func returned_{c}(v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\tpanic(\"a nil {local} returned to Rust\")\n\t}}\n\th := v.Handle_.Hold()\n\tif h == nil {{\n\t\tpanic(v.Handle_.Closed())\n\t}}\n\tdefer unhold_(v.Handle_, v)\n\treturn C.{c}_copy((*C.{c})(h))\n}}\n\n\ - func returnedOption_{c}(v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\treturn nil\n\t}}\n\treturn returned_{c}(v)\n}}\n\n" - ) - }); - } - /// The Go statement closing each object the value `e` of type `ty` holds, once a call /// moved them out. fn close(&self, ty: &Type, e: &str, depth: usize) -> String { diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index caa894a74..cfd780642 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -143,7 +143,8 @@ pub struct Home { pub outside: usize, } -/// A struct whose fields are all `pub`: a value, copied across. +/// A struct whose fields are all `pub`, or a `Copy` type C cannot see inside: a value, +/// copied across. #[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)] pub struct Record { pub path: Path, @@ -163,6 +164,61 @@ pub struct Record { pub tuple: bool, pub methods: Vec, pub traits: Traits, + /// For a `Copy` type with private fields, which has no `fields`: its bytes, which only + /// Rust reads. + pub opaque: Option, + /// For an opaque value, its padding on each target, by target, as `(offset, length)` + /// ranges, which the glue zeroes, so no byte Rust leaves uninitialized reaches a + /// language. A baseline written before it existed has none, as its records had. + #[serde(default)] + pub padding: std::collections::BTreeMap>, +} + +/// A `Copy` type held as its bytes, which every language copies and none reads. +#[derive(Clone, Copy, PartialEq, Eq, Debug, serde::Serialize, serde::Deserialize)] +pub struct Opaque { + /// Its size and alignment: on several targets, the largest of each, so one binding + /// holds it on all of them. + pub size: u64, + pub align: u64, + /// Whether its bytes are its fields' with no padding between them, each an integer, a + /// `bool`, or a `char`, and each struct's `PartialEq` in it, if any, derived or std's, + /// so two values Rust calls equal have equal bytes: on several targets, on all of them. + pub dense: bool, + /// How many lifetime parameters it has, each `'static` in the export, as an object's. + pub lifetimes: usize, + /// Whether it is a foreign type the facade maps, such as `std::time::Instant`: its path + /// is the type's in its own crate. + pub foreign: bool, +} + +impl Opaque { + /// Whether it fits a 64-bit word, which a language may hold it in with no allocation. + pub fn word(&self) -> bool { + self.size <= 8 + } + + /// Whether a language may hold it in a word and compare that word: it fits one and is + /// dense, so values Rust calls equal have equal bits. Kotlin's type for it turns on + /// this, so a change of it breaks Kotlin's compiled callers. + pub fn compared(&self) -> bool { + self.word() && self.dense + } + + /// The bytes of each unit its C struct holds it in, by its alignment, at most 8. + pub fn unit(&self) -> u64 { + self.align.clamp(1, 8) + } + + /// How many units its C struct holds: at least one, as C has no empty struct. + pub fn units(&self) -> u64 { + self.size.div_ceil(self.unit()).max(1) + } + + /// How many 64-bit words hold its C struct. + pub fn words(&self) -> u64 { + (self.units() * self.unit()).div_ceil(8) + } } #[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)] @@ -230,8 +286,9 @@ pub struct Object { pub docs: String, /// Whether it is `Sync`: if not, `&self` borrows it on one thread at a time. pub sync: bool, - /// Whether it is `Copy`: taken by value, it is copied out of its handle, as Rust - /// copies it, rather than moved out. + /// Whether it is `Copy`, which only one with a `pub` field or a `&mut self` method + /// is, any other being a value: taken by value, it is copied out of its handle, as + /// Rust copies it, rather than moved out. pub copy: bool, /// Whether copies of its handle may share one value, since no call tells that apart /// from `Clone` but by an `Arc`'s or `Rc`'s count: it is `Clone` and `Sync`, its clone owns no state that changes @@ -527,11 +584,10 @@ pub enum Type { /// A [`Type::Closure`]: what it takes and returns. #[derive(Clone, PartialEq, Eq, Hash, Debug, serde::Serialize, serde::Deserialize)] pub struct Closure { - /// Each value it takes, which Rust lends for the call: a value, a borrowed object, or - /// a `Copy` object, lent as its handle. + /// Each value it takes, which Rust lends for the call: a value or a borrowed object. pub params: Vec, - /// What it returns, or for a `Result`, its value: a value, a `String`, or a `Copy` - /// object or an `Option` of one, which the foreign side hands Rust. + /// What it returns, or for a `Result`, its value: a value or a `String`, which the + /// foreign side hands Rust. pub ok: Type, /// The error of a `Result` it returns: a `String`. pub err: Option, diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index fd7ed0c72..7c28f19e1 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -28,7 +28,7 @@ use std::hash::Hasher; use rustc_data_structures::stable_hash::StableHasher; use crate::abi::{self, Abi, Ty, pascal}; -use crate::ir::{Access, Family, Function, Int, Literal, Mutability, On, Param, Receiver, Type}; +use crate::ir::{Access, Family, Function, Int, Literal, Mutability, On, Opaque, Param, Receiver, Type}; const RUNTIME: &str = include_str!("../runtime/rt.kt"); pub const GLUE_RUNTIME: &str = include_str!("../runtime/jni.rs"); @@ -798,6 +798,7 @@ impl<'a> Gen<'a> { _ => wrap(e), } } + Ty::Struct(name) if opaque_of(self.abi, c).is_some() => format!("{}({e})", self.class(name)), Ty::Buf => match (decode(ty), matches!(ty.base(), Type::Option(_)), lent) { (Some(decode), true, _) => format!("{e}?.{decode}"), (Some(decode), false, _) => format!("{e}!!.{decode}"), @@ -815,18 +816,17 @@ impl<'a> Gen<'a> { } /// The Kotlin expression passing `e` of type `ty` back to Rust as the C type `c`, as a - /// callback or closure returns it: a scalar, a string's bytes, or a new handle to a - /// copy of a `Copy` object, made without its `Clone`, or 0 for none, which Rust frees. - fn lower_ret(&self, c: &Ty, ty: &Type, e: &str) -> String { + /// callback or closure returns it: a scalar, a string's bytes, an opaque value's word + /// or words, or any other value encoded. + fn lower_ret(&mut self, c: &Ty, ty: &Type, e: &str) -> String { match c { Ty::Int(int) => to_signed(*int, e), Ty::Buf => format!("{e}.encodeToByteArray()"), - Ty::Handle { name, .. } => { - let clone = format!("Native.{}_copy", self.bare(name)); - match ty { - Type::Option(_) => format!("{e}?.handle?.hold {{ h -> {clone}(h) }} ?: 0L"), - _ => format!("{e}.handle.hold {{ h -> {clone}(h) }}"), - } + Ty::Struct(_) if let Some(opaque) = opaque_of(self.abi, c) => format!("{e}.{}", holder(&opaque)), + Ty::Struct(_) => { + let w = self.fresh("w"); + let write = self.write_value(ty, &w, e); + format!("Native.Writer().use {{ {w} -> {write}; {w}.finish() }}") } _ => e.to_string(), } @@ -851,7 +851,7 @@ impl<'a> Gen<'a> { arg.rust_args.push(format!("rt::list(&b{p})")); return arg; } - let (jni, native) = jni_type(c, ty); + let (jni, native) = jni_type(self.abi, c, ty); arg.jni.push(jni); arg.native.push(native.to_string()); match c { @@ -935,11 +935,20 @@ impl<'a> Gen<'a> { "crate::{name} {{ vtable: &VTABLE_{name}, data: rt::global(env, {p}) }}" )); } + // An opaque value crosses as the word or words Kotlin holds it in. + Ty::Struct(_) | Ty::In(_) if let Some(opaque) = opaque_of(abi, c) => { + arg.exprs.push(format!("{e}.{}", holder(&opaque))); + arg.rust_pre.push(format!("let a{p} = {};", from_kotlin(&opaque, &p))); + arg.rust_args.push(match c { + Ty::In(_) => format!("&a{p}"), + _ => format!("a{p}"), + }); + } // A record crosses as its scalars, and its other fields encoded into one `byte[]`. // Only while the call's JVM parameters stay within the 255 slots a method takes. Ty::Struct(_) | Ty::In(_) if let Type::Record(i) = ty.base() - && let Some(left) = self.room.checked_sub(weight(abi, *i).saturating_sub(c_slots(c))) => + && let Some(left) = self.room.checked_sub(weight(abi, *i).saturating_sub(c_slots(abi, c))) => { self.room = left; arg.jni.clear(); @@ -1206,18 +1215,32 @@ impl<'a> Gen<'a> { let class = self.class(c); let bare = self.bare(c).to_string(); let object = matches!(ty, Type::Object(_)); - // The receiver as the glue takes it: a handle, held for the call, or encoded. - let (params, open, arg, rust_self): (&[(&str, &str)], _, _, _) = match object { - true => ( - &[("jlong", "Long")], + let opaque = match ty { + Type::Record(i) => self.abi.api.records[*i].opaque, + _ => None, + }; + // The receiver as the glue takes it: a handle, held for the call, an opaque value's + // word or words, or encoded. + let (params, open, arg, rust_self): (Vec<(&str, &str)>, _, _, _) = match (object, opaque) { + (true, _) => ( + vec![("jlong", "Long")], "handle.hold { h ->".to_string(), "h".to_string(), "p0 as usize as _".to_string(), ), - false => { + (false, Some(opaque)) => { + let (jni, native) = jni_type(self.abi, &Ty::Struct(c.to_string()), ty); + ( + vec![(jni, native)], + "run {".to_string(), + holder(&opaque).to_string(), + format!("&{}", from_kotlin(&opaque, "p0")), + ) + } + (false, None) => { let write = self.write_value(ty, "w", "this"); ( - &[("jbyteArray", "ByteArray"), ("jobjectArray", "kotlin.Array?")], + vec![("jbyteArray", "ByteArray"), ("jobjectArray", "kotlin.Array?")], format!("Native.Writer().use {{ w ->\n\t\t{write}"), "w.finish(), w.refs()".to_string(), "&rt::Reader::new(env, &rt::bytes(env, p0).unwrap_or_default(), p1).get()".to_string(), @@ -1227,7 +1250,7 @@ impl<'a> Gen<'a> { let unary = |g: &mut Self, suffix: &str, sig: &str| { g.native( &format!("{bare}_{suffix}"), - params, + ¶ms, ("jbyteArray", "ByteArray?"), &format!("rt::owned(env, crate::{c}_{suffix}({rust_self}))"), ); @@ -1245,16 +1268,6 @@ impl<'a> Gen<'a> { "/** Rust's `Debug` form. */\n\tfun debug(): String", )); } - if let Type::Object(i) = ty - && self.abi.copies.contains(i) - { - self.native( - &format!("{bare}_copy"), - &[("jlong", "Long")], - ("jlong", "Long"), - &format!("crate::{c}_copy(p0 as usize as _) as usize as jlong"), - ); - } if object && t.clone { self.native( &format!("{bare}_clone"), @@ -1266,6 +1279,22 @@ impl<'a> Gen<'a> { "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(Native.{bare}_clone(h)) }}\n" )); } + // An opaque value Kotlin holds in a word compares by it, as a value class does. + if let Some(opaque) = opaque.filter(|opaque| t.eq && !word(opaque)) { + self.native( + &format!("{bare}_eq"), + &[("jlongArray", "LongArray"), ("jlongArray", "LongArray")], + ("jboolean", "Boolean"), + &format!( + "crate::{c}_eq(&{}, &{})", + from_kotlin(&opaque, "p0"), + from_kotlin(&opaque, "p1") + ), + ); + body.push(format!( + "\toverride fun equals(other: Any?): Boolean = other is {class} && Native.{bare}_eq(words, other.words)\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): Int = 0\n" + )); + } if object && t.eq { self.native( &format!("{bare}_eq"), @@ -1322,6 +1351,9 @@ impl<'a> Gen<'a> { let mut statics = Vec::new(); self.traits(&ty, &c, record.traits, &mut body, &mut statics); let mut members: Vec = fields.iter().map(|(f, _)| format!("val {f}")).collect(); + if let Some(opaque) = record.opaque { + members.push(format!("val {}", holder(&opaque))); + } let owner = Ty::Struct(c.clone()); for method in &record.methods { let place = match method.receiver { @@ -1357,6 +1389,10 @@ impl<'a> Gen<'a> { true => String::new(), false => format!(" {{\n{}}}", body.join("\n")), }; + if let Some(opaque) = record.opaque { + self.opaque(&c, &name, &opaque, &docs, &body); + return; + } match fields.is_empty() { true => { let _ = write!( @@ -1419,6 +1455,34 @@ impl<'a> Gen<'a> { ); } + /// An opaque value's class, `name` in Kotlin and `c` in C, and its codec: a value class + /// over the `Long` it fits in with no padding, or else a class over its words, which + /// compares through Rust's `PartialEq`. Only Rust makes one. + fn opaque(&mut self, c: &str, name: &str, opaque: &Opaque, docs: &str, body: &str) { + let words = opaque.words(); + let (head, write, read) = match word(opaque) { + true => ( + format!("@kotlin.jvm.JvmInline\nvalue class {name} internal constructor(internal val bits: Long)"), + "\tw.i64(v.bits)\n".to_string(), + format!("{name}(r.i64())"), + ), + false => ( + format!("class {name} internal constructor(internal val words: LongArray)"), + "\tfor (word in v.words) w.i64(word)\n".to_string(), + format!("{name}(LongArray({words}) {{ r.i64() }})"), + ), + }; + let _ = write!(self.types, "{docs}{head}{body}\n\n"); + let _ = write!( + self.top, + "internal fun w_{c}(w: Native.Writer, v: {name}) {{\n{write}}}\n\ninternal fun r_{c}(r: Native.Reader): {name} = {read}\n\n" + ); + let _ = write!( + self.glue, + "impl Wire for crate::{c} {{\n\tfn read(r: &mut Reader<'_>) -> Self {{\n\t\trt::read_words::<_, {words}>(r)\n\t}}\n\n\tfn write(self, w: &mut Writer) {{\n\t\trt::write_words::<_, {words}>(w, self);\n\t}}\n}}\n\n" + ); + } + fn enumeration(&mut self, i: usize) { let abi = self.abi; let e = &abi.api.enums[i]; @@ -1730,7 +1794,7 @@ impl<'a> Gen<'a> { for (k, (ty, c)) in lent.iter().zip(c_params).enumerate() { let rust_ty = rust_c(self.abi, c); rust_params.push(format!("p{k}: {rust_ty}")); - let (jni, native) = jni_type(c, ty); + let (jni, native) = jni_type(self.abi, c, ty); sig.push_str(descriptor(jni)); kt_params.push(format!("p{k}: {native}")); let (field, value) = match c { @@ -1742,6 +1806,10 @@ impl<'a> Gen<'a> { Ty::Enum(_) => ("i", format!("p{k}")), Ty::Handle { .. } => ("j", format!("p{k} as usize as jlong")), Ty::Buf => ("l", format!("unsafe {{ rt::lent(env, p{k}) }}")), + Ty::Struct(_) if let Some(opaque) = opaque_of(self.abi, c) => match word(&opaque) { + true => ("j", to_kotlin(&opaque, &format!("p{k}"))), + false => ("l", to_kotlin(&opaque, &format!("p{k}"))), + }, _ => ( "l", format!("{{ let mut w = rt::Writer::default(); w.put(p{k}); w.array(env) }}"), @@ -1771,7 +1839,7 @@ impl<'a> Gen<'a> { (Some(_), [Ty::Out(ok), _]) => (**ok).clone(), (Some(_), _) => Ty::Void, }; - let (ret_jni, ret_native) = jni_type(&c_ok, ret); + let (ret_jni, ret_native) = jni_type(self.abi, &c_ok, ret); sig.push_str(descriptor(ret_jni)); let call_kind = call_method(ret_jni); let rust_ret = rust_c(self.abi, c_ret); @@ -1783,8 +1851,12 @@ impl<'a> Gen<'a> { Ty::Char => "ret as u32".to_string(), // A buffer Rust frees, as `buf_copy` makes one. Ty::Buf => "crate::rt::Buf::vec(rt::bytes(env, ret).unwrap_or_default())".to_string(), - // A handle Kotlin cloned for Rust to free. - Ty::Handle { .. } => "ret as usize as *const c_void".to_string(), + Ty::Struct(_) if let Some(opaque) = opaque_of(self.abi, &c_ok) => from_kotlin(&opaque, "ret"), + // Encoded, as an option of an opaque value, which names no array or text to refer to. + Ty::Struct(_) => { + "rt::Reader::new(env, &rt::bytes(env, ret).unwrap_or_default(), ::std::ptr::null_mut()).get()" + .to_string() + } _ => unreachable!("a callback or closure returns no {c_ok:?}"), }; let back = match thrown { @@ -1972,7 +2044,7 @@ impl<'a> Gen<'a> { .iter() .any(|p| matches!(&p.ty, Type::Ref(..)) && p.ty.written()); self.room = room( - export.call.params.iter().map(|(_, c)| c), + export.call.params.iter().map(|(_, c)| c_slots(abi, c)).sum(), export.future.is_some(), written, ); @@ -2110,7 +2182,7 @@ impl<'a> Gen<'a> { // A record returned directly crosses as its scalars, set into slots the call takes, // and its other fields encoded into the `byte[]` it returns. let flat = match (&export.future, outs.is_empty()) { - (None, true) if self.room > 0 => returned_record(&ok), + (None, true) if self.room > 0 => returned_record(&ok).filter(|(i, _)| abi.api.records[*i].opaque.is_none()), _ => None, }; let flat = flat.map(|(i, option)| { @@ -2231,7 +2303,7 @@ impl<'a> Gen<'a> { let mut sig = "(".to_string(); for (k, ty) in elements.iter().enumerate() { let c = abi.ty(ty); - let (jni, native) = jni_type(&c, ty); + let (jni, native) = jni_type(abi, &c, ty); fields.push(format!("p{k}: {native}")); sig.push_str(descriptor(jni)); lifted.push(self.lift(&strip_refs(ty), &c, &format!("p{k}"), false)); @@ -2245,7 +2317,7 @@ impl<'a> Gen<'a> { values.push(format!( "jvalue {{ {field}: if success {{ let ret = o_{}.assume_init(); {} }} else {{ std::mem::zeroed() }} }}", outs[k].0, - ret_expr(&c, ty) + ret_expr(abi, &c, ty) )); } let mut failure = String::new(); @@ -2304,7 +2376,7 @@ impl<'a> Gen<'a> { // A result of a scalar, a handle, or nothing returns its value directly, raising // its error as a `Native.Failed` Kotlin catches. let err_ty = function.err.as_ref().expect("a direct return is a result's"); - let (jni, native) = jni_type(&c_ok, &ok); + let (jni, native) = jni_type(abi, &c_ok, &ok); let params: Vec<(&str, &str)> = native_params.iter().map(|(j, k)| (*j, k.as_str())).collect(); let mut ptrs = Vec::new(); let mut decl = Vec::new(); @@ -2315,7 +2387,10 @@ impl<'a> Gen<'a> { let ok_name = &outs[0].0; let ret = match c_ok { Ty::Void => String::new(), - _ => format!("let ret = o_{ok_name}.assume_init();\n\t\t{}", ret_expr(&c_ok, &ok)), + _ => format!( + "let ret = o_{ok_name}.assume_init();\n\t\t{}", + ret_expr(abi, &c_ok, &ok) + ), }; let err_name = &outs[outs.len() - 1].0; self.native( @@ -2469,7 +2544,7 @@ impl<'a> Gen<'a> { true => made, false => format!("Native.__slots.get()!!.let {{ {} -> {made} }}", flat.a), }; - } else if jni_type(&export.call.ret, &ok).0 == "jbyteArray" + } else if jni_type(abi, &export.call.ret, &ok).0 == "jbyteArray" && export.call.ret != Ty::Buf && ok.block().is_none() { @@ -2500,12 +2575,12 @@ impl<'a> Gen<'a> { // Bytes cross copied or as their owner. Ty::Buf if !string(&ok) => ("jobject", "Any?"), Ty::Waker => ("jlong", "Long"), - _ => jni_type(&c_ret, &ok), + _ => jni_type(abi, &c_ret, &ok), }; let params: Vec<(&str, &str)> = native_params.iter().map(|(j, k)| (*j, k.as_str())).collect(); let ret_expr = match &c_ret { _ if returned.is_some() => "rt::new_block(env, ret)".to_string(), - _ => ret_expr(&c_ret, &ok), + _ => ret_expr(abi, &c_ret, &ok), }; self.native( &bare, @@ -2714,14 +2789,16 @@ impl<'a> Gen<'a> { for (k, field) in abi.api.records[i].fields.iter().enumerate() { let e = format!("{e}.{}", field_name(&field.name)); let value = match &field.ty { - Type::Record(j) => self.flat_in(*j, &e, (&mut *arg, &mut *n), (w, &mut *writes), r), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => { + self.flat_in(*j, &e, (&mut *arg, &mut *n), (w, &mut *writes), r) + } ty if scalar(abi, ty) => { let p = format!("p{n}"); *n += 1; let (jni, native, kt, rust) = match ty { Type::Bool => ("jboolean", "Boolean", e, p), Type::Int(int) | Type::NonZero(int) => { - let (jni, native) = jni_type(&Ty::Int(*int), ty); + let (jni, native) = jni_type(abi, &Ty::Int(*int), ty); (jni, native, to_signed(*int, &e), from_jni(*int, &p)) } Type::F32 => ("jfloat", "Float", e, p), @@ -2759,7 +2836,9 @@ impl<'a> Gen<'a> { for (k, field) in abi.api.records[i].fields.iter().enumerate() { let v = format!("{v}.f{k}"); fields.push(match &field.ty { - Type::Record(j) => self.flat_out(*j, &v, (a, &mut *slots), (r, &mut *puts)), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => { + self.flat_out(*j, &v, (a, &mut *slots), (r, &mut *puts)) + } ty if scalar(abi, ty) => { let slot = format!("{a}[{}]", slots.len()); let (rust, kt) = match ty { @@ -2810,7 +2889,7 @@ fn scalar(abi: &Abi<'_>, ty: &Type) -> bool { /// Whether the record `i`, or one it nests, holds a field that crosses encoded. fn rest(abi: &Abi<'_>, i: usize) -> bool { abi.api.records[i].fields.iter().any(|f| match &f.ty { - Type::Record(j) => rest(abi, *j), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => rest(abi, *j), ty => !scalar(abi, ty), }) } @@ -2820,7 +2899,7 @@ fn rest(abi: &Abi<'_>, i: usize) -> bool { /// arrays and text they name. fn weight(abi: &Abi<'_>, i: usize) -> usize { let fields = abi.api.records[i].fields.iter().map(|f| match &f.ty { - Type::Record(j) => weight(abi, *j) - 2 * usize::from(rest(abi, *j)), + Type::Record(j) if abi.api.records[*j].opaque.is_none() => weight(abi, *j) - 2 * usize::from(rest(abi, *j)), Type::F64 => 2, Type::Int(int) | Type::NonZero(int) if jni_int(*int) == "jlong" => 2, ty if scalar(abi, ty) => 1, @@ -2829,21 +2908,23 @@ fn weight(abi: &Abi<'_>, i: usize) -> usize { fields.sum::() + 2 * usize::from(rest(abi, i)) } -/// The JVM slots of the 255 a method takes left past what each C parameter takes -/// unflattened, a future's waker, and the array a written-back argument returns through: -/// what flattening records, then the slots a returned record crosses in, may spend. -fn room<'a>(params: impl Iterator, future: bool, written: bool) -> usize { - let unflattened: usize = params.map(c_slots).sum(); +/// The JVM slots of the 255 a method takes left past the `unflattened` its C parameters +/// take, a future's waker, and the array a written-back argument returns through: what +/// flattening records, then the slots a returned record crosses in, may spend. +fn room(unflattened: usize, future: bool, written: bool) -> usize { 255usize.saturating_sub(unflattened + usize::from(future) + usize::from(written)) } /// The JVM parameter slots the C parameter `c` takes as a native method's at most: two for -/// a `long` or `double`, none for what never crosses, five for bytes (what holds them, a -/// `long` owner, and two `int`s; text takes one), two for an encoded value's `byte[]` and -/// the arrays and text it names (a callback takes one), and one for anything else. -fn c_slots(c: &Ty) -> usize { +/// a `long` or `double`, as an opaque value Kotlin holds in a word is, none for what never +/// crosses, five for bytes (what holds them, a `long` owner, and two `int`s; text takes +/// one), two for an encoded value's `byte[]` and the arrays and text it names (a callback +/// takes one), and one for anything else, such as an opaque value's `long[]`. +fn c_slots(abi: &Abi<'_>, c: &Ty) -> usize { match c { Ty::Void | Ty::Out(_) | Ty::Data | Ty::Poll => 0, + Ty::Struct(_) | Ty::In(_) if opaque_of(abi, c).is_some_and(|o| word(&o)) => 2, + Ty::Struct(_) | Ty::In(_) if opaque_of(abi, c).is_some() => 1, Ty::Int(int) if jni_int(*int) == "jlong" => 2, Ty::F64 | Ty::Handle { .. } | Ty::Future(_) => 2, Ty::Buf => 5, @@ -2975,8 +3056,12 @@ fn abi_bound(abi: &Abi<'_>, int: Int) -> String { } /// The JNI type and the Kotlin native type of the C type `c`, of IR type `ty`. -fn jni_type(c: &Ty, ty: &Type) -> (&'static str, &'static str) { +fn jni_type(abi: &Abi<'_>, c: &Ty, ty: &Type) -> (&'static str, &'static str) { match c { + Ty::Struct(_) | Ty::In(_) if let Some(opaque) = opaque_of(abi, c) => match word(&opaque) { + true => ("jlong", "Long"), + false => ("jlongArray", "LongArray"), + }, Ty::Void => ("()", "Unit"), Ty::Bool => ("jboolean", "Boolean"), Ty::Int(int) => match jni_int(*int) { @@ -3000,9 +3085,55 @@ fn jni_type(c: &Ty, ty: &Type) -> (&'static str, &'static str) { } } +/// The opaque value the C type `c` is, by value or as a receiver. +fn opaque_of(abi: &Abi<'_>, c: &Ty) -> Option { + let name = match c { + Ty::Struct(name) => name, + Ty::In(inner) => match &**inner { + Ty::Struct(name) => name, + _ => return None, + }, + _ => return None, + }; + (0..abi.api.records.len()) + .find(|&i| &abi.record(i) == name) + .and_then(|i| abi.api.records[i].opaque) +} + +/// The property of an opaque value's class holding it. +fn holder(opaque: &Opaque) -> &'static str { + match word(opaque) { + true => "bits", + false => "words", + } +} + +/// Whether Kotlin holds `opaque` in a `Long`, with no allocation: it fits one and is +/// dense, so values Rust calls equal have equal bits, as the value class compares them. +fn word(opaque: &Opaque) -> bool { + opaque.compared() +} + +/// The Rust expression taking the opaque value Kotlin passed as `p`, as its C struct. +fn from_kotlin(opaque: &Opaque, p: &str) -> String { + match word(opaque) { + true => format!("rt::from_word({p})"), + false => format!("unsafe {{ rt::from_words::<_, {}>(env, {p}) }}", opaque.words()), + } +} + +/// The JNI value Kotlin takes the opaque value's C struct `e` as. +fn to_kotlin(opaque: &Opaque, e: &str) -> String { + match word(opaque) { + true => format!("rt::to_word({e})"), + false => format!("rt::to_words::<_, {}>(env, {e})", opaque.words()), + } +} + /// The Rust expression returning to Kotlin a value `ret` of type `ok` that C returns as `c`. -fn ret_expr(c: &Ty, ok: &Type) -> String { +fn ret_expr(abi: &Abi<'_>, c: &Ty, ok: &Type) -> String { match c { + Ty::Struct(_) if let Some(opaque) = opaque_of(abi, c) => to_kotlin(&opaque, "ret"), Ty::Void | Ty::Bool | Ty::F32 | Ty::F64 | Ty::Enum(_) => "ret".to_string(), Ty::Int(int) => format!("ret as {}", jni_int(*int)), Ty::Char => "ret as jint".to_string(), @@ -3047,6 +3178,7 @@ fn descriptor(jni: &str) -> &'static str { "jfloat" => "F", "jdouble" => "D", "jbyteArray" => "[B", + "jlongArray" => "[J", _ => "Ljava/lang/Object;", } } @@ -3373,11 +3505,9 @@ mod tests { /// leave out. #[test] fn room_reserves_writeback() { - use super::Ty; - let params = [Ty::F64, Ty::Bool]; - assert_eq!(super::room(params.iter(), false, false), 252); - assert_eq!(super::room(params.iter(), false, true), 251); - assert_eq!(super::room(params.iter(), true, true), 250); + assert_eq!(super::room(3, false, false), 252); + assert_eq!(super::room(3, false, true), 251); + assert_eq!(super::room(3, true, true), 250); } /// Every class the runtime declares is taken, so an exported type of its name is renamed diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index 7f4a6962a..3fc202248 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -15,7 +15,7 @@ use std::collections::{BTreeMap, BTreeSet}; use rustc_span::{DUMMY_SP, Span}; use crate::abi::{Abi, Ty}; -use crate::ir::{Access, Api, Constant, Function, Literal, On, Traits, Type}; +use crate::ir::{Access, Api, Constant, Function, Literal, On, Opaque, Traits, Type}; use crate::merge::{self, Maps}; /// Each language's public names, keyed by the item's kind and Rust path. @@ -560,6 +560,23 @@ fn changed(base: &Index<'_>, now: &Index<'_>, maps: &Maps, ours: &Item<'_>, thei match (*ours, *theirs) { (Item::Record(i), Item::Record(j)) => { traits(b.records[i].traits, n.records[j].traits); + // An opaque value's bytes are its C struct, which every binding sizes, and + // whether a word holds it and compares it is Kotlin's type for it. + if let (Some(o), Some(p)) = (b.records[i].opaque, n.records[j].opaque) { + if (o.size, o.align) != (p.size, p.align) { + changes.push(format!( + "its bytes: {} aligned to {}, and now {} aligned to {}", + o.size, o.align, p.size, p.align + )); + } + let held = |o: Opaque| match o.compared() { + true => "a word compared by its bits", + false => "bytes compared by Rust", + }; + if o.compared() != p.compared() { + changes.push(format!("how it is held: {}, and now {}", held(o), held(p))); + } + } let mut ours = merge::bare_record(&b.records[i]); maps.fields(&mut ours.fields); maps.types(&mut ours.args); @@ -839,6 +856,8 @@ mod tests { tuple: false, methods: vec![], traits: Traits::default(), + opaque: None, + padding: Default::default(), } } diff --git a/ffi/src/merge.rs b/ffi/src/merge.rs index 7dc494547..d57698fc4 100644 --- a/ffi/src/merge.rs +++ b/ffi/src/merge.rs @@ -202,6 +202,14 @@ pub fn bare_record(r: &Record) -> Record { r.on = On::ALL; r.methods.clear(); r.fields.iter_mut().for_each(|f| f.docs.clear()); + // Merged apart: one binding holds the largest, and each target zeroes its own + // padding. + if let Some(opaque) = &mut r.opaque { + opaque.size = 0; + opaque.align = 0; + opaque.dense = true; + } + r.padding.clear(); }) } @@ -416,6 +424,12 @@ impl Merged { self.differ(what, (ours.on, ours.span), (record.on, record.span)); } self.api.records[at].on.0 |= record.on.0; + if let (Some(ours), Some(theirs)) = (&mut self.api.records[at].opaque, record.opaque) { + ours.size = ours.size.max(theirs.size); + ours.align = ours.align.max(theirs.align); + ours.dense &= theirs.dense; + } + self.api.records[at].padding.extend(record.padding); let methods = std::mem::take(&mut self.api.records[at].methods); self.api.records[at].methods = self.functions(methods, record.methods); } diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 6b1725b8f..fdb508a9d 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -909,6 +909,7 @@ impl<'a> Gen<'a> { let bare = self.bare(c).to_string(); let class = self.class(c); let object = matches!(ty, Type::Object(_)); + let opaque = matches!(ty, Type::Record(i) if self.abi.api.records[*i].opaque.is_some()); // The receiver as the glue reads it: a handle, held for the call, or a value. let this = match object { true => self.lift( @@ -956,18 +957,27 @@ impl<'a> Gen<'a> { "\tdef clone(self) -> {class}:\n\t\t\"\"\"{docs}\"\"\"\n\t\t...\n" )); } - if object && t.eq { - let other = self.lift( - &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), - "a[1]", - "false", - "false", - ); + // An opaque value compares through Rust, as an object does; a record's fields + // compare as a dataclass's. + if (object || opaque) && t.eq { + let other = match object { + true => self.lift( + &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), + "a[1]", + "false", + "false", + ), + false => self.lift(ty, "a[1]", "false", "false"), + }; + let (a, b) = match object { + true => ("this", "other"), + false => ("&this", "&other"), + }; self.native( &format!("{bare}_eq"), 2, &format!( - "let this = {this}?;\n\t\tlet other = {other}?;\n\t\tlet ret = rt::call(|| crate::{c}_eq(this, other));\n\t\ts.done();\n\t\trt::bool_out(ret)" + "let this = {this}?;\n\t\tlet other = {other}?;\n\t\tlet ret = rt::call(|| crate::{c}_eq({a}, {b}));\n\t\ts.done();\n\t\trt::bool_out(ret)" ), ); // Python makes a class that defines `__eq__` alone unhashable, as `Eq` without `Hash` is. @@ -1056,6 +1066,9 @@ impl<'a> Gen<'a> { let t = self.register(&c, name.clone()); let ty = Type::Record(i); let mut body = Body::default(); + if record.opaque.is_some() { + body.python.push("\t__slots__ = ()\n".to_string()); + } self.traits(&ty, &c, record.traits, &mut body); let owner = Ty::Struct(c.clone()); let data: Vec = record.fields.iter().map(|f| field_name(&f.name)).collect(); @@ -1063,6 +1076,23 @@ impl<'a> Gen<'a> { if record.fields.iter().any(|f| own_eq(abi, &f.ty)) { body.python.push(ieee_eq(&name, &data)); } + if record.opaque.is_some() { + let text = format!( + "{rust}\n\n{}\n\nIts bytes, which only Rust makes or reads: copy it, but never make one.", + record.docs + ); + let _ = write!( + self.py, + "class {name}(_n.Value):\n{}{}\n\n", + docstring(text.trim(), "\t"), + body.members().join("\n") + ); + let _ = write!( + self.glue, + "fn r_{c}(o: *mut PyObject, _s: &mut rt::Scope, _take: bool) -> R {{\n\trt::value_in(o, {t})\n}}\n\nfn w_{c}(v: crate::{c}) -> R {{\n\trt::value_out({t}, v)\n}}\n\n" + ); + return; + } let fields: String = record .fields .iter() @@ -1426,21 +1456,10 @@ impl<'a> Gen<'a> { /// The glue's expression, an `R`, of the Python object `o` a closure returns as the /// C value of `ty` that Rust then owns: a string copied, as `_buf_copy` makes - /// one, and a new handle to a copy of an object, made without its `Clone`, so the Python - /// object keeps its own. + /// one. fn given(&mut self, ty: &Type, o: &str) -> String { - let abi = self.abi; match ty { Type::String => format!("rt::str_given({o})"), - Type::Object(i) => format!( - "rt::hold({o}, {}, s, false).map(|h| crate::{}_copy(h))", - self.t(&abi.object(*i)), - abi.object(*i) - ), - Type::Option(inner) if matches!(**inner, Type::Object(_)) => { - let some = self.given(inner, o); - format!("(if rt::is_none({o}) {{ Ok(std::ptr::null()) }} else {{ {some} }})") - } _ => self.lift(ty, o, "false", "false"), } } diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 4bd5ba40c..82ff4b02e 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -8,10 +8,11 @@ //! handle, freed in `deinit`, so Swift's compiler checks each move and borrow Rust makes: //! `self` is `consuming`, `&mut self` `mutating`, a parameter `consuming`, `borrowing`, or //! `inout`, a tuple holding one a struct of its own, and an array of them swift-collections' -//! `UniqueArray`. An object that is `Copy` or thrown as an error is a final class instead, -//! since `Swift.Error` requires a copyable type, whose handle a call holds at run time. A -//! record is a struct; an enum a Swift enum, with -//! its data as associated values; a trait a protocol; a closure a function type; a future +//! `UniqueArray`. An object that is `Copy`, which only one C may change is, or thrown as an +//! error is a final class instead, since `Swift.Error` requires a copyable type, whose +//! handle a call holds at run time. A record is a struct, and any other `Copy` type with +//! private fields a struct over the bytes of its C struct, which only Rust makes or reads; +//! an enum a Swift enum, with its data as associated values; a trait a protocol; a closure a function type; a future //! an `async` function; an error a thrown `Swift.Error`. The runtime they share, //! [`runtime/rt.swift`](../runtime/rt.swift), holds the classes' handles, the arena a call //! lowers nested values into, and the signal a future parks on. @@ -880,6 +881,9 @@ impl<'a> Gen<'a> { let name = self.class(&c); let rust = abi.api.rust(&record.path, &record.args); let ty = Type::Record(i); + if record.opaque.is_some() { + return self.opaque(i); + } let fields: Vec<(String, String)> = (record.fields.iter()) .map(|f| (field_name(&f.name), self.sw(&f.ty))) .collect(); @@ -939,6 +943,37 @@ impl<'a> Gen<'a> { ); } + /// A `Copy` type with private fields: a struct over the bytes of its C struct, which + /// only Rust makes or reads. Swift copies it as it copies the bytes. + fn opaque(&mut self, i: usize) { + let abi = self.abi; + let record = &abi.api.records[i]; + let c = abi.record(i); + let name = self.class(&c); + let rust = abi.api.rust(&record.path, &record.args); + let ty = Type::Record(i); + let mut members = Vec::new(); + let mut body = vec![format!( + "\t/// Its bytes, which only Rust reads.\n\tlet c_: {c}\n\n\tprivate init(c_: {c}) {{\n\t\tself.c_ = c_\n\t}}\n" + )]; + let mut protocols = vec!["Sendable"]; + self.traits(&ty, &c, record.traits, &mut body, &mut members, &mut protocols); + self.methods(&record.methods, &Ty::Struct(c.clone()), &ty, &mut body, &mut members); + self.distinct(&rust, &members); + body.push(format!( + "\tstatic func _lift(_ c: {c}) -> {name} {{\n\t\t{name}(c_: c)\n\t}}\n" + )); + body.push(format!("\tfunc _lower() -> {c} {{\n\t\tc_\n\t}}\n")); + let text = format!("{rust}\n\n{}", record.docs); + let _ = write!( + self.types, + "{}public struct {name}: {} {{\n{}}}\n\n", + docs(text.trim(), ""), + protocols.join(", "), + body.join("\n") + ); + } + fn enumeration(&mut self, i: usize) { let abi = self.abi; let e = &abi.api.enums[i]; @@ -1306,8 +1341,6 @@ impl<'a> Gen<'a> { body.push(format!("defer {{ {l}.handle.end() }}")); args.push(l); } - // A `Copy` object, which Swift's class holds a copy of. - Type::Object(i) => args.push(format!("{}(raw: {}_copy({p}))", self.sw(ty), self.abi.object(*i))), ty => args.push(self.lift_lent(ty, p)), } } @@ -1353,26 +1386,14 @@ impl<'a> Gen<'a> { format!("{{ {} in\n\t{}\n}}", params.join(", "), body.join("\n\t")) } - /// The C value of `e`, of type `ty`, which a closure returns for Rust to own: a scalar - /// as it is, text in a buffer `buf_copy` made, and a copy of an object's handle. + /// The C value of `e`, of type `ty`, which a closure returns for Rust to own: a value + /// as it lowers, and text in a buffer `buf_copy` made. fn returned(&mut self, ty: &Type, e: &str) -> String { - let abi = self.abi; match ty { Type::Str | Type::String => format!("Native.owned({e})"), - // Kept alive while its handle is copied: once `.handle` is read, ARC may release - // the object, whose deinit would free the handle first. - Type::Object(i) => format!( - "withExtendedLifetime({e}) {{ r_ in r_.handle.with {{ {}_copy($0) }} }}", - abi.object(*i) - ), - Type::Option(inner) => match &**inner { - Type::Str | Type::String => format!("({e}.map {{ Native.owned($0) }} ?? Native.none)"), - Type::Object(i) => format!( - "withExtendedLifetime({e}) {{ r_ in r_?.handle.with {{ {}_copy($0) }} ?? nil }}", - abi.object(*i) - ), - _ => self.lower(ty, e), - }, + Type::Option(inner) if matches!(**inner, Type::Str | Type::String) => { + format!("({e}.map {{ Native.owned($0) }} ?? Native.none)") + } ty => self.lower(ty, e), } } diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 8ddfad885..ef1d53cee 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -236,15 +236,21 @@ static api_buf made_times(void *data, uint32_t n) { return api_buf_copy(str(text)); } -/* reissue's closures: a new ticket, and a copy of one C keeps, each Rust's to free. */ -static api_Ticket *reissue_next(void *data, const api_Ticket *ticket) { +/* reissue's closures: a new ticket, and a copy of one C keeps. */ +static api_Ticket reissue_next(void *data, api_Ticket ticket) { (void)data; - return api_Ticket_new(api_Ticket_number(ticket) + 1); + return api_Ticket_new(api_Ticket_number(&ticket) + 1); } -static api_Ticket *reissue_kept(void *data, const api_Ticket *ticket) { +static api_Ticket reissue_kept(void *data, api_Ticket ticket) { (void)ticket; - return api_Ticket_copy(data); + return *(const api_Ticket *)data; +} + +/* shifted's closure: the moment 500 ticks later. */ +static api_Moment moment_after(void *data, api_Moment moment) { + (void)data; + return api_Moment_after(&moment, 500); } static uint32_t spawned_task(void *data) { @@ -267,15 +273,15 @@ static api_buf made_view(void *data, uint32_t n) { * waiter, which it wakes, then stops. Rust frees it once it drops it. */ typedef struct { int calls; - const api_Instant *start; + api_Instant start; bool freed; } driver; -static bool drive_poll(void *data, const api_Instant *now, const api_Waiter *waiter, api_Instant **out, api_buf *err) { +static bool drive_poll(void *data, api_Instant now, const api_Waiter *waiter, api_option_Instant *out, api_buf *err) { driver *d = data; d->calls++; if (d->calls == 1) { - *out = api_later(now, (api_duration){.secs = 0, .nanos = 5000000}); + *out = (api_option_Instant){.some = true, .value = api_later(now, (api_duration){.secs = 0, .nanos = 5000000})}; return true; } if (d->calls == 2) { @@ -284,7 +290,7 @@ static bool drive_poll(void *data, const api_Instant *now, const api_Waiter *wai api_waker returned = api_Waiter_waker(waiter); returned.wake(returned.data); returned.drop(returned.data); - *out = NULL; + *out = (api_option_Instant){0}; return true; } char text[32]; @@ -294,12 +300,12 @@ static bool drive_poll(void *data, const api_Instant *now, const api_Waiter *wai } /* A drive that only ever waits. */ -static bool drive_idle(void *data, const api_Instant *now, const api_Waiter *waiter, api_Instant **out, api_buf *err) { +static bool drive_idle(void *data, api_Instant now, const api_Waiter *waiter, api_option_Instant *out, api_buf *err) { (void)data; (void)now; (void)waiter; (void)err; - *out = NULL; + *out = (api_option_Instant){0}; return true; } @@ -415,17 +421,13 @@ static void tail(api_foreign_reader reader) { static bool closures(void) { print("made ", api_made(3, made_times, NULL), "\n"); - /* A closure taking and returning `Copy` objects copies them, never running their `Clone`. */ - api_Ticket *kept = api_Ticket_new(9); - api_Ticket *seven = api_Ticket_new(7); - api_Ticket *given = api_reissue(seven, reissue_next, NULL); - api_Ticket *held = api_reissue(seven, reissue_kept, kept); - printf("reissued %u %u, cloned %u\n", (unsigned)api_Ticket_number(given), (unsigned)api_Ticket_number(held), + /* A closure taking and returning `Copy` values copies them, never running their `Clone`. */ + api_Ticket kept = api_Ticket_new(9); + api_Ticket seven = api_Ticket_new(7); + api_Ticket given = api_reissue(seven, reissue_next, NULL); + api_Ticket held = api_reissue(seven, reissue_kept, &kept); + printf("reissued %u %u, cloned %u\n", (unsigned)api_Ticket_number(&given), (unsigned)api_Ticket_number(&held), (unsigned)api_Ticket_clones()); - api_Ticket_free(held); - api_Ticket_free(given); - api_Ticket_free(seven); - api_Ticket_free(kept); /* A kept closure, called on another thread after the call that took it returned. */ bool spawned_freed = false; @@ -437,7 +439,7 @@ static bool closures(void) { } api_waker waker = {.wake = wake, .drop = drop, .data = NULL}; - api_Instant *start = api_now(); + api_Instant start = api_now(); driver d = {.start = start}; api_drive_future *drive = api_drive(start, drive_poll, &d, drive_free); api_buf stopped; @@ -454,7 +456,6 @@ static bool closures(void) { ok = ok && api_drive_poll(drive, waker, &stopped) == API_POLL_PENDING && !idle.freed; api_drive_future_free(drive); ok = ok && idle.freed; - api_Instant_free(start); memory hello = {.bytes = "hello world", .len = 11}; tail((api_foreign_reader){.read = memory_read, .seek = memory_seek, .data = &hello}); @@ -1011,34 +1012,78 @@ static int script(void) { print("", api_describe(events[i]), "\n"); } - /* A `Copy` handle passed by value is copied, so it stays usable. */ - api_Stamp *stamp = api_Stamp_from_millis(2500); - printf("secs %llu\n", (unsigned long long)api_Stamp_secs(stamp)); - api_Stamp *later = api_Stamp_from_millis(3000); - printf("later %s\n", api_Stamp_later(later, stamp) ? "true" : "false"); - api_Stamp_free(later); - printf("secs %llu\n", (unsigned long long)api_Stamp_secs(stamp)); - api_Message *message = api_Message_new(stamp, str("body")); - api_Stamp_free(stamp); + /* A `Copy` value passed by value is copied, so it stays usable, and frees nothing. */ + api_Stamp stamp = api_Stamp_from_millis(2500); + printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&stamp)); + api_Stamp later = api_Stamp_from_millis(3000); + printf("later %s\n", api_Stamp_later(&later, stamp) ? "true" : "false"); + printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&stamp)); + api_Stamp rounded = api_Stamp_whole(&stamp); + print("whole ", api_Stamp_debug(&rounded), "\n"); + api_Message *message = api_Message_new((api_option_Stamp){.some = true, .value = stamp}, str("body")); print("payload ", api_Message_payload_get(message), "\n"); - api_Stamp *read = api_Message_stamp_get(message); - print("stamp ", api_Stamp_debug(read), "\n"); - api_Stamp_free(read); + api_option_Stamp read = api_Message_stamp_get(message); + print("stamp ", api_Stamp_debug(&read.value), "\n"); printf("sent %s\n", api_Message_sent(message) ? "true" : "false"); - /* Setters assign in place: the bytes are copied in, and a `Copy` handle copied. */ - api_Stamp *reset = api_Stamp_from_millis(4000); - api_Message_stamp_set(message, reset); + /* Setters assign in place: the bytes are copied in, and a `Copy` value copied. */ + api_Stamp reset = api_Stamp_from_millis(4000); + api_Message_stamp_set(message, (api_option_Stamp){.some = true, .value = reset}); api_Message_payload_set(message, str("edited")); print("payload ", api_Message_payload_get(message), "\n"); read = api_Message_stamp_get(message); - print("stamp ", api_Stamp_debug(read), "\n"); - api_Stamp_free(read); - printf("secs %llu\n", (unsigned long long)api_Stamp_secs(reset)); - api_Stamp_free(reset); - api_Message_stamp_set(message, NULL); - printf("stamp %s\n", api_Message_stamp_get(message) != NULL ? "true" : "false"); + print("stamp ", api_Stamp_debug(&read.value), "\n"); + printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&reset)); + api_Message_stamp_set(message, (api_option_Stamp){0}); + printf("stamp %s\n", api_Message_stamp_get(message).some ? "true" : "false"); api_Message_free(message); + /* A value past a word, copied across: a closure takes and returns one by value. */ + api_option_Moment made = api_Moment_new(1500, 1000); + api_Moment moment = made.value; + printf("moment %llu %s\n", (unsigned long long)api_Moment_millis(&moment), + api_Moment_new(1, 0).some ? "false" : "true"); + api_Moment shifted = api_shifted(moment, moment_after, NULL); + api_Moment later_moment = api_Moment_after(&moment, 500); + printf("shifted %llu %s\n", (unsigned long long)api_Moment_millis(&shifted), + api_Moment_eq(&shifted, &later_moment) ? "true" : "false"); + api_Moment wrapped = api_Moment_after(&moment, UINT64_MAX); + printf("wrapped %llu %s\n", (unsigned long long)api_Moment_millis(&wrapped), + api_Moment_eq(&moment, &shifted) ? "true" : "false"); + + /* A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. */ + api_Gap gap = api_Gap_new(7, 3); + api_Gap same_gap = api_Gap_new(7, 3); + printf("gap %llu %s\n", (unsigned long long)api_Gap_sum(&gap), api_Gap_eq(&gap, &same_gap) ? "true" : "false"); + /* Its `u64` then its `u8`, so every byte past the ninth is padding Rust made `0xff`. */ + for (size_t n = 9; n < sizeof gap; n++) { + if (((const unsigned char *)&gap)[n] != 0) { + fprintf(stderr, "gap's padding byte %zu is not zero\n", n); + return 1; + } + } + api_Berth *berth = api_Berth_new((api_option_u32){.some = true, .value = 4}); + api_Berth *empty_berth = api_Berth_new((api_option_u32){0}); + api_option_u32 berth_at = api_Berth_at(berth); + printf("berth Some(%u) %s\n", berth_at.value, api_Berth_at(empty_berth).some ? "Some" : "None"); + api_Berth_free(empty_berth); + api_Berth_free(berth); + + /* A `Copy` type changed in place is a handle, which a call taking it by value copies. */ + api_Score *score = api_Score_of(2); + api_Score *step = api_Score_of(3); + api_Score_add(score, step); + api_Score_add(score, step); + api_Score *doubled_score = api_Score_doubled(step); + printf("score %llu %llu %llu\n", (unsigned long long)api_Score_count(score), + (unsigned long long)api_Score_count(doubled_score), (unsigned long long)api_Score_count(step)); + api_Weighted weighted = {.score = api_Score_clone(step), .weight = 4}; + printf("weighted %llu %llu\n", (unsigned long long)api_Weighted_total(&weighted), + (unsigned long long)(api_Score_count(weighted.score) + api_Score_count(step))); + api_Weighted_free(&weighted); + api_Score_free(doubled_score); + api_Score_free(step); + api_Score_free(score); + static const api_Log_vtable print_log = {.free = log_free, .log = log_line}; uint8_t levels[] = {1, 2, 3, 4}; api_buf lines = {.ptr = levels, .len = sizeof levels}; @@ -1803,19 +1848,17 @@ static int script(void) { api_vec_tuple_string_Json_free(sections); /* An instant only Rust makes, which C copies into each call and compares. */ - api_Instant *began = api_now(); - api_Instant *ended = api_later(began, (api_duration){.secs = 1, .nanos = 500000000}); - api_Instant *same = api_later(began, (api_duration){.secs = 0, .nanos = 0}); + api_Instant began = api_now(); + api_Instant ended = api_later(began, (api_duration){.secs = 1, .nanos = 500000000}); + api_Instant same = api_later(began, (api_duration){.secs = 0, .nanos = 0}); api_duration forward = api_since(ended, began); api_duration backward = api_since(began, ended); printf("instants %llu %llu %s %s\n", (unsigned long long)(forward.secs * 1000 + forward.nanos / 1000000), - (unsigned long long)(backward.secs * 1000 + backward.nanos / 1000000), api_Instant_eq(began, same) ? "true" : "false", - api_Instant_eq(began, ended) ? "true" : "false"); - /* An instant lent inside an `Option`, which takes no handle. */ - printf("has_instant %s %s\n", api_has_instant(began) ? "true" : "false", api_has_instant(NULL) ? "true" : "false"); - api_Instant_free(same); - api_Instant_free(ended); - api_Instant_free(began); + (unsigned long long)(backward.secs * 1000 + backward.nanos / 1000000), api_Instant_eq(&began, &same) ? "true" : "false", + api_Instant_eq(&began, &ended) ? "true" : "false"); + /* An instant lent inside an `Option`, which C copies. */ + printf("has_instant %s %s\n", api_has_instant((api_option_Instant){.some = true, .value = began}) ? "true" : "false", + api_has_instant((api_option_Instant){.some = false}) ? "true" : "false"); /* tracing's level, and its filter as an optional level whose none is `OFF`. */ api_Verbosity *verbosity = api_Verbosity_new(API_LEVEL_WARN); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index d3536345c..ba6ecc56d 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -559,27 +559,56 @@ int script() { print("", api::describe(event), "\n"); } - // A `Copy` handle passed by value is copied, so it stays usable. + // A `Copy` value passed by value is copied, so it stays usable. { api::Stamp stamp = api::Stamp::from_millis(2500); std::printf("secs %llu\n", ull(stamp.secs())); std::printf("later %s\n", yes(api::Stamp::from_millis(3000).later(stamp))); std::printf("secs %llu\n", ull(stamp.secs())); - api::Message message = api::Message::new_(&stamp, "body"); + print("whole ", stamp.whole().debug(), "\n"); + api::Message message = api::Message::new_(stamp, "body"); print("payload ", message.payload_get(), "\n"); print("stamp ", message.stamp_get()->debug(), "\n"); std::printf("sent %s\n", yes(message.sent())); - // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. + // Setters assign in place: the bytes are copied in, and a `Copy` value copied. api::Stamp reset = api::Stamp::from_millis(4000); - message.stamp_set(&reset); + message.stamp_set(reset); message.payload_set("edited"); print("payload ", message.payload_get(), "\n"); print("stamp ", message.stamp_get()->debug(), "\n"); std::printf("secs %llu\n", ull(reset.secs())); - message.stamp_set(nullptr); + message.stamp_set(std::nullopt); std::printf("stamp %s\n", yes(message.stamp_get().has_value())); } + // A value past a word, copied across: a closure takes and returns one by value. + { + api::Moment moment = *api::Moment::new_(1500, 1000); + std::printf("moment %llu %s\n", ull(moment.millis()), yes(!api::Moment::new_(1, 0).has_value())); + api::Moment shifted = api::shifted(moment, [](const api::Moment &m) { return m.after(500); }); + std::printf("shifted %llu %s\n", ull(shifted.millis()), yes(shifted == moment.after(500))); + std::printf("wrapped %llu %s\n", ull(moment.after(UINT64_MAX).millis()), yes(moment == shifted)); + } + + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + { + api::Gap gap = api::Gap::new_(7, 3); + std::printf("gap %llu %s\n", ull(gap.sum()), yes(gap == api::Gap::new_(7, 3))); + api::Berth berth = api::Berth::new_(4u); + std::printf("berth Some(%u) %s\n", *berth.at(), api::Berth::new_(std::nullopt).at() ? "Some" : "None"); + } + + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + { + api::Score score = api::Score::of(2); + api::Score step = api::Score::of(3); + score.add(step); + score.add(step); + std::printf("score %llu %llu %llu\n", ull(score.count()), ull(step.doubled().count()), ull(step.count())); + api::Weighted weighted{step.clone(), 4}; + std::printf("weighted %llu %llu\n", ull(weighted.total()), ull(weighted.score.count() + step.count())); + } + std::uint8_t levels[] = {1, 2, 3, 4}; std::printf("kept %u\n", api::emit(std::make_unique(), api::view(levels, sizeof levels))); { @@ -1169,8 +1198,8 @@ int script() { auto ended = api::later(began, {1, 500000000}); std::printf("instants %llu %llu %s %s\n", millis(api::since(ended, began)), millis(api::since(began, ended)), began == api::later(began, {0, 0}) ? "true" : "false", began == ended ? "true" : "false"); - // An instant lent inside an `Option`, which takes no handle. - std::printf("has_instant %s %s\n", api::has_instant(&began) ? "true" : "false", api::has_instant(nullptr) ? "true" : "false"); + // An instant lent inside an `Option`, which C++ copies. + std::printf("has_instant %s %s\n", api::has_instant(began) ? "true" : "false", api::has_instant(std::nullopt) ? "true" : "false"); } // tracing's level, and its filter as an optional level whose empty is `OFF`. { @@ -1186,15 +1215,13 @@ int script() { // Closures C++ implements that return what Rust then owns, or that Rust keeps. print("made ", api::made(3, [](std::uint32_t n) { return std::to_string(n) + " times"; }), "\n"); - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. { api::Ticket kept = api::Ticket::new_(9); std::uint32_t given = api::reissue(api::Ticket::new_(7), [](const api::Ticket &t) { return api::Ticket::new_(t.number() + 1); }) .number(); - std::uint32_t held = api::reissue(api::Ticket::new_(7), [&kept](const api::Ticket &) { - return std::move(kept); - }).number(); + std::uint32_t held = api::reissue(api::Ticket::new_(7), [&kept](const api::Ticket &) { return kept; }).number(); std::printf("reissued %u %u, cloned %u\n", static_cast(given), static_cast(held), static_cast(api::Ticket::clones())); } @@ -1692,7 +1719,7 @@ int lifetimes() { { api::buf payload; { - api::Message message = api::Message::new_(nullptr, "kept"); + api::Message message = api::Message::new_(std::nullopt, "kept"); payload = message.payload_get(); } check(payload.str() == "kept", "bytes outlive the object they came from"); diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index f491961b9..40e58a309 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -337,6 +337,7 @@ Future script() async { print('secs ${stamp.secs()}'); print('later ${api.Stamp.fromMillis(3000).later(stamp)}'); print('secs ${stamp.secs()}'); + print('whole ${stamp.whole().debug()}'); final message = api.Message(stamp, utf8.encode('body')); print('payload ${text(message.payloadGet)}'); print('stamp ${message.stampGet!.debug()}'); @@ -351,6 +352,28 @@ Future script() async { message.stampSet(null); print('stamp ${message.stampGet != null}'); + // A value past a word, copied across: a closure takes and returns one by value. + final moment = api.Moment.new_(1500, 1000)!; + print('moment ${moment.millis()} ${api.Moment.new_(1, 0) == null}'); + final shifted = api.shifted(moment, (m) => m.after(500)); + print('shifted ${shifted.millis()} ${shifted == moment.after(500)}'); + print('wrapped ${moment.after(-1).millis()} ${moment == shifted}'); + + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + final gap = api.Gap(7, 3); + print('gap ${gap.sum()} ${gap == api.Gap(7, 3)}'); + final berth = api.Berth(4); + print('berth Some(${berth.at()}) ${api.Berth(null).at() == null ? 'None' : 'Some'}'); + + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + final score = api.Score.of(2); + final increment = api.Score.of(3); + score.add(increment); + score.add(increment); + print('score ${score.count()} ${increment.doubled().count()} ${increment.count()}'); + final weighted = api.Weighted(score: increment, weight: 4); + print('weighted ${weighted.total()} ${weighted.score.count() + increment.count()}'); + print('kept ${api.emit(PrintLog(), bytes([1, 2, 3, 4]))}'); final logger = api.Logger(PrintLog()); print('write ${logger.write('hi')}'); @@ -686,15 +709,12 @@ Future script() async { for (final section in sections) { section.$2.close(); } - // An instant only Rust makes, which each call copies out of its handle, and compares. + // An instant only Rust makes, which each call copies, and compares. final began = api.now(); final ended = api.later(began, const Duration(milliseconds: 1500)); final same = api.later(began, Duration.zero); print('instants ${api.since(ended, began).inMilliseconds} ${api.since(began, ended).inMilliseconds} ${began == same} ${began == ended}'); print('has_instant ${api.hasInstant(began)} ${api.hasInstant(null)}'); - began.close(); - ended.close(); - same.close(); // tracing's level, and its filter as an optional level whose null is `OFF`. final verbosity = api.Verbosity(api.Level.warn); print('verbosity ${api.spell(verbosity.filter())} ${api.spell(api.louder(verbosity.filter()))}'); @@ -704,7 +724,7 @@ Future script() async { // A closure's string, which Rust takes. print('made ${api.made(3, (n) => '$n times')}'); - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. final nine = api.Ticket(9); final renewed = api.reissue(api.Ticket(7), (t) => api.Ticket(t.number() + 1)).number(); final copied = api.reissue(api.Ticket(7), (_) => nine).number(); @@ -733,7 +753,6 @@ Future script() async { 'a kept closure wakes its future', ); print('drive $stopped'); - start.close(); // A reader the caller implements: dart:io's file, then one whose reads fail. final dir = Directory.systemTemp.createTempSync('api'); final opened = (File('${dir.path}/tail')..writeAsStringSync('hello world')).openSync(); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index c20fdce5b..86c984eb1 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -419,25 +419,66 @@ func script() { fmt.Println(api.Describe(event)) } - // A `Copy` handle passed by value is copied, so it stays usable. + // A `Copy` value passed by value is copied, so it stays usable. stamp := api.StampFromMillis(2500) - fmt.Printf("secs %d\n", must(stamp.Secs())) - fmt.Printf("later %t\n", must(api.StampFromMillis(3000).Later(stamp))) - fmt.Printf("secs %d\n", must(stamp.Secs())) - message := must(api.NewMessage(stamp, rt.NewBytes([]byte("body")))) + fmt.Printf("secs %d\n", stamp.Secs()) + fmt.Printf("later %t\n", api.StampFromMillis(3000).Later(stamp)) + fmt.Printf("secs %d\n", stamp.Secs()) + fmt.Printf("whole %s\n", stamp.Whole().GoString()) + message := api.NewMessage(&stamp, rt.NewBytes([]byte("body"))) fmt.Printf("payload %s\n", must(message.PayloadGet()).Copy()) fmt.Printf("stamp %s\n", must(message.StampGet()).GoString()) fmt.Printf("sent %t\n", must(message.Sent())) - // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. + // Setters assign in place: the bytes are copied in, and a `Copy` value copied. reset := api.StampFromMillis(4000) - ok(message.StampSet(reset)) + ok(message.StampSet(&reset)) ok(message.PayloadSet(rt.NewBytes([]byte("edited")))) fmt.Printf("payload %s\n", must(message.PayloadGet()).Copy()) fmt.Printf("stamp %s\n", must(message.StampGet()).GoString()) - fmt.Printf("secs %d\n", must(reset.Secs())) + fmt.Printf("secs %d\n", reset.Secs()) ok(message.StampSet(nil)) fmt.Printf("stamp %t\n", must(message.StampGet()) != nil) + // A value past a word, copied across: a closure takes and returns one by value. + moment := *api.NewMoment(1500, 1000) + fmt.Printf("moment %d %t\n", moment.Millis(), api.NewMoment(1, 0) == nil) + shifted := api.Shifted(moment, func(m api.Moment) api.Moment { return m.After(500) }) + fmt.Printf("shifted %d %t\n", shifted.Millis(), shifted.Equal(moment.After(500))) + fmt.Printf("wrapped %d %t\n", moment.After(math.MaxUint64).Millis(), moment.Equal(shifted)) + + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + gap := api.NewGap(7, 3) + fmt.Printf("gap %d %t\n", gap.Sum(), gap.Equal(api.NewGap(7, 3))) + // Its `u64` then its `u8`, so every byte past the ninth is padding Rust made `0xff`. + padding := unsafe.Slice((*byte)(unsafe.Pointer(&gap.Bytes_)), unsafe.Sizeof(gap.Bytes_))[9:] + for n, b := range padding { + if b != 0 { + panic(fmt.Sprintf("gap's padding byte %d is not zero", n+9)) + } + } + four := uint32(4) + berth := api.NewBerth(&four) + empty := api.NewBerth(nil) + berthAt := *must(berth.At()) + emptyAt := "Some" + if must(empty.At()) == nil { + emptyAt = "None" + } + fmt.Printf("berth Some(%d) %s\n", berthAt, emptyAt) + berth.Close() + empty.Close() + + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + score := api.ScoreOf(2) + step := api.ScoreOf(3) + ok(score.Add(step)) + ok(score.Add(step)) + fmt.Printf("score %d %d %d\n", must(score.Count()), must(must(step.Doubled()).Count()), must(step.Count())) + weighted := api.Weighted{Score: step, Weight: 4} + fmt.Printf("weighted %d %d\n", must(weighted.Total()), must(weighted.Score.Count())+must(step.Count())) + score.Close() + step.Close() + // Rust frees the implementation once it drops it, releasing Go's. fmt.Printf("kept %d\n", api.Emit(printLog{}, rt.NewBytes([]byte{1, 2, 3, 4}))) logger := api.NewLogger(printLog{}) @@ -810,16 +851,13 @@ func script() { section.V1.Close() } fmt.Printf("sections %s\n", strings.Join(sectionTexts, ", ")) - // An instant only Rust makes, which each call copies out of its handle, and compares. - var began *std.Instant = api.Now() - ended := must(api.Later(began, 1500*time.Millisecond)) - same := must(api.Later(began, 0)) - fmt.Printf("instants %d %d %t %t\n", must(api.Since(ended, began)).Milliseconds(), must(api.Since(began, ended)).Milliseconds(), - must(began.Equal(same)), must(began.Equal(ended))) - fmt.Printf("has_instant %t %t\n", must(api.HasInstant(began)), must(api.HasInstant(nil))) - began.Close() - ended.Close() - same.Close() + // An instant only Rust makes, which each call copies, and compares. + var began std.Instant = api.Now() + ended := api.Later(began, 1500*time.Millisecond) + same := api.Later(began, 0) + fmt.Printf("instants %d %d %t %t\n", api.Since(ended, began).Milliseconds(), api.Since(began, ended).Milliseconds(), + began.Equal(same), began.Equal(ended)) + fmt.Printf("has_instant %t %t\n", api.HasInstant(&began), api.HasInstant(nil)) // tracing's level, and its filter as an optional level whose nil is `OFF`. verbosity := api.NewVerbosity(tracingcore.LevelWarn) fmt.Printf("verbosity %s %s\n", api.Spell(must(verbosity.Filter())), api.Spell(api.Louder(must(verbosity.Filter())))) @@ -829,34 +867,31 @@ func script() { verbosity.Close() fmt.Printf("made %s\n", api.Made(3, func(n uint32) string { return fmt.Sprintf("%d times", n) })) - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. nine, seven := api.NewTicket(9), api.NewTicket(7) - renewed := must(api.Reissue(seven, func(t *api.Ticket) *api.Ticket { return api.NewTicket(must(t.Number()) + 1) })) - copied := must(api.Reissue(seven, func(*api.Ticket) *api.Ticket { return nine })) - fmt.Printf("reissued %d %d, cloned %d\n", must(renewed.Number()), must(copied.Number()), api.TicketClones()) - for _, ticket := range []*api.Ticket{copied, renewed, seven, nine} { - ticket.Close() - } + renewed := api.Reissue(seven, func(t api.Ticket) api.Ticket { return api.NewTicket(t.Number() + 1) }) + copied := api.Reissue(seven, func(api.Ticket) api.Ticket { return nine }) + fmt.Printf("reissued %d %d, cloned %d\n", renewed.Number(), copied.Number(), api.TicketClones()) // A kept closure, called on another thread after the call that took it returned. spawned := api.NewSpawned(func() uint32 { return 7 }) fmt.Printf("spawned %d\n", must(spawned.Join())) // A kept closure: called again at the instant it asks for, then once its waiter wakes. driven := api.Now() calls := 0 - stopped := must(api.Drive(context.Background(), driven, func(now *std.Instant, waiter *api.Waiter) (*std.Instant, error) { + stopped := must(api.Drive(context.Background(), driven, func(now std.Instant, waiter *api.Waiter) (*std.Instant, error) { calls++ switch calls { case 1: - return must(api.Later(now, 5*time.Millisecond)), nil + later := api.Later(now, 5*time.Millisecond) + return &later, nil case 2: - fmt.Printf("drive %d\n", must(api.Since(now, driven)).Milliseconds()) + fmt.Printf("drive %d\n", api.Since(now, driven).Milliseconds()) must(waiter.Waker())() return nil, nil } return nil, fmt.Errorf("stopped after %d", calls) })) fmt.Printf("drive %s\n", stopped) - driven.Close() tail(strings.NewReader("hello world")) tail(brokenReader{}) // A seek from the start reaches the reader whole, past `math.MaxInt64`, as `Cursor` takes one. @@ -980,7 +1015,7 @@ func closures() { cancelled, cancel := context.WithCancel(ctx) done := make(chan error) go func() { - _, err := api.Drive(cancelled, start, func(*std.Instant, *api.Waiter) (*std.Instant, error) { + _, err := api.Drive(cancelled, start, func(std.Instant, *api.Waiter) (*std.Instant, error) { _ = marker[0] select { case called <- struct{}{}: @@ -999,26 +1034,21 @@ func closures() { cancel() check(errors.Is(<-done, context.Canceled), "a cancelled drive returns its context's error") released(freed, "a dropped future releases its closure") - start.Close() } // What a closure returns crosses: the instant it asks for, then its error. { start := api.Now() - later := must(api.Later(start, time.Second)) + later := api.Later(start, time.Second) var seen []int64 - stopped := must(api.Drive(ctx, start, func(now *std.Instant, _ *api.Waiter) (*std.Instant, error) { - seen = append(seen, must(api.Since(now, start)).Milliseconds()) + stopped := must(api.Drive(ctx, start, func(now std.Instant, _ *api.Waiter) (*std.Instant, error) { + seen = append(seen, api.Since(now, start).Milliseconds()) if len(seen) == 1 { - return later, nil + return &later, nil } return nil, errors.New("") })) check(stopped == "" && len(seen) == 2 && seen[1] == 1000, "a returned instant and an empty error cross") - // Rust took a clone: Go's is still its own. - check(must(later.Equal(later)), "a returned object stays Go's") - later.Close() - start.Close() } // A read's bytes and error both reach Rust, the error on the next read. @@ -1326,6 +1356,11 @@ func uncollected(n uint32, what string) { // closed, and that a second Close does nothing. func closed() { ctx := context.Background() + panics := func(f func()) (panicked any) { + defer func() { panicked = recover() }() + f() + return nil + } isClosed := func(err error, name, what string) { check(errors.Is(err, rt.ErrClosed) && err.Error() == name+" is closed", what) } @@ -1352,13 +1387,11 @@ func closed() { isClosed(registry.FirstSet(second), "Registry", "a field's setter of a closed object") check(must(second.Get()) == 2, "a failed call leaves what it would move") - // A record's method, whose receiver holds an object. - stamp := api.StampFromMillis(2500) - stamped := api.Stamped{At: stamp, Count: 3} - check(must(stamped.Total()) == 5, "a record's method reads the object it holds") - stamp.Close() - _, err = stamped.Total() - isClosed(err, "Stamp", "a record's method whose object is closed") + // A record's method, whose receiver holds a value only Rust makes: Go's zero value of + // one panics before Rust reads it. + stamped := api.Stamped{At: api.StampFromMillis(2500), Count: 3} + check(stamped.Total() == 5, "a record's method reads the value it holds") + check(panics(func() { api.Stamped{Count: 3}.Total() }) == "a zero Stamp, which only Rust makes", "a zero value panics") // An argument, alone, or nested where the call holds it through its arena. _, err = api.BumpBoth(api.NewTally(), tally) @@ -1482,7 +1515,8 @@ func bytes() { // Bytes an object returns outlive its Close: no Bytes depends on an object. body := strings.Repeat("b", 2048) - message := must(api.NewMessage(api.StampFromMillis(1), rt.NewBytes([]byte(body)))) + stamp := api.StampFromMillis(1) + message := api.NewMessage(&stamp, rt.NewBytes([]byte(body))) payload := must(message.PayloadGet()) message.Close() runtime.GC() @@ -1580,11 +1614,9 @@ func broken(which string) { case "nil reader": api.Tail(nil, 1) // An instant Go closed, which Rust cannot copy out. - case "returned closed": - start := api.Now() - api.Drive(context.Background(), start, func(now *std.Instant, _ *api.Waiter) (*std.Instant, error) { - now.Close() - return now, nil + case "returned zero": + api.Drive(context.Background(), api.Now(), func(std.Instant, *api.Waiter) (*std.Instant, error) { + return &std.Instant{}, nil }) default: check(false, "no case "+which) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 5f5a2390e..251f398e1 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -261,6 +261,7 @@ fun script() { println("secs ${stamp.secs()}") println("later ${Stamp.fromMillis(3000u).later(stamp)}") println("secs ${stamp.secs()}") + println("whole ${stamp.whole().debug()}") val message = Message.new(stamp, Bytes("body".encodeToByteArray())) println("payload ${message.payloadGet.text()}") println("stamp ${message.stampGet!!.debug()}") @@ -275,6 +276,28 @@ fun script() { message.stampSet(null) println("stamp ${message.stampGet != null}") + // A value past a word, copied across: a closure takes and returns one by value. + val moment = Moment.new(1500u, 1000u)!! + println("moment ${moment.millis()} ${Moment.new(1u, 0u) == null}") + val shifted = shifted(moment) { m -> m.after(500u) } + println("shifted ${shifted.millis()} ${shifted == moment.after(500u)}") + println("wrapped ${moment.after(ULong.MAX_VALUE).millis()} ${moment == shifted}") + + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + val gap = Gap.new(7u, 3u) + println("gap ${gap.sum()} ${gap == Gap.new(7u, 3u)}") + val berth = Berth.new(4u) + println("berth Some(${berth.at()}) ${if (Berth.new(null).at() == null) "None" else "Some"}") + + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + val score = Score.of(2u) + val step = Score.of(3u) + score.add(step) + score.add(step) + println("score ${score.count()} ${step.doubled().count()} ${step.count()}") + val weighted = Weighted(step, 4u) + println("weighted ${weighted.total()} ${weighted.score.count() + step.count()}") + println("kept ${emit(PrintLog(), Bytes(byteArrayOf(1, 2, 3, 4)))}") Logger.new(PrintLog()).use { logger -> println("write ${logger.write("hi")}") } println("dropped") @@ -540,8 +563,8 @@ fun script() { check(Numbers.zero(true).hashCode() == Numbers.zero(false).hashCode(), "a negative zero hashes as zero does") check(Measure.new(-0.0, true).hashCode() == Measure.new(0.0, false).hashCode(), "a negative zero hashes as zero does in a nested record") val ints = edges.copy(f32s = FloatArray(0), f64s = DoubleArray(0)) - val same = Numbers.edges().copy(f32s = FloatArray(0), f64s = DoubleArray(0)) - check(ints == same && ints.hashCode() == same.hashCode(), "records of arrays compare by contents") + val alike = Numbers.edges().copy(f32s = FloatArray(0), f64s = DoubleArray(0)) + check(ints == alike && ints.hashCode() == alike.hashCode(), "records of arrays compare by contents") val back = edges.reversed().reversed() check(back.copy(f32s = FloatArray(0), f64s = DoubleArray(0)) == ints, "every integer edge value crosses both ways exactly") check(back.f32s.map { it.toRawBits() } == edges.f32s.map { it.toRawBits() } && back.f64s.map { it.toRawBits() } == edges.f64s.map { it.toRawBits() }, "every float edge value crosses both ways exactly") @@ -600,15 +623,12 @@ fun script() { val jsonSections = sections(listOf(Pair("b", Json.parse("[1]")), Pair("a", Json.parse("2")))) println("sections ${jsonSections.joinToString(", ") { (key, value) -> "$key ${value.toText()}" }}") jsonSections.forEach { it.second.close() } - // An instant only Rust makes, which each call copies out of its handle, and compares. - now().use { began -> - later(began, 1500.milliseconds).use { ended -> - later(began, kotlin.time.Duration.ZERO).use { same -> - println("instants ${since(ended, began).inWholeMilliseconds} ${since(began, ended).inWholeMilliseconds} ${began == same} ${began == ended}") - println("has_instant ${hasInstant(began)} ${hasInstant(null)}") - } - } - } + // An instant only Rust makes, which each call copies, and compares. + val began = now() + val ended = later(began, 1500.milliseconds) + val same = later(began, kotlin.time.Duration.ZERO) + println("instants ${since(ended, began).inWholeMilliseconds} ${since(began, ended).inWholeMilliseconds} ${began == same} ${began == ended}") + println("has_instant ${hasInstant(began)} ${hasInstant(null)}") // tracing's level, and its filter as an optional level whose null is `OFF`. Verbosity.new(Level.WARN).use { verbosity -> println("verbosity ${spell(verbosity.filter())} ${spell(louder(verbosity.filter()))}") @@ -617,7 +637,7 @@ fun script() { } println("made ${made(3u) { n -> "$n times" }}") - // A closure taking and returning `Copy` objects copies them, never running their `Clone`. + // A closure taking and returning `Copy` values copies them, never running their `Clone`. val nine = Ticket.new(9u) val given = reissue(Ticket.new(7u)) { t -> Ticket.new(t.number() + 1u) }.number() val held = reissue(Ticket.new(7u)) { nine }.number() @@ -625,7 +645,7 @@ fun script() { // A kept closure, called on another thread after the call that took it returned. println("spawned ${Spawned.new { 7u }.join()}") // A kept closure: called again at the instant it asks for, then once its waiter wakes. - now().use { start -> + now().let { start -> var calls = 0 val stopped = runBlocking { drive(start) { now, waiter -> @@ -1016,7 +1036,7 @@ fun broken(which: String) { "stray" -> stray() // Any exception but a closure's own error, and a reader's IOException, aborts. "closure threw" -> made(1u) { throw IllegalStateException("a make failed") } - "closure failed" -> now().use { start -> + "closure failed" -> now().let { start -> runBlocking { drive(start) { _, _ -> throw IllegalStateException("a poll failed") } } } "reader threw" -> tail( @@ -1069,9 +1089,17 @@ fun readings() { } } +/** A call near the JVM's 255 parameter slots, which a record's flattening must not pass. */ +fun crowding() { + val r = reading(1u)!! + val sum = crowded(r, Stamp.fromMillis(2uL), Stamp.fromMillis(3uL), 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL, 1uL) + check(sum == r.count + 2uL + 3uL + 119uL, "a crowded call crosses every parameter: $sum") +} + fun owned() { encoded() readings() + crowding() budgeted() for (n in 1023..1025) { val payload = ByteArray(n) { it.toByte() } diff --git a/ffi/tests/api.py b/ffi/tests/api.py index fe1b73332..cad35e729 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -325,6 +325,7 @@ def edit(counter): print(f"secs {stamp.secs()}") print(f"later {flag(api.Stamp.from_millis(3000).later(stamp))}") print(f"secs {stamp.secs()}") + print(f"whole {stamp.whole()!r}") message = api.Message.new(stamp, b"body") print(f"payload {text(message.payload_get)}") print(f"stamp {message.stamp_get!r}") @@ -339,6 +340,33 @@ def edit(counter): message.stamp_set(None) print(f"stamp {flag(message.stamp_get is not None)}") + # A value past a word, copied across: a closure takes and returns one by value. + moment = api.Moment.new(1500, 1000) + print(f"moment {moment.millis()} {flag(api.Moment.new(1, 0) is None)}") + shifted = api.shifted(moment, lambda m: m.after(500)) + print(f"shifted {shifted.millis()} {flag(shifted == moment.after(500))}") + print(f"wrapped {moment.after(2**64 - 1).millis()} {flag(moment == shifted)}") + try: + api.Moment() + check(False, "Python makes no opaque value") + except TypeError: + pass + + # A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + gap = api.Gap.new(7, 3) + print(f"gap {gap.sum()} {flag(gap == api.Gap.new(7, 3))}") + berth = api.Berth.new(4) + print(f"berth Some({berth.at()}) {'None' if api.Berth.new(None).at() is None else 'Some'}") + + # A `Copy` type changed in place is a handle, which a call taking it by value copies. + score = api.Score.of(2) + increment = api.Score.of(3) + score.add(increment) + score.add(increment) + print(f"score {score.count()} {increment.doubled().count()} {increment.count()}") + weighted = api.Weighted(increment, 4) + print(f"weighted {weighted.total()} {weighted.score.count() + increment.count()}") + print(f"kept {api.emit(PrintLog(), bytes([1, 2, 3, 4]))}") logger = api.Logger.new(PrintLog()) print(f"write {flag(logger.write('hi'))}") @@ -1291,6 +1319,14 @@ def raises(error, f, what): raises(OverflowError, lambda: api.scaled(2**128, 1), "an int past u128 raises") raises(OverflowError, lambda: api.scaled(-1, 1), "a negative int for a u128 raises") + # An object reclassed in Python is still what Rust made: its methods refuse it. + stamp = api.Stamp.from_millis(5) + stamp.__class__ = api.Moment + raises(TypeError, lambda: stamp.millis(), "a Stamp reclassed as a Moment is no Moment") + counter = api.Counter.new("r", 1) + counter.__class__ = api.Berth + raises(TypeError, lambda: counter.at(), "a Counter reclassed as a Berth is no Berth") + # A list of numbers is any buffer of exactly its numbers, one dimension and contiguous, # and returns as an array of them. edges = api.Numbers.edges() diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 9917d8a67..4ea09eb64 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -406,6 +406,7 @@ func script() { print("secs \(stamp.secs())") print("later \(Stamp.fromMillis(3000).later(stamp))") print("secs \(stamp.secs())") + print("whole \(debug(stamp.whole()))") var message = Message.new(stamp, Bytes("body".utf8)) print("payload \(text(message.payloadGet))") print("stamp \(debug(message.stampGet!))") @@ -420,6 +421,28 @@ func script() { message.stampSet(nil) print("stamp \(message.stampGet != nil)") + // A value past a word, copied across: a closure takes and returns one by value. + let moment = Moment.new(1500, 1000)! + print("moment \(moment.millis()) \(Moment.new(1, 0) == nil)") + let shifted = shifted(moment) { $0.after(500) } + print("shifted \(shifted.millis()) \(shifted == moment.after(500))") + print("wrapped \(moment.after(UInt64.max).millis()) \(moment == shifted)") + + // A padded value crosses with its padding zeroed, and one whose bytes vary is a handle. + let gap = Gap.new(7, 3) + print("gap \(gap.sum()) \(gap == Gap.new(7, 3))") + let berth = Berth.new(4) + print("berth Some(\(berth.at()!)) \(Berth.new(nil).at() == nil ? "None" : "Some")") + + // A `Copy` type changed in place is a handle, which a call taking it by value copies. + let score = Score.of(2) + let step = Score.of(3) + score.add(step) + score.add(step) + print("score \(score.count()) \(step.doubled().count()) \(step.count())") + let weighted = Weighted(score: step, weight: 4) + print("weighted \(weighted.total()) \(weighted.score.count() + step.count())") + // Bound first, so Swift's reference is gone before the print. let kept = emit(PrintLog(), Bytes([1, 2, 3, 4])) print("kept \(kept)") diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 3e09d14e2..a4f6f74f5 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -577,14 +577,23 @@ fn cxx_command(link: &Link, standard: u8, plain: bool, src: &Path, exe: &Path) - if plain { cmd.args(["-fno-exceptions", "-fno-rtti"]); } - // -Wshadow stands in for MSVC's warnings that a name hides another. - cmd.args(["-pedantic", "-Wall", "-Wextra", "-Wshadow", "-Werror", "-I"]) - .arg(include) - .arg(src) - .arg(&link.staticlib) - .args(link.native) - .arg("-o") - .arg(exe); + // -Wshadow stands in for MSVC's warnings that a name hides another, and + // -Wmismatched-tags for C4099, a class declared `struct` or a struct `class`. + cmd.args([ + "-pedantic", + "-Wall", + "-Wextra", + "-Wshadow", + "-Wmismatched-tags", + "-Werror", + "-I", + ]) + .arg(include) + .arg(src) + .arg(&link.staticlib) + .args(link.native) + .arg("-o") + .arg(exe); cmd } @@ -1356,7 +1365,7 @@ fn api_go() { "api_Counter_into_label: a lent handle's value cannot move out", ), ("nil reader", "a nil io.ReadSeeker"), - ("returned closed", "Instant is closed"), + ("returned zero", "a zero Instant, which only Rust makes"), ("family", "an address that is not IPv4 has no Rust SocketAddrV4"), ("outlived", OUTLIVED), ("text", "api_greet: a string is not UTF-8"), @@ -2165,6 +2174,23 @@ fn lock() { stderr.contains("library `other`: it is now `lock`"), "no library error:\n{stderr}" ); + // A baseline an earlier export wrote, before records had their padding, still reads. + let older = dir.join("older.json"); + let mut json = String::from_utf8(before.clone()).unwrap(); + while let Some(at) = json.find(",\"padding\":{") { + let end = at + json[at..].find('}').unwrap() + 1; + json.replace_range(at..end, ""); + } + std::fs::write(&older, json).unwrap(); + let args = ["--baseline".as_ref(), older.as_os_str()]; + let read = exporter("lock", &dir.join("older"), &targets, &args, &[]) + .output() + .expect("running rust2-ffi"); + assert!( + read.status.success(), + "a baseline without padding failed:\n{}", + String::from_utf8_lossy(&read.stderr) + ); let locked = |feature: &str| { let args = ["--baseline".as_ref(), baseline.as_os_str()]; exporter("lock", &dir.join(feature), &targets, &args, &["--features", feature]) @@ -2469,6 +2495,30 @@ fn standard_names() { } } +/// The padding of a `Copy` value of a huge array is found without walking or storing its +/// bytes: a zero-sized element repeated `usize::MAX` times, or a 1 TiB array of bytes, which +/// no export returns, so it is left out only after its padding is read. An array of padded +/// elements still zeroes each element's padding. +#[test] +fn huge_arrays() { + let lib = "#[derive(Clone, Copy)]\npub struct Marker([(); usize::MAX]);\n\nimpl Marker {\n\tpub fn new() -> Self {\n\t\tSelf([(); usize::MAX])\n\t}\n}\n\npub fn take(marker: Marker) -> u32 {\n\tmarker.0.len().count_ones()\n}\n\n\ + #[derive(Clone, Copy)]\npub struct Huge([u8; 1 << 40]);\n\npub fn first(huge: &Huge) -> u8 {\n\thuge.0[0]\n}\n\n\ + #[derive(Clone, Copy)]\n#[repr(C)]\nstruct Pair {\n\ta: u8,\n\tb: u16,\n}\n\n\ + #[derive(Clone, Copy)]\npub struct Pairs([Pair; 3]);\n\nimpl Pairs {\n\tpub fn new() -> Self {\n\t\tSelf([Pair { a: 1, b: 2 }; 3])\n\t}\n\n\tpub fn sum(&self) -> u32 {\n\t\tself.0.iter().map(|p| u32::from(p.a) + u32::from(p.b)).sum()\n\t}\n}\n"; + let package = package("huge_arrays", &[("src/lib.rs", lib)]); + export_and_build(&package, &["--offline"]); + let header = std::fs::read_to_string(package.join("out/include/huge_arrays.h")).unwrap(); + assert!( + header.contains("\tuint8_t opaque[1];\n} huge_arrays_Marker;"), + "Marker is no opaque value:\n{header}" + ); + let glue = std::fs::read_to_string(package.join("out/src/lib.rs")).unwrap(); + assert!( + glue.contains("const PADDING: &[(usize, usize)] = &[(1, 1), (5, 1), (9, 1)];"), + "Pairs' padding is not each element's:\n{glue}" + ); +} + /// A facade whose lib takes the name of a foreign type's crate maps that crate's type /// through a dependency of its own, apart from the glue's dependency on the facade. #[test] diff --git a/quest/m1/ffi-export/checks-raise.md b/quest/m1/ffi-export/checks-raise.md index d54ac3930..c71fb7144 100644 --- a/quest/m1/ffi-export/checks-raise.md +++ b/quest/m1/ffi-export/checks-raise.md @@ -58,5 +58,4 @@ error types the runtimes add, and the status the C functions return. ## Related -- [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - landing it first leaves only thrown errors as Swift classes, so fewer signatures turn throwing - [Cheaper returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - rewrites the same handle word, so its baselines move with this diff --git a/quest/m1/ffi-export/copy-values.md b/quest/m1/ffi-export/copy-values.md index 8e564da8f..f4aab9b5e 100644 --- a/quest/m1/ffi-export/copy-values.md +++ b/quest/m1/ffi-export/copy-values.md @@ -1,49 +1,36 @@ -# [L] Copy types cross by value +# [M] Copy types that change cross by value ## Goal -A [rust2-ffi](/ffi/README.md) export of a type that is `Copy` and has private fields, such -as moq's `Timestamp`, crosses as an opaque value copied across the C ABI instead of an -`Arc` handle, in every generator. Making and passing one allocates and frees nothing on -either side. [The binding benchmark](/bench/ffi/README.md)'s stamped write then costs -within a few ns of its plain write in each language, against 140 to 256 ns of CPU time -more today in Kotlin, Python, Go, and Dart. +A [rust2-ffi](/ffi/README.md) export's `Copy` type with a `pub` field or a `pub` method +taking `&mut self` crosses as an opaque value, as a `Send` `Copy` type with only private +fields and value-independent padding does since +[#742](https://github.com/kixel-ai/rust2/pull/742), instead of an `Arc` handle. A setter +or `&mut self` method writes the new bytes back into the language's own copy, so +`x.add(other)` changes `x` and only `x`, as in Rust. moq has five today, each +still a handle: `stats::Traffic`, `stats::Presence`, `stats::Failures`, and +`stats::Refusals` (`add`), and `clock::WallMap` (`sample`). ## Plan -- The C ABI emits a fixed-size, aligned struct of opaque bytes with the Rust type's size - and alignment, read and written only by Rust glue; the language sees no fields, so - privacy holds. Recommended over a `#[repr(C)]` mirror of the fields, which would expose - them and break on a private layout change. -- Each language holds it as its own value type: a C struct, a C++ trivially copyable - class, a Swift struct, a Go struct, a Kotlin value class, a Python object holding bytes, - and a Dart `Struct`. Methods on it call Rust with the bytes. -- A type whose size changes is an ABI change of the export, as a field change already is. -- Prove it with the api fixture's differential tests and re-measure the bench. - -Decided with the maintainer on 2026-11-12, in the quest audit, for a size that differs by -target: since [#700](https://github.com/kixel-ai/rust2/pull/700) one binding serves every -target, moq ships 32-bit `armv7-linux-androideabi` beside 64-bit ones, and Kotlin, Python, -and Dart share one source across them. - -- ✅ The merge picks the representation from the type's size on every target. At most 8 - bytes on all of them: an inline word, such as a Kotlin value class over a `Long`, with no - allocation. Otherwise: bytes sized by the library when it loads. Rejected: always sized - at load, which allocates a small array per value in Kotlin where a `Long` is free; - refusing a type whose layout differs between targets, which would make moq change the - type, against "rust2 asks moq for no source changes". -- The IR carries no type's layout today: `ffi/src/collect.rs` reads a scalar's size and - the target's pointer width, and nothing records a struct's size or alignment. The - staged API (`--api`) gains each such type's size and alignment per target, and the merge - reads them. - -What this replaces, from [#710](https://github.com/kixel-ai/rust2/pull/710): the C ABI's -`_copy`, the IR's `Object.copy`, and Swift's class for a `Copy` object. - -Public API: how every generated API holds a `Copy` type (breaking), and the staged API's -layout fields. +- C takes `T *` for `&mut self` and writes the value back in place; C++ makes the method + non-`const`; Swift makes it `mutating`; Go takes a pointer receiver. Each writes only + the caller's copy. +- A `pub` field gets a getter and a setter over the bytes, through Rust, as an object's + field does; the bytes stay opaque. +- Prove it with the api fixture's differential tests: a `Copy` type with a `pub` field and + a `&mut self` method, mutated through a copy and through the original, and the copy left + as it was. +- Kotlin, Dart, and Python, whose values are immutable, hand back a `&mut self` method's + result as its new value, `x = x.add(o)`, with Rust's own return, if any, as a pair: + Python's `Value` and Kotlin's class over words are shared by reference, so mutating one + in place would change every alias, unlike a Rust copy. ✅ the method returns the new + value (rejected: a mutable wrapper type, which allocates; keeping such types as handles + in these three, which splits one Rust type across languages). +- Then `ffi/src/collect.rs`'s `changed`, which keeps these types objects today, goes. + +Public API: how every generated API holds these types (breaking). ## Related -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - a value has no handle to check, so fewer calls can fail one -- [Cheaper returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - the other half of Go's and Dart's stamped write +- [Kotlin values without a LongArray](/quest/m1/ffi-speed/kotlin-values.md) - how Kotlin holds the bytes this writes back diff --git a/quest/m1/ffi-export/padding-layout.md b/quest/m1/ffi-export/padding-layout.md new file mode 100644 index 000000000..671aa60f9 --- /dev/null +++ b/quest/m1/ffi-export/padding-layout.md @@ -0,0 +1,45 @@ +# [M] Opaque padding from the rustc the glue builds with + +## Goal + +A [rust2-ffi](/ffi/README.md) export zeroes an opaque value's padding from a table only +when the table describes the layout the glue is compiled with. Today `collect.rs` reads +each target's padding from the exporter's own nightly `rustc`, while the glue builds with +the `rustc` cargo resolved, and Rust promises no `repr(Rust)` layout across compilers. A +compiler that orders a type's fields differently makes `lower_` zero a live field on +every write, silently. That mismatch fails the glue's build instead. + +## Plan + +- The maintainer settled on 2026-11-13: ✅ keep the quest (rejected: drop). How the table + is tied to the glue's layout: ✅ decide later (rejected for now: pin the glue's `rustc`; + padding-free types only). The decision stays under Waiting. +- First step, before the maintainer picks: measure which of moq's opaque values have + padding, and report it on the decision. +- Pinning would make every consumer build the glue with rust2's nightly, which today only + the exporter needs. +- The glue can't name a private field, so it can't check an offset itself; the table's + provenance has to be pinned instead. +- Recommended: the export records the commit hash of the `rustc` cargo resolved, the one + it already runs for a build script's probe, beside each table, and the glue's build + script refuses any other `rustc` with an error naming both. The table itself then has + to come from that `rustc`'s layout, such as its `-Zprint-type-sizes` field offsets under + `RUSTC_BOOTSTRAP`, rather than the exporter's nightly. +- Prove it with a test that builds the glue with a `rustc` whose hash differs from the + recorded one and expects the refusal, and the api fixture's `Gap` padding test still + passing with the matching one. +- The API lock leaves padding out: it is no part of what a binding's caller compiles + against, so a change of it is no break. + +## Waiting + +- Maintainer: how the table is tied to the glue's layout. Options: pin the glue's `rustc` + to the one whose layout the export read (recommended: exact, and fails loudly at + build); compare only each target's size and alignment exactly (rejected: misses a + reorder of the same size); stop zeroing padding (rejected: stale Rust bytes reach + languages that serialize a value); cross as raw bytes only types whose leaf field sizes + add up to their size, so no padding exists under any field order, assert that size in + the glue, and cross padded types as objects (handles): sound on any `rustc` with no + table and no pin, but padded types such as `Instant` pay a handle. Measure moq's padded + opaque values first; the maintainer picks after. An agent cannot pick, since pinning the glue's + `rustc` constrains every consumer's toolchain. diff --git a/quest/m1/ffi-export/ship.md b/quest/m1/ffi-export/ship.md index bbf1ca45a..fab62a833 100644 --- a/quest/m1/ffi-export/ship.md +++ b/quest/m1/ffi-export/ship.md @@ -22,6 +22,7 @@ then see change. A sibling planned later adds itself to `Required` here. - [Refusals](/quest/m1/ffi-refusals/README.md) - the census lists no refusals at the moq pin, and `ffi.toml` is gone - [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - which generated functions throw - [Lossless binding types](/quest/m1/ffi-export/lossless.md) - the types of durations, addresses, paths, and text -- [Copy types cross by value](/quest/m1/ffi-export/copy-values.md) - how a `Copy` type is held in every language +- [Copy types that change cross by value](/quest/m1/ffi-export/copy-values.md) - how moq's stats and `WallMap` are held in every language +- [Opaque padding from the glue's rustc](/quest/m1/ffi-export/padding-layout.md) - that zeroing an opaque value's padding never zeroes a live field - [Not Send](/quest/m1/ffi-export/not-send.md) - the types and futures that export checked by thread - [Export moq for every target](/quest/m1/ffi-export/moq-targets.md) - one binding across the targets moq ships diff --git a/quest/m1/ffi-speed/kotlin-values.md b/quest/m1/ffi-speed/kotlin-values.md new file mode 100644 index 000000000..12ca762a8 --- /dev/null +++ b/quest/m1/ffi-speed/kotlin-values.md @@ -0,0 +1,23 @@ +# [S] Kotlin values without a LongArray + +## Goal + +A [rust2-ffi](/ffi/README.md) Kotlin binding holds a `Copy` value over 8 bytes, such as +moq's 16-byte `Timestamp`, in one object with no array beside it. Today it is a class +over a `LongArray`, two allocations per value, so +[the binding benchmark](/bench/ffi/README.md)'s stamped write costs 150 ns of CPU more +than the plain write in Kotlin (35 wall), against Swift's 1. The stamped write then costs +within its `from_micros` call of the plain write, as Go's and Dart's do. + +## Plan + +- One `Long` field per 8-byte word in the value's class, passed to and from JNI as that + many `jlong` arguments and a returned struct's words written into the class's fields, + so HotSpot's escape analysis can drop a value that does not escape. +- `equals` and `hashCode` keep today's rules, so only the storage changes. +- Re-measure the stamped write, CPU and wall, on HotSpot and on ART in the nightly + emulator job. + +## Related + +- [Copy types that change cross by value](/quest/m1/ffi-export/copy-values.md) - writes a value's bytes back, through the same fields diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index c5c3e6b4d..57b4b17fe 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -23,5 +23,3 @@ Ownership stays automatic: a dropped object is still freed without a call from t ## Related - [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - rewrites the same handle word; profile after it lands, or the baselines move -- [Copy values](/quest/m1/ffi-export/copy-values.md) - removes the per-frame `Timestamp` - object, the other half of Go's and Dart's stamped write