diff --git a/ffi/README.md b/ffi/README.md index b6744c81b..7a81c65e6 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -111,17 +111,20 @@ public path names, or an error taken by value, is held by value as an opaque han | `poll_x`, when `x` exports as a future | left out: the future is its poll | | `bool`, integers but 128-bit, floats, `char` | `bool`, `` types, `float`, `double`, a `uint32_t` code point | | `u128` | `_u128`: its low and high 64 bits | -| `NonZero` of an integer but 128-bit | `T`, in every binding too; a zero passed in aborts, and `Option>` is `T`'s option | +| `NonZero` of an integer but 128-bit | `T`, in every binding but Swift, whose runtime `NonZero` holds no zero; a zero passed in aborts in C and C++, and Kotlin, Python, Go, and Dart check it first and raise their error. Its option and list are its own, `_option_non_zero_` beside `T`'s | | `[u8; N]` | `_array_u8_`: a struct of its `N` bytes, `items`, copied across; an empty one is refused | | `[T; N]` of any other `T`, returned or a constant's value | `Vec` of its `N` items; taken, it is refused | | `&str`, `String`, `&[u8]`, `Vec`, `bytes::Bytes` | `_buf`: an owned buffer returned; passed in, a view, or a buffer Rust returned, which Rust takes back without a copy unless it sits behind a pointer or in an array; `_buf_clone` shares one the caller keeps, which a `Vec`, `String`, or `PathBuf` parameter then copies, since only a sole owner's allocation becomes a `Vec` | -| `&Path`, `PathBuf` | `_buf` of the path's bytes as the OS holds them on Unix, which need not be UTF-8, else UTF-8 | -| `Duration` | `_duration` | -| `SocketAddr`, `SocketAddrV4`, `SocketAddrV6` | `_socket_addr`: its IP's bytes, port, and whether it is IPv6; one of the other family passed to `V4` or `V6` aborts | -| `IpAddr`, `Ipv4Addr`, `Ipv6Addr` | `_ip_addr`: its bytes and whether it is IPv6; one of the other family passed to `Ipv4Addr` or `Ipv6Addr` aborts | +| `&Path`, `PathBuf` | `_buf` of the path's bytes as the OS holds them: on Unix any, on Windows WTF-8, which holds a name that is not UTF-16, else UTF-8 | +| `Duration` | `_duration`: its seconds and nanoseconds; a billion nanoseconds or more passed in aborts | +| `SocketAddrV4`, `SocketAddrV6` | `_socket_addr_v4`, `_socket_addr_v6`: its IP, port, and for IPv6 its flow label and scope, so each holds its family alone | +| `SocketAddr` | `_socket_addr`: `v6`, and a union `addr` of the two | +| `Ipv4Addr`, `Ipv6Addr` | `_ipv4_addr`, `_ipv6_addr`: its bytes in network order | +| `IpAddr` | `_ip_addr`: `v6`, and a union `ip` of the two | | `url::Url` | an object `_Url`, made by `_Url_parse`, which fails with the parse error's message, and printed by `_to_string` | | `Arc` of another crate's `Send` and `Sync` type that no other rule maps, such as `Arc`, or `Arc` and `&'static dyn Trait` of another crate's trait without generic arguments, whose auto traits are `Send` and `Sync` but those the trait implies, borrowing nothing C lends, as rustls's `Arc` | an opaque object named by `T`'s crate and name, such as `_rustls_ServerConfig`, with `_clone` and `_free` and none of `T`'s API: it only passes back to Rust | | another crate's `Send` and `Sync` type taken or returned by value or lent, with no type parameters, that no other rule maps, that is no iterator or future, and that is not reached by its public path, such as one only a `#[doc(hidden)]` module names, or a standard library type no other rule maps, such as `io::Error`, named by its full path, `_std_io_Error` | an opaque object named as the `Arc` form is, with `_free` and none of `T`'s API, holding the value: Rust takes it back by value, or borrows it; a type also named behind an `Arc` is that form, and refused by value | +| a type implementing `PartialEq` and `Hash` | beside `_eq`, `uint64_t _hash`, its `Hash` through std's hasher with its fixed keys, so equal values hash alike in every binding | | `std::time::Instant` | an object `_Instant`, which only Rust makes, copied into each call that takes it and compared by `_eq`, with none of its methods | | `tracing::Level` | `_Level`: a C enum of its five levels, `TRACE` to `ERROR` | | `tracing::level_filters::LevelFilter` | `Option`, whose `None` is `OFF` | @@ -426,7 +429,8 @@ packages place it in. member or as anything in the namespace, which it would hide. - **Objects** are classes owning their handle through a `std::unique_ptr` whose deleter calls `_free`, so they move and never copy, and a `Clone` type's `clone()` crosses into - Rust for a value of its own. A type whose copies may share one value, by the rule below, + Rust for a value of its own. A type implementing `Hash` has `hash()`, Rust's, by which + `std::hash` hashes it. A type whose copies may share one value, by the rule below, owns it through a `std::shared_ptr` instead: a copy shares the handle without crossing into Rust, and the last copy frees it, on whichever thread drops it. `&self` methods are `const`, and `self` methods are `&&`, so a call that moves the value out reads @@ -547,7 +551,8 @@ the rest cross encoded. `close()` frees the handle, and throws while a call holds it. One never closed is freed once the GC collects it, on a daemon thread a phantom reference queue feeds. A `self` method, or passing the object by value, moves its value out and closes it once the call - returns. `Clone` is `clone()`, and `PartialEq` is `equals`. + returns. `Clone` is `clone()`, `PartialEq` is `equals`, and `Hash` is `hashCode`, Rust's + hash folded as `Long.hashCode` folds it, or 0 without one. A field is a `val`, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in and closes its handle, which an assignment would hide. @@ -561,8 +566,8 @@ the rest cross encoded. 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 a class of a `Long` for each 64-bit word of its bytes, which allocates - nothing where the JIT sees it does not escape, whose `==` is Rust's, and whose `hashCode` is 0, since none Kotlin - computes agrees with it. Either is shared by reference, so it never changes: a `&mut + nothing where the JIT sees it does not escape, whose `==` is Rust's, and whose `hashCode` is Rust's `Hash`, or 0 + without one, since none Kotlin computes agrees with its `==`. Either is shared by reference, so it never changes: a `&mut self` method, a field's setter included, returns the value as the call left it, `x = x.add(o)`, and beside Rust's own return, a `Pair`, or each element of its tuple, `val (y, n) = x.take()`. @@ -578,21 +583,35 @@ the rest cross encoded. 64 MiB are lent at once, freed or not; past that, longer ones are copied too, and that garbage in Kotlin's heap runs the GC, which frees them. A `Bytes` equals and hashes by its contents, wherever each keeps them, so a record holding bytes does too; Rust's lent ones are copied across in one call to compare. - Strings are `String`, passed in as their UTF-16, which Rust encodes once into UTF-8 as - `encodeToByteArray` does, a lone surrogate as `?`; `usize` `ULong` and `isize` `Long`, which abort past a 32-bit - target's range, `Option` `T?`, a tuple `Pair` or + Strings are `String`, passed in as their UTF-16, which Rust encodes once into UTF-8 before + the call reads any argument, so one holding a lone surrogate, which no Rust text holds, + throws `IllegalArgumentException` before anything moves; a `char` is an `Int`, which + throws `IllegalArgumentException` passed in unless it is a Unicode scalar value; `usize` + `ULong` and `isize` `Long`, which throw `IllegalArgumentException` passed in past the + library's target's range, as a 32-bit one's is, and a `NonZero` `T`'s type, which throws + `IllegalArgumentException` passed in zero; `Option` is `T?`, a tuple `Pair` or `Triple`, or else a generated `Tuple`, and also `Tuple2` or `Tuple3` when it holds a numeric array or a float anywhere, records included, since `Pair` and `Triple` cannot - compare them as Rust does (so a `BTreeMap` is a `List>`), `u128` `java.math.BigInteger`, which throws `IllegalArgumentException` passed in out of its range, `Duration` `kotlin.time.Duration`, and an `Err` a thrown `Exception` + compare them as Rust does (so a `BTreeMap` is a `List>`), `u128` `java.math.BigInteger`, which throws `IllegalArgumentException` passed in out of its range, and an `Err` a thrown `Exception` holding it, whose message is its `Display`. A `[u8; N]` is a generated `ArrayU8x`, which copies the `ByteArray` it is made of and throws `IllegalArgumentException` unless it holds exactly `N` bytes. -- A **path** is a `String`: one Rust returns that is not UTF-8 throws. A `SocketAddr`, - `SocketAddrV4`, or `SocketAddrV6` is a `java.net.InetSocketAddress`, which must be - resolved and, for `V4` or `V6`, of that family; one Rust returns with an IPv6 flow - label, which Java does not keep, throws. An `IpAddr`, `Ipv4Addr`, or `Ipv6Addr` is a - `java.net.InetAddress`, of that family for `Ipv4Addr` or `Ipv6Addr`; one passed in with - an IPv6 scope, which Rust does not keep, throws. A `Vec`, `&[T]`, or `Box<[T]>` of a +- **Std values** no Kotlin or Java type holds exactly are the runtime's own, each holding + every Rust value and made only of valid ones, with conversions to and from the + idiomatic type that throw `IllegalArgumentException` where the other cannot hold a value + exactly. A `Duration` is a `Duration` of `secs: ULong` and `nanos: UInt`, made from and + converted to a `kotlin.time.Duration` (`Duration(5.seconds)`, `toKotlinDuration()`), + which is never negative or infinite in Rust and drops what is below a millisecond past + about 146 years. A path is a `PathBuf` of the OS's bytes, any on Unix and WTF-8 on + Windows, made from them or from a `String`, and converted to one or to a + `java.nio.file.Path` by `toText()` and `toPath()`, which throw where it is not UTF-8; + its `toString()` shows what is not as U+FFFD, as Rust's `display` does. An `IpAddr` + is a sealed `IpAddr` of an `Ipv4Addr`, a value class of its bits, or an `Ipv6Addr`, + each kept of its family, so an IPv4-mapped address stays IPv6, and a `SocketAddr` a + sealed `SocketAddr` of a `SocketAddrV4`, a value class of its address and port, or a + `SocketAddrV6`, which keeps its flow label and scope; each prints as Rust does, and + converts to and from `java.net`'s, which has no unresolved address in Rust, no scope in + an `IpAddr`, and no flow label in Java. A `Vec`, `&[T]`, or `Box<[T]>` of a fixed-width integer other than `u8` (bytes) or `u128`, or of a float, is Kotlin's primitive array of it: `ByteArray`, `ShortArray`, `IntArray`, or `LongArray` for `i8` through `i64`, `UShortArray`, `UIntArray`, or `ULongArray` for `u16` through `u64`, which a caller uses under @@ -674,7 +693,7 @@ caller imports it with no ignores. - **Objects** are classes owning their handle, freed once their last reference goes, which the caller's reference keeps through a call, so there is no `close()`. A `self` method, or passing the object by value, moves its value out, after which a call raises - `ValueError`. `Clone` is `clone()`, and `PartialEq` is `==`, unhashable. A field is a + `ValueError`. `Clone` is `clone()`, and `PartialEq` is `==`, unhashable unless the type implements `Hash`, which `hash()` hashes it by. A field is a property, `first_get`, and its setter a method, `first_set(value)`, named as the C ones, since setting an object moves it in and closes it, which an assignment would hide. - **Checks**: a borrow or move Rust's rules refuse raises, naming the function and the rule, @@ -695,8 +714,10 @@ caller imports it with no ignores. returns, or a record holds, are a read-only `memoryview` lending Rust's buffer until it and its views are released. Those Rust lends a closure or trait method are copied into `bytes`. Strings are `str`, `u128` an `int`, `Option` `T | None`, a tuple a `tuple`, `Duration` - `datetime.timedelta`, truncated to the microsecond, with `timedelta.max` for any longer - and for `Duration::MAX`, a `Range` a `range`, and an `Err` a raised `Exception` + the runtime's `Duration`, a `NamedTuple` of `secs` and `nanos` as Rust holds them, which + raises `OverflowError` or `ValueError` passed in out of their range; its `from_timedelta` + and `to_timedelta` raise rather than lose anything, as a `timedelta` holds only whole + microseconds up to 999,999,999 days. A zero passed for a `NonZero` raises `ValueError`. A `Range` is a `range`, and an `Err` a raised `Exception` holding it in `error`, whose `str` is its `Display`. A `[u8; N]` is a generated `ArrayU8x`, a `bytes` that raises `ValueError` unless it holds exactly `N`; a parameter takes any `bytes` of `N`. @@ -760,7 +781,8 @@ beside `swift/_atomics.h`, the atomics its runtime needs that Swift's st library has only from macOS 15: a build puts `swift/` on its import path and links the static library. It imports swift-collections' `BasicContainers` for `UniqueArray`, which `just ffi swift-collections` builds at the version the tests pin, 1.7.5. It needs Swift 6.2 -and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mode. +and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mode. Every +value of a Rust type crosses as one Swift type that holds it exactly, and back unchanged. - **Names** are as Kotlin's, at the module's top level: types in PascalCase without the prefix, members in camelCase without their type's name (`TrackProducer.appendGroup`). A @@ -780,7 +802,8 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod is an overload with and without it, labelled by its name, as a defaulted argument is: `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` unless it throws, as below; `Display` is `description` and `Debug` + `Equatable` unless it throws, as below, and `Hashable` with `Hash`, whose `hash(into:)` + combines Rust's hash; `Display` is `description` and `Debug` `debugDescription`, which Swift 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 @@ -794,12 +817,14 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod field read stays a property whose getter throws, except one of a noncopyable value, whose getter Swift cannot let throw, which is a method, `firstGet()`. Of a type whose checks can fail, or a record or enum holding one, `description` and `debugDescription` - are getters that throw, and `clone()` and `==` throw, so it conforms to none of - `CustomStringConvertible`, `CustomDebugStringConvertible`, and `Equatable`, whose + are getters that throw, and `clone()`, `==`, and `hash(into:)` throw, so it conforms to + none of `CustomStringConvertible`, `CustomDebugStringConvertible`, `Equatable`, and + `Hashable`, whose requirements cannot throw. A closed class anywhere in a call's arguments throws too. A copy a closure returns, which has no caller to throw to, traps instead. - **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`, + values, all `Sendable`. `PartialEq` is `==`, through Rust's, and `Hash` `hash(into:)`, + making it `Hashable`; `Display` is `description`, `Debug` `debugDescription`, each throwing as an object's does when it holds a checked one, and `Default` a static `default()`. An **opaque value** is a struct holding its C struct, with no public initializer, so only Rust makes one; a `&mut self` method is @@ -813,17 +838,26 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod writes into a buffer Rust shares. Strings are `String`, copied, `char` `Unicode.Scalar`, `usize` `UInt`, `Option` `T?`, a tuple a Swift tuple, though a tuple of one is its element, `u128` a `U128` of its - `high` and `low` halves until the deployment target allows `UInt128`, and `Duration` - `Swift.Duration`. An `Err` is thrown: an exported type as itself, which conforms to + `high` and `low` halves until the deployment target allows `UInt128`, and `Duration` the + runtime's `RustDuration` of its `secs` and `nanos`, whose initializer throws a + `CheckError` for a second's nanoseconds or more. `duration` is it as a `Swift.Duration`, + which holds every one, and `RustDuration(_:)` makes one from a `Swift.Duration`, + throwing for one negative, past Rust's longest, or with a part below a nanosecond. A + `NonZero` is the runtime's `NonZero` of `T`'s integer, its `value`, made with + `try NonZero(3)`, whose initializer throws a `CheckError` for a zero, so no call + taking one checks it and a type holding one keeps its protocols. An `Err` is thrown: an exported type as itself, which conforms to `Swift.Error`, and any other in a generated `Error` holding it, whose `init(error:)` makes one for a closure to throw back. A `[u8; N]` is a generated `ArrayU8x`, a collection holding its bytes inline in C's struct, made by a failable initializer from exactly `N` bytes, until the deployment target allows `InlineArray`. -- A **path** is a `String`: one Rust returns that is not UTF-8 traps. A `SocketAddr`, - `SocketAddrV4`, or `SocketAddrV6` is the runtime's `SocketAddr`, its IP's 4 or 16 bytes - and its port, flow label, and scope, whose family Rust checks. An `IpAddr`, `Ipv4Addr`, - or `Ipv6Addr` is the runtime's `IpAddr`, its 4 or 16 bytes. A `Vec`, `&[T]`, or +- A **path** is the runtime's `PathBuf` of its `bytes`, a `Bytes` sharing Rust's buffer as + bytes do, so one that is not UTF-8 keeps them; `PathBuf("a/b")`, or a string literal, + makes one, and `string` is its text, or nil if it is not UTF-8. Each address type is the + runtime's of its name, as std's: `Ipv4Addr` and `Ipv6Addr` of their `bits`, as + `to_bits` gives them, or `octets`, `IpAddr` an enum of `.v4` and `.v6`, `SocketAddrV4` + and `SocketAddrV6` of their IP, port, and IPv6's flow label and scope, and `SocketAddr` + an enum of either. A `Vec`, `&[T]`, or `Box<[T]>` is an array, or a `UniqueArray` of objects; one of a fixed-width integer other than `u8` (bytes) or `u128`, or of a float, is copied across whole, with no call per item. - **Traits** are `Sendable` protocols, which Rust releases once it drops an implementation. @@ -948,15 +982,32 @@ reads a go.mod. returns freed memory, such as [mimalloc](https://crates.io/crates/mimalloc), declared in the exported crate, holds it near what is lent. `Copy()` copies it out. Text Rust returns is copied once into a `string`, and Rust's freed at the call. A path is a `string` - of the OS's bytes, which need not be UTF-8. `Option` is `*T`, or for an object or - `u128` its pointer, nil for `None`. A tuple is `rt.Tuple2[A, B]` and so on, but a tuple of one is its - element, and one returned whole is the function's results. A `u128` is a `*big.Int`, which - panics passed in out of its range, and a `[u8; N]` a `[N]byte`. `Duration` is - `time.Duration`: a negative one passed in, or one Rust returns past its 292 years, - panics. A socket address is a `netip.AddrPort`, whose zone names an IPv6 scope by its - number or its interface's name; one Rust returns with an IPv6 flow label, which - `netip` does not keep, panics. An IP address is a `netip.Addr`; one passed in that is - invalid, has a zone, or is of the wrong family panics. A `Vec`, `&[T]`, or `Box<[T]>` is a `[]T`; one of a + of the OS's bytes, which need not be UTF-8. `Option` is `*T`, or for an object its + pointer, nil for `None`. A tuple is `rt.Tuple2[A, B]` and so on, but a tuple of one is its + element, and one returned whole is the function's results. A `[u8; N]` is a `[N]byte`. +- **Lossless types**: each holds every value of its Rust type, so one passed in and + returned is the same value, and none panics. Where Go's own type does not, the + runtime's crosses, with conversions to and from Go's that return an error matching + `rt.ErrInvalid` for a value the other has no room for. A `u128` is an `rt.U128{Hi, Lo}`, + converted by `Big()` and `rt.U128Of(*big.Int)`. A `Duration` is an `rt.Duration`, whose + zero value is zero and whose `Secs()` and `Nanos()` are Rust's: `rt.NewDuration(secs, + nanos)` carries nanoseconds past a second into the seconds, as Rust's `Duration::new` + does, `rt.DurationOf(time.Duration)` fails on a negative one, and `Std()` fails past + `time.Duration`'s 292 years. An address is the runtime's mirror of `std::net`'s: + `rt.Ipv4Addr` and `rt.Ipv6Addr` their octets, `rt.SocketAddrV4` and `rt.SocketAddrV6` + structs of their fields, flow label and scope too, and `rt.IpAddr` and `rt.SocketAddr` + one of either family, made by `rt.IpAddrFromV4` and so on and read by `V4()` and `V6()`, + whose zero value is IPv4's `0.0.0.0`, port 0. Each converts to and from `net/netip`'s, + `Netip()` and `rt.Of`, which fail on the wrong family, a zone an `Ipv6Addr` or + `IpAddr` has no room for, or a flow label `netip` has none for; a scope is a zone of its + number, and a zone converts by its number or its interface's name. Every one compares + with `==`. A `string` that is not UTF-8, a `rune` that is not a Unicode scalar value, + a path that is not WTF-8 on Windows, or a zero for a `NonZero`, fails the call taking it, + anywhere in its arguments, before it is made, with an error matching `rt.ErrInvalid`, so + every such call returns an `error`, on every target for a path too, `Equal` of a value + holding text too, and `String()` and `GoString()` the error's text; what a closure or + trait Go implements returns Rust has no caller to return one to, so it aborts. +- **Lists**: a `Vec`, `&[T]`, or `Box<[T]>` is a `[]T`; one of a fixed-width integer other than `u8` (bytes) or `u128`, or of a float, is lent in place, pinned for the call, as bytes are, and one Rust returns is copied out whole. - **Errors**: a `Result` returns an `error` last. An exported error type is returned as @@ -1035,17 +1086,26 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass - **Records** are final classes with a named, required parameter per field, and positional fields are `$0`, `$1`, and so on. **Plain enums** are Dart enums, and an enum 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_()`. An + over it is exhaustive. `Display` is `toString()`, `Debug` `debug()`, `PartialEq` `==`, and + `Hash` `hashCode`, all through Rust's; without `Hash`, `hashCode` throws an + `UnsupportedError`, since no hash Dart computes agrees with Rust's `==`. `Default` is 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. It never changes: a `&mut self` method, a field's setter included, returns the value as the call left it, `x = x.add(o)`, and beside Rust's own return, a record, with each element of a tuple Rust returns, `final (y, n) = x.take()`. 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. -- **Bytes** a call takes are a `Uint8List`, and text a `String`. Rust keeps Dart's own +- **Integers** are `int`, but for `u64` and `usize`, which are the runtime's `U64`: an + extension type on the `int` of its 64 bits, costing nothing over it, which compares + unsigned and converts, checked, to and from an `int` (`U64(5)`, `toInt()`) or a + `BigInt`; `U64.fromBits` takes any `int`'s bits. Being an extension type, it prints as + its `int` does, signed: `toRadixString` and `toBigInt` print it unsigned. A narrower + integer passed in throws a `RangeError` when out of its range, as do a `usize` or + `isize` past 32 bits on a 32-bit target, and a zero passed for a `NonZero` throws an + `ArgumentError`. A `char` is a `String` of one Unicode scalar, and any other throws an + `ArgumentError`. +- **Bytes** a call takes are a `Uint8List`, and text a `String`, which throws an + `ArgumentError` passed in holding a lone surrogate, since UTF-8 has no form of one and + Dart's encoder would replace it. Rust keeps Dart's own with one copy from 1 KiB, into a buffer Rust takes, and with two below, since allocating Rust's buffer costs more there than the second copy; a lent `Uint8List` Rust returned passes back as a second owner of Rust's buffer, with no copy. Bytes Rust returns are a `Uint8List` owned by Dart's memory @@ -1053,22 +1113,27 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass unmodifiable view of Rust's buffer, freed once neither it nor any view of it is reachable, so no view outlives its memory. A lent view costs a finalizer and charges the garbage collector its length, which costs more than a copy up to about 64 KiB. Text Rust - returns is copied into a `String`. A path is a `String`, and one Rust returns that is - not UTF-8 aborts, since a throw would leave the rest of a list or record being lifted - unfreed. `Option` is `T?`, a tuple a Dart record, and a `Vec` or `&[T]` a + returns is copied into a `String`. A path is the runtime's `PathBuf` of its bytes, + crossing as bytes do, so one that is not UTF-8 crosses whole: `PathBuf(String)` makes + one, and `toFilePath()` is its `String`, throwing a `FormatException` where it is not + UTF-8. `Option` is `T?`, a tuple a Dart record, and a `Vec` or `&[T]` a `List`, except one of a fixed-width integer other than `u8` (bytes) or `u128` or of a float, or a `Box<[T]>` of one, which is the `dart:typed_data` list of its width, such as a `Uint32List` or `Float64List`: - copied whole into the call's memory passed in, and out of Rust's returned. A - `Uint64List` holds a `u64`'s bits, so one past `i64`'s range reads negative, as a `u64` - alone does. A `u128` is a `BigInt`, which throws an `ArgumentError` passed in out of - its range. A `[u8; N]` is a generated `ArrayU8x`, which copies the list it is made - of and throws an `ArgumentError` unless it holds exactly `N` bytes. `Duration` is `Duration`, truncated to the microsecond: a negative one passed - in throws an `ArgumentError`, and Rust's longest is Dart's. A socket address is a - `SocketAddr` of an `InternetAddress`, a port, and IPv6's flow label and scope; one passed - in of the wrong family throws an `ArgumentError`, and a port, label, or scope out of its - range a `RangeError`. An IP address is an `InternetAddress`. A Unix socket's - `InternetAddress`, or one of the wrong family, passed in for either throws an - `ArgumentError`. + copied whole into the call's memory passed in, and out of Rust's returned; a `u64`'s is + a `List`, a `Uint64List` read as `U64`s. A `u128` is a `BigInt`, which throws an + `ArgumentError` passed in out of its range. A `[u8; N]` is a generated `ArrayU8x`, + which copies the list it is made of and throws an `ArgumentError` unless it holds + exactly `N` bytes. `Duration` is the runtime's `RustDuration`, its `U64` seconds and + nanoseconds below one, which throws a `RangeError` made of a second's nanoseconds or + more: `RustDuration.from` takes a `Duration`, throwing an `ArgumentError` for a negative + one, and `toDuration()` gives one, throwing a `RangeError` for part of a microsecond or + one past `Duration`'s longest. Addresses are the runtime's, as Rust's: `Ipv4Addr` and + `Ipv6Addr` of their octets, each an `IpAddr`, a sealed class; and `SocketAddrV4` and + `SocketAddrV6`, with its flow label and scope, each a `SocketAddr`, whose port, label, + and scope throw a `RangeError` out of range. So the type holds only its family, and each + crosses whole. `IpAddr.from`, `Ipv4Addr.from`, and `Ipv6Addr.from` take a `dart:io` + `InternetAddress`, throwing an `ArgumentError` for a Unix one, one of the other family, + or an IPv6 one with a scope, which Rust's has none of; `toInternetAddress()` gives one. - **Errors**: a `Result`'s error is thrown. An exported error type is thrown as itself, implementing `Exception`, and any other in a final class `Exception` holding it as `error`, such as `StringException`. @@ -1083,8 +1148,8 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass - 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. `SeekableReader.file` reads a `RandomAccessFile` through its synchronous methods, as - Rust's calls are. A seek from `SeekFrom.start` takes a `u64`'s bits, as every `u64` is, - so one past `i64`'s range is negative, which a file refuses. Rust's buffer is never + Rust's calls are. A seek from `SeekFrom.start` takes a `u64`'s bits, as `U64.bits` holds + them, so one past `i64`'s range is negative, which a file refuses. Rust's buffer is never `readInto`'s: it fills a list of Dart's own, which Rust copies. An `Exception` either throws fails the call into Rust as an `io::Error` of its text, and anything else it throws aborts. diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 9b80554a8..7e5798785 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -887,6 +887,80 @@ fn main() { ip_hex(other) ); + // Lossless round trips: each type's edge values, passed through Rust and back. + let durations: Vec = [ + std::time::Duration::ZERO, + std::time::Duration::from_nanos(1), + std::time::Duration::from_nanos(i64::MAX as u64 + 1), + std::time::Duration::MAX, + ] + .into_iter() + .map(|d| { + let d = api::same_duration(d); + format!("{}.{:09}", d.as_secs(), d.subsec_nanos()) + }) + .collect(); + println!("lossless duration {}", durations.join(" ")); + println!( + "lossless u64 {} {} {}", + api::same_u64(0), + api::same_u64(1 << 63), + api::same_u64(u64::MAX) + ); + println!("lossless usize {} {}", api::same_usize(0), api::same_usize(usize::MAX)); + println!( + "lossless isize {} {}", + api::same_isize(isize::MIN), + api::same_isize(isize::MAX) + ); + println!( + "lossless u128 {:032x} {:032x} {:032x}", + api::same_u128(0), + api::same_u128(1 << 64), + api::same_u128(u128::MAX) + ); + let chars: Vec = ['\0', '\u{d7ff}', '\u{e000}', char::MAX] + .into_iter() + .map(|c| format!("{:x}", u32::from(api::same_char(c)))) + .collect(); + println!("lossless char {}", chars.join(" ")); + let paths: Vec = [std::path::PathBuf::from("a/é"), unspelled()] + .into_iter() + .map(|p| hex(api::same_path(p).as_os_str().as_encoded_bytes())) + .collect(); + println!("lossless path {}", paths.join(" ")); + let v4 = api::same_socket_addr_v4(std::net::SocketAddrV4::new(std::net::Ipv4Addr::BROADCAST, u16::MAX)); + let v6 = api::same_socket_addr_v6(std::net::SocketAddrV6::new( + std::net::Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 2), + 3, + u32::MAX, + u32::MAX, + )); + let addrs: Vec = [v4.into(), v6.into()] + .into_iter() + .map(|a| socket_hex(api::same_socket_addr(a))) + .collect(); + println!( + "lossless socket {} {} {}", + socket_hex(v4.into()), + socket_hex(v6.into()), + addrs.join(" ") + ); + // A key hashes by Rust's `Hash`: equal keys alike, and these two apart. + let (a, b) = (api::Key::new("a"), api::Key::new("b")); + println!( + "key {} {}", + a.hash_of() == api::Key::new("a").hash_of(), + a.hash_of() == b.hash_of() + ); + println!( + "lossless ip {} {}", + ip_hex(api::same_ip_addr(std::net::Ipv4Addr::BROADCAST.into())), + ip_hex(api::same_ip_addr( + std::net::Ipv6Addr::new(0xffff, 0, 0, 0, 0, 0, 0, 1).into() + )) + ); + // Standard and common types: serde_json's map iterator, std's instant, and tracing's // level and its filter. let sections: Vec = api::sections( @@ -991,11 +1065,40 @@ fn main() { ); } +/// A path no Rust string spells: on Unix, bytes that are not UTF-8; on Windows, a lone +/// surrogate, which is WTF-8's `ed a0 80`. +#[cfg(unix)] +fn unspelled() -> std::path::PathBuf { + api::stray() +} + +/// A path no Rust string spells: on Unix, bytes that are not UTF-8; on Windows, a lone +/// surrogate, which is WTF-8's `ed a0 80`. +#[cfg(windows)] +fn unspelled() -> std::path::PathBuf { + ::from_wide(&[0xd800]).into() +} + /// Lowercase hex of `bytes`, two digits each. fn hex(bytes: &[u8]) -> String { bytes.iter().map(|b| format!("{b:02x}")).collect() } +/// A socket address as its IP's bytes in hex, its port, flow label, and scope, each +/// IPv6's alone, apart by colons. +fn socket_hex(addr: std::net::SocketAddr) -> String { + match addr { + std::net::SocketAddr::V4(v4) => format!("{}:{}", hex(&v4.ip().octets()), v4.port()), + std::net::SocketAddr::V6(v6) => format!( + "{}:{}:{}:{}", + hex(&v6.ip().octets()), + v6.port(), + v6.flowinfo(), + v6.scope_id() + ), + } +} + /// An address's bytes in hex, so the length shows its family. fn ip_hex(ip: std::net::IpAddr) -> String { match ip { diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 74228c8d3..eb1215991 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -4026,3 +4026,88 @@ impl Handle { Handle { count } } } + +// Lossless round trips: each returns what it is given, so a binding passes a type's edge +// values through Rust and back, and prints what it holds. + +/// `v`, as given: the longest `Duration` included. +pub fn same_duration(v: Duration) -> Duration { + v +} + +/// `v`, as given: past `i64::MAX` included. +pub fn same_u64(v: u64) -> u64 { + v +} + +/// `v`, as given: past a 32-bit target's included. +pub fn same_usize(v: usize) -> usize { + v +} + +/// `v`, as given. +pub fn same_isize(v: isize) -> isize { + v +} + +/// `v`, as given. +pub fn same_u128(v: u128) -> u128 { + v +} + +/// `v`, as given. +pub fn same_char(v: char) -> char { + v +} + +/// `v`, as given. +pub fn same_string(v: String) -> String { + v +} + +/// `v`, as given: one whose bytes are not UTF-8 included, as a Unix path may be. +pub fn same_path(v: std::path::PathBuf) -> std::path::PathBuf { + v +} + +/// `v`, as given: a flow label and scope included. +pub fn same_socket_addr(v: std::net::SocketAddr) -> std::net::SocketAddr { + v +} + +/// `v`, as given. +pub fn same_socket_addr_v4(v: std::net::SocketAddrV4) -> std::net::SocketAddrV4 { + v +} + +/// `v`, as given: a flow label and scope included. +pub fn same_socket_addr_v6(v: std::net::SocketAddrV6) -> std::net::SocketAddrV6 { + v +} + +/// `v`, as given. +pub fn same_ip_addr(v: std::net::IpAddr) -> std::net::IpAddr { + v +} + +/// A key: an object whose `Hash` agrees with its `PartialEq`, so a binding hashes it by +/// Rust's, as a map keyed by it needs. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct Key { + name: String, +} + +impl Key { + pub fn new(name: &str) -> Key { + Key { name: name.to_string() } + } + + /// Its hash by std's hasher, which a binding's own of it must equal, as both hash + /// through Rust's `Hash` alike. + pub fn hash_of(&self) -> u64 { + std::hash::BuildHasher::hash_one( + &std::hash::BuildHasherDefault::::default(), + self, + ) + } +} diff --git a/ffi/moq/bench/export.dart b/ffi/moq/bench/export.dart index e0a16f1ff..7929f99fb 100644 --- a/ffi/moq/bench/export.dart +++ b/ffi/moq/bench/export.dart @@ -129,7 +129,7 @@ Future> round() async { final demand = demanded.demand(); start = begin(); for (var i = 0; i < n; i++) { - sink += demand.sequence(); + sink += demand.sequence().bits; } out['getter'] = end(start, 'getter'); demand.close(); @@ -154,14 +154,14 @@ Future> round() async { for (var g = 0; g < groups; g++) { final group = track.appendGroup(); for (var f = 0; f < frames; f++) { - group.writeFrame(moq.Timestamp.fromMicros(micros++), frame); + group.writeFrame(moq.Timestamp.fromMicros(moq.U64(micros++)), frame); } group.finish(); group.close(); } out['stamped'] = end(start, 'stamped'); - final at = moq.Timestamp.fromMicros(micros); + final at = moq.Timestamp.fromMicros(moq.U64(micros)); start = begin(); for (var g = 0; g < groups; g++) { final group = track.appendGroup(); diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 732914637..e579448e2 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -556,9 +556,9 @@ int main(void) { moq_vec_string_free(alpns); moq_Versions_free(versions); /* A socket address, crossing in a listener's bind, set on its config and read back. */ - moq_tokio_listen_Bind bind = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.ip = {[15] = 1}, .port = 4443, .v6 = true}}}; + moq_tokio_listen_Bind bind = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.v6 = true, .addr.v6 = {.ip.octets = {[15] = 1}, .port = 4443}}}}; CHECK(is(moq_tokio_listen_Bind_to_string(&bind), "[::1]:4443")); - moq_tokio_listen_Bind v4 = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.ip = {127, 0, 0, 1}, .port = 0}}}; + moq_tokio_listen_Bind v4 = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.addr.v4 = {.ip.octets = {127, 0, 0, 1}, .port = 0}}}}; CHECK(is(moq_tokio_listen_Bind_to_string(&v4), "127.0.0.1:0")); CHECK(!moq_tokio_listen_Bind_eq(&bind, &v4)); moq_tokio_listen_Config *listen = moq_tokio_listen_Config_default(); @@ -567,10 +567,10 @@ int main(void) { CHECK(bound.some && moq_tokio_listen_Bind_eq(&bound.value, &bind)); moq_tokio_listen_Bind_free(&bound.value); /* The preferred address of each family, which takes only that family. */ - moq_socket_addr preferred = {.ip = {10, 0, 0, 1}, .port = 4443}; - moq_tokio_listen_Config_preferred_v4_set(listen, (moq_option_socket_addr){.some = true, .value = preferred}, NULL); - moq_option_socket_addr preferred_v4 = moq_tokio_listen_Config_preferred_v4_get(listen, NULL); - CHECK(preferred_v4.some && !preferred_v4.value.v6 && preferred_v4.value.ip[0] == 10 && preferred_v4.value.port == 4443); + moq_socket_addr_v4 preferred = {.ip.octets = {10, 0, 0, 1}, .port = 4443}; + moq_tokio_listen_Config_preferred_v4_set(listen, (moq_option_socket_addr_v4){.some = true, .value = preferred}, NULL); + moq_option_socket_addr_v4 preferred_v4 = moq_tokio_listen_Config_preferred_v4_get(listen, NULL); + CHECK(preferred_v4.some && preferred_v4.value.ip.octets[0] == 10 && preferred_v4.value.port == 4443); CHECK(!moq_tokio_listen_Config_preferred_v6_get(listen, NULL).some); moq_tokio_listen_Config_free(listen); /* A constant slice, as an array of clones. */ @@ -598,7 +598,7 @@ int main(void) { moq_tokio_server_Config *server_config = moq_tokio_server_Config_default(); moq_tokio_listen_Config *listen_config = moq_tokio_server_Config_listen_get(server_config, NULL); - moq_tokio_listen_Bind any = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.port = 0, .v6 = true}}}; + moq_tokio_listen_Bind any = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.v6 = true}}}; moq_tokio_listen_Config_bind_set(listen_config, (moq_option_tokio_listen_Bind){.some = true, .value = any}, NULL); moq_tokio_tls_Listen *tls_listen = moq_tokio_tls_Listen_default(); moq_buf names[] = {str("localhost")}; @@ -612,14 +612,14 @@ int main(void) { moq_tokio_server_Config_free(server_config); moq_socket_addr local; CHECK(moq_tokio_Server_local_addr(server, &local, &tokio_err, NULL)); - CHECK(local.v6 && local.port != 0); + CHECK(local.v6 && local.addr.v6.port != 0); moq_origin_Consumer *published = moq_origin_Producer_consume(served, NULL); serving = moq_tokio_Server_serve_publish(server, published, NULL); moq_tokio_Server_free(server); moq_origin_Consumer_free(published); char address[32]; - snprintf(address, sizeof address, "moqt://localhost:%u", (unsigned)local.port); + snprintf(address, sizeof address, "moqt://localhost:%u", (unsigned)local.addr.v6.port); moq_tokio_connect_Config *dialing = moq_tokio_connect_Config_default(); CHECK(moq_Url_parse(str(address), &url, &parse_err)); moq_tokio_connect_Config_url_set(dialing, url, NULL); diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index 7d699d24a..bdc750d81 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -356,18 +356,20 @@ int main() { CHECK(is(connect.url_get()->to_string(), "https://relay.example:4443/demo")); // A listener's bind, a socket address. + moq::socket_addr_v6 addr_v6{}; + addr_v6.ip.octets[15] = 1; + addr_v6.port = 4443; moq::socket_addr addr{}; - addr.ip[15] = 1; - addr.port = 4443; addr.v6 = true; + addr.addr.v6 = addr_v6; moq::tokio_listen_Config listen = moq::tokio_listen_Config::default_(); listen.bind_set(moq::tokio_listen_Bind(moq::tokio_listen_Bind_Addr{addr})); auto bind = listen.bind_get(); CHECK(bind && is(moq::tokio_listen_Bind_to_string(*bind), "[::1]:4443")); // The preferred address of each family, which takes only that family. - listen.preferred_v6_set(addr); + listen.preferred_v6_set(addr_v6); auto preferred = listen.preferred_v6_get(); - CHECK(preferred && preferred->v6 && preferred->port == 4443 && !listen.preferred_v4_get()); + CHECK(preferred && preferred->port == 4443 && !listen.preferred_v4_get()); // A constant slice, as a vector of clones. CHECK(!moq::ALPNS().empty()); } @@ -404,14 +406,14 @@ int main() { auto server = std::move(server_config).init(); CHECK(server); auto local = server->local_addr(); - CHECK(local && local->v6 && local->port != 0); + CHECK(local && local->v6 && local->addr.v6.port != 0); worker serving_thread; std::atomic stopped{false}; moq::task serving = std::move(*server).serve_publish(served.consume()).then(serving_thread.executor(), [&](auto &&) { stopped = true; }); moq::tokio_connect_Config connect = moq::tokio_connect_Config::default_(); - connect.url_set(moq::Url::parse("moqt://localhost:" + std::to_string(local->port)).value()); + connect.url_set(moq::Url::parse("moqt://localhost:" + std::to_string(local->addr.v6.port)).value()); moq::tokio_tls_Connect insecure = connect.tls_get(); insecure.insecure_set(true); connect.tls_set(std::move(insecure)); diff --git a/ffi/moq/moq.dart b/ffi/moq/moq.dart index 621588ed7..e83b3daf4 100644 --- a/ffi/moq/moq.dart +++ b/ffi/moq/moq.dart @@ -39,6 +39,11 @@ bool same(Uint8List? a, List b) => a != null && a.length == b.length && a.i /// Let the event loop run what is ready. Future step() => Future.delayed(Duration.zero); +/// `d` rounded up to the microsecond, as a timer waits at least that long: a Rust duration +/// holds nanoseconds, which `toDuration` refuses to drop. +Duration rounded(moq.RustDuration d) => + moq.RustDuration(d.secs).toDuration() + Duration(microseconds: (d.nanos + 999) ~/ 1000); + /// Poll `driver` from the isolate's event loop: on each wake, and at the deadline each poll /// returns, through a [Timer], with one waiter held across polls, since what a poll /// registers lives only as long as its waiter. It completes once the driver finishes, with @@ -56,7 +61,7 @@ Future drive(moq.OriginDriver driver) { try { final next = driver.poll(moq.clockNow(), waiter); if (next != null) { - deadline = Timer(next.saturatingDurationSince(moq.clockNow()), poll); + deadline = Timer(rounded(next.saturatingDurationSince(moq.clockNow())), poll); } } on moq.Error catch (e) { waiter.close(); @@ -117,8 +122,8 @@ Future local() async { // 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'); + final at = moq.Timestamp.fromMillis(moq.U64(1500)); + expect(at.asSecs() == moq.U64(1), '1500 ms is 1 s'); throws( () => writer.writeFrame(at, utf8.encode('late')), (e) => '$e' == "frame timestamp doesn't fit its track", @@ -211,12 +216,12 @@ Future catalog() async { ); // A subscription's ranges, from any iterable of them: a list. - final groups = moq.TrackRange.groups(const moq.BoundsU64(moq.BoundU64Included(2), moq.BoundU64Excluded(6))); + final groups = moq.TrackRange.groups(moq.BoundsU64(moq.BoundU64Included(moq.U64(2)), moq.BoundU64Excluded(moq.U64(6)))); final ranges = [moq.TrackRange.live(), groups]; final ranged = moq.TrackSubscription.default_().withRanges(ranges).rangesGet; expect(ranged.length == 2 && ranged[0] == ranges[0] && ranged[1] == ranges[1], 'a subscription keeps its ranges'); - expect(groups.start == const moq.TrackAnchorAbsolute(2), 'groups start at their first'); - expect(groups.end == const moq.TrackAnchorAbsolute(6), 'groups end before their end'); + expect(groups.start == moq.TrackAnchorAbsolute(moq.U64(2)), 'groups start at their first'); + expect(groups.end == moq.TrackAnchorAbsolute(moq.U64(6)), 'groups end before their end'); // A catalog at moq-mux's untyped extension, from the config Dart builds, whose generic // `is_claimed` is named by the last segment of its argument, and whose `mutate` lends @@ -310,9 +315,9 @@ void config() { expect(alpns.isNotEmpty && alpns.every((a) => a.isNotEmpty), 'each version has an ALPN'); versions.close(); // A socket address, crossing in a listener's bind. - final bind = moq.TokioListenBindAddr(moq.SocketAddr(InternetAddress('::1'), 4443)); + final bind = moq.TokioListenBindAddr(moq.SocketAddr(moq.IpAddr.from(InternetAddress('::1')), 4443)); expect('$bind' == '[::1]:4443', "an IPv6 bind prints as Rust's"); - final v4 = moq.SocketAddr(InternetAddress('127.0.0.1'), 0); + final v4 = moq.SocketAddrV4(moq.Ipv4Addr.from(InternetAddress('127.0.0.1')), 0); expect('${moq.TokioListenBindAddr(v4)}' == '127.0.0.1:0', "an IPv4 bind prints as Rust's"); final listen = moq.TokioListenConfig.default_(); listen.bindSet(bind); @@ -344,7 +349,7 @@ Future session() async { final serverConfig = moq.TokioServerConfig.default_(); final listen = serverConfig.listenGet; - listen.bindSet(moq.TokioListenBindAddr(moq.SocketAddr(InternetAddress('::'), 0))); + listen.bindSet(moq.TokioListenBindAddr(moq.SocketAddr(moq.IpAddr.from(InternetAddress('::')), 0))); final tls = listen.tlsGet; tls.generateSet(['localhost']); listen.tlsSet(tls); diff --git a/ffi/moq/moq.go b/ffi/moq/moq.go index 1a062c8bd..816050f42 100644 --- a/ffi/moq/moq.go +++ b/ffi/moq/moq.go @@ -113,8 +113,11 @@ func drive(driver *net.OriginDriver) <-chan error { return } var deadline <-chan time.Time + // A wait past time.Duration's 292 years waits for a wake alone. if next != nil { - deadline = time.After(next.SaturatingDurationSince(kio.ClockNow())) + if wait, err := next.SaturatingDurationSince(kio.ClockNow()).Std(); err == nil { + deadline = time.After(wait) + } } select { case <-woken: @@ -338,10 +341,10 @@ func script() { expect(len(alpns) > 0 && !slices.Contains(alpns, ""), "each version has an ALPN") versions.Close() // A socket address, crossing in a listener's bind. - bind := tokio.Bind(tokio.BindAddr{V0: netip.MustParseAddrPort("[::1]:4443")}) + bind := tokio.Bind(tokio.BindAddr{V0: must(rt.SocketAddrOf(netip.MustParseAddrPort("[::1]:4443")))}) expect(fmt.Sprint(bind) == "[::1]:4443", "an IPv6 bind prints as Rust's") - v4 := netip.MustParseAddrPort("127.0.0.1:0") - expect(tokio.BindAddr{V0: v4}.String() == "127.0.0.1:0", "an IPv4 bind prints as Rust's") + v4 := must(rt.SocketAddrV4Of(netip.MustParseAddrPort("127.0.0.1:0"))) + expect(tokio.BindAddr{V0: rt.SocketAddrFromV4(v4)}.String() == "127.0.0.1:0", "an IPv4 bind prints as Rust's") listen := tokio.ListenConfigDefault() check(listen.BindSet(&bind)) // The preferred address of each family, which takes only that family. @@ -377,14 +380,14 @@ func session() { serverConfig := tokio.ServerConfigDefault() listen := must(serverConfig.ListenGet()) - var bind tokio.Bind = tokio.BindAddr{V0: netip.MustParseAddrPort("[::]:0")} + var bind tokio.Bind = tokio.BindAddr{V0: rt.SocketAddrFromV6(rt.SocketAddrV6{})} check(listen.BindSet(&bind)) tls := must(listen.TlsGet()) check(tls.GenerateSet([]string{"localhost"})) check(listen.TlsSet(tls)) check(serverConfig.ListenSet(listen)) server := must(serverConfig.Init()) - local := must(server.LocalAddr()) + local := must(must(server.LocalAddr()).Netip()) expect(local.Port() != 0, "a server bound to port 0 reads back the port it got") serving, stop := context.WithCancel(ctx) defer stop() diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index fcc1e23e8..764428bb0 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -7,8 +7,6 @@ // script drains, after the write that wakes it. A session over QUIC runs on moq-tokio's // fallback runtime, whose threads resume Kotlin's. -import java.net.InetAddress -import java.net.InetSocketAddress import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.Executor import kotlin.time.Duration.Companion.seconds @@ -284,15 +282,16 @@ fun main() { versions.close() // A socket address, crossing in a listener's bind. val loopback = ByteArray(16).also { it[15] = 1 } - val bind = TokioListenBind.Addr(java.net.InetSocketAddress(java.net.Inet6Address.getByAddress(null, loopback, null as java.net.NetworkInterface?), 4443)) + val bind = TokioListenBind.Addr(SocketAddrV6(Ipv6Addr(loopback), 4443u)) expect(bind.toString() == "[::1]:4443", "an IPv6 bind prints as Rust's") - val v4 = TokioListenBind.Addr(java.net.InetSocketAddress(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1)), 0)) + val preferred = SocketAddrV4(Ipv4Addr(127u, 0u, 0u, 1u), 0u) + val v4 = TokioListenBind.Addr(preferred) expect(v4.toString() == "127.0.0.1:0", "an IPv4 bind prints as Rust's") TokioListenConfig.default().use { listen -> listen.bindSet(bind) // The preferred address of each family, which takes only that family. - listen.preferredV4Set(v4._0) - expect(listen.preferredV4Get == v4._0 && listen.preferredV6Get == null, "a listener keeps its preferred IPv4 address") + listen.preferredV4Set(preferred) + expect(listen.preferredV4Get == preferred && listen.preferredV6Get == null, "a listener keeps its preferred IPv4 address") expect(listen.bindGet == bind, "a listener's config keeps its bind") } // TLS files, as OS paths, and fingerprints, as strings: none by default. @@ -316,14 +315,14 @@ fun main() { val serverConfig = TokioServerConfig.default() val listen = serverConfig.listenGet - listen.bindSet(TokioListenBind.Addr(InetSocketAddress(InetAddress.getByName("::"), 0))) + listen.bindSet(TokioListenBind.Addr(SocketAddrV6(Ipv6Addr(ByteArray(16)), 0u))) val tls = listen.tlsGet tls.generateSet(listOf("localhost")) listen.tlsSet(tls) serverConfig.listenSet(listen) val server = serverConfig.init() val local = server.localAddr() - expect(local.port != 0, "a server bound to port 0 reads back the port it got") + expect(local.port.toInt() != 0, "a server bound to port 0 reads back the port it got") val serving = launch { server.servePublish(served.consume()) } val connect = TokioConnectConfig.default() diff --git a/ffi/moq/moq.py b/ffi/moq/moq.py index 5162ca2c4..5d2efe18d 100644 --- a/ffi/moq/moq.py +++ b/ffi/moq/moq.py @@ -42,7 +42,8 @@ async def drive(driver): while True: woken.clear() deadline = driver.poll(moq.clock_now(), waiter) - timeout = None if deadline is None else deadline.saturating_duration_since(moq.clock_now()).total_seconds() + left = None if deadline is None else deadline.saturating_duration_since(moq.clock_now()) + timeout = None if left is None else left.secs + left.nanos / 1e9 try: await asyncio.wait_for(woken.wait(), timeout) except TimeoutError: diff --git a/ffi/moq/moq.swift b/ffi/moq/moq.swift index 8a545fe0e..46b7d486d 100644 --- a/ffi/moq/moq.swift +++ b/ffi/moq/moq.swift @@ -402,11 +402,10 @@ func script() throws { let alpns = try versions.alpns() expect(!alpns.isEmpty && alpns.allSatisfy { !$0.isEmpty }, "each version has an ALPN") // A socket address, crossing in a listener's bind. - let loopback: [UInt8] = Array(repeating: 0, count: 15) + [1] - let bind = TokioListenBind.addr(SocketAddr(ip: loopback, port: 4443)) + let bind = TokioListenBind.addr(.v6(SocketAddrV6(ip: Ipv6Addr(bits: U128(1)), port: 4443))) expect("\(bind)" == "[::1]:4443", "an IPv6 bind prints as Rust's") - let v4 = SocketAddr(ip: [127, 0, 0, 1], port: 0) - expect("\(TokioListenBind.addr(v4))" == "127.0.0.1:0", "an IPv4 bind prints as Rust's") + let v4 = SocketAddrV4(ip: Ipv4Addr(127, 0, 0, 1), port: 0) + expect("\(TokioListenBind.addr(.v4(v4)))" == "127.0.0.1:0", "an IPv4 bind prints as Rust's") var listen = TokioListenConfig.default() try listen.bindSet(bind) // The preferred address of each family, which takes only that family. @@ -436,7 +435,7 @@ func script() throws { var serverConfig = TokioServerConfig.default() var listen = try serverConfig.listenGet() - try listen.bindSet(.addr(SocketAddr(ip: Array(repeating: 0, count: 16), port: 0))) + try listen.bindSet(.addr(.v6(SocketAddrV6(ip: Ipv6Addr(bits: U128(0)), port: 0)))) var tls = try listen.tlsGet() try tls.generateSet(["localhost"]) try listen.tlsSet(tls) diff --git a/ffi/runtime/ffi_cgo.go b/ffi/runtime/ffi_cgo.go index 3abf4878f..138b62213 100644 --- a/ffi/runtime/ffi_cgo.go +++ b/ffi/runtime/ffi_cgo.go @@ -1,6 +1,6 @@ // What each generated package converts its C types with, which cgo makes its own, after // the generated code names the export's C types as cBuf, cWaker, cForeignFuture, -// cForeignReader, cU128, cDuration, and cAddr, its buffer's free and copy as cBufFree and +// cForeignReader, cU128, cDuration, and each address's, its buffer's free and copy as cBufFree and // cBufCopy, and the runtime's callbacks into Go as goCall, goRelease, goPoll, goRead, and // goSeek: the arena a call lowers nested values into, values across the C ABI, and what a // future waits on. @@ -123,148 +123,169 @@ func lentBytesOption_(buf cBuf) *[]byte { return &b } -// lowerU128_ is a u128 as Rust holds it, from 0 through 2^128 - 1. -func lowerU128_(v *big.Int) cU128 { - if v.Sign() < 0 || v.BitLen() > 128 { - panic("an int out of range for Rust's u128") - } - var b [16]byte - v.FillBytes(b[:]) - var hi, lo uint64 - for i := range 8 { - hi = hi<<8 | uint64(b[i]) - lo = lo<<8 | uint64(b[8+i]) - } - return cU128{lo: C.uint64_t(lo), hi: C.uint64_t(hi)} +func lowerU128_(v rt.U128) cU128 { + return cU128{lo: C.uint64_t(v.Lo), hi: C.uint64_t(v.Hi)} } -func liftU128_(c cU128) *big.Int { - hi := new(big.Int).SetUint64(uint64(c.hi)) - return hi.Lsh(hi, 64).Or(hi, new(big.Int).SetUint64(uint64(c.lo))) +func liftU128_(c cU128) rt.U128 { + return rt.U128{Hi: uint64(c.hi), Lo: uint64(c.lo)} } -// lowerDuration_ is a Duration as Rust holds it, which has no negative one. -func lowerDuration_(d time.Duration) cDuration { - if d < 0 { - panic("a negative duration has no Rust Duration") - } - return cDuration{secs: C.uint64_t(d / time.Second), nanos: C.uint32_t(d % time.Second)} +func lowerDuration_(d rt.Duration) cDuration { + return cDuration{secs: C.uint64_t(d.Secs()), nanos: C.uint32_t(d.Nanos())} } -// liftDuration_ is a Duration Rust returned, which panics past time.Duration's longest, -// about 292 years. -func liftDuration_(c cDuration) time.Duration { - secs, nanos := uint64(c.secs), time.Duration(c.nanos) - if secs > uint64((math.MaxInt64-nanos)/time.Second) { - panic("a Rust Duration past time.Duration's longest") - } - return time.Duration(secs)*time.Second + nanos +// liftDuration_ is a Duration Rust returned, whose nanoseconds are below a second, so it +// never fails. +func liftDuration_(c cDuration) rt.Duration { + d, _ := rt.NewDuration(uint64(c.secs), uint32(c.nanos)) + return d } -// lowerAddr_ is an address as Rust's SocketAddr holds it: a zone names IPv6's scope by -// its number or its interface's name. -func lowerAddr_(a netip.AddrPort) cAddr { - ip := a.Addr() - if !ip.IsValid() { - panic("an invalid address has no Rust SocketAddr") - } - var c cAddr - c.port = C.uint16_t(a.Port()) - if ip.Is4() { - b := ip.As4() - copy(unsafe.Slice((*byte)(unsafe.Pointer(&c.ip)), 4), b[:]) - return c - } - b := ip.As16() - copy(unsafe.Slice((*byte)(unsafe.Pointer(&c.ip)), 16), b[:]) - c.v6 = true - if zone := ip.Zone(); zone != "" { - if id, err := strconv.ParseUint(zone, 10, 32); err == nil { - c.scope_id = C.uint32_t(id) - } else if iface, err := net.InterfaceByName(zone); err == nil { - c.scope_id = C.uint32_t(iface.Index) - } else { - panic("an IPv6 zone that names no interface: " + zone) - } +// The addresses, whose C structs hold their octets as Go's arrays do. + +func lowerIpv4Addr_(ip rt.Ipv4Addr) cIpv4Addr { + return *(*cIpv4Addr)(unsafe.Pointer(&ip)) +} + +func liftIpv4Addr_(c cIpv4Addr) rt.Ipv4Addr { + return *(*rt.Ipv4Addr)(unsafe.Pointer(&c)) +} + +func lowerIpv6Addr_(ip rt.Ipv6Addr) cIpv6Addr { + return *(*cIpv6Addr)(unsafe.Pointer(&ip)) +} + +func liftIpv6Addr_(c cIpv6Addr) rt.Ipv6Addr { + return *(*rt.Ipv6Addr)(unsafe.Pointer(&c)) +} + +func lowerIpAddr_(a rt.IpAddr) cIpAddr { + var c cIpAddr + if v6, ok := a.V6(); ok { + c.v6 = true + *(*cIpv6Addr)(unsafe.Pointer(&c.ip)) = lowerIpv6Addr_(v6) + } else { + v4, _ := a.V4() + *(*cIpv4Addr)(unsafe.Pointer(&c.ip)) = lowerIpv4Addr_(v4) } return c } -// lowerAddrV4_ is an IPv4 address, for Rust's SocketAddrV4. -func lowerAddrV4_(a netip.AddrPort) cAddr { - if !a.Addr().Is4() { - panic("an address that is not IPv4 has no Rust SocketAddrV4") +func liftIpAddr_(c cIpAddr) rt.IpAddr { + if c.v6 { + return rt.IpAddrFromV6(liftIpv6Addr_(*(*cIpv6Addr)(unsafe.Pointer(&c.ip)))) } - return lowerAddr_(a) + return rt.IpAddrFromV4(liftIpv4Addr_(*(*cIpv4Addr)(unsafe.Pointer(&c.ip)))) +} + +func lowerSocketAddrV4_(a rt.SocketAddrV4) cSocketAddrV4 { + return cSocketAddrV4{ip: lowerIpv4Addr_(a.IP), port: C.uint16_t(a.Port)} } -// lowerAddrV6_ is an IPv6 address, for Rust's SocketAddrV6. -func lowerAddrV6_(a netip.AddrPort) cAddr { - if !a.Addr().Is6() { - panic("an address that is not IPv6 has no Rust SocketAddrV6") +func liftSocketAddrV4_(c cSocketAddrV4) rt.SocketAddrV4 { + return rt.SocketAddrV4{IP: liftIpv4Addr_(c.ip), Port: uint16(c.port)} +} + +func lowerSocketAddrV6_(a rt.SocketAddrV6) cSocketAddrV6 { + return cSocketAddrV6{ + ip: lowerIpv6Addr_(a.IP), + port: C.uint16_t(a.Port), + flowinfo: C.uint32_t(a.FlowInfo), + scope_id: C.uint32_t(a.ScopeID), } - return lowerAddr_(a) } -// liftAddr_ is an address Rust returned. Go keeps no IPv6 flow label, so one panics. -func liftAddr_(c cAddr) netip.AddrPort { - b := unsafe.Slice((*byte)(unsafe.Pointer(&c.ip)), 16) - if !c.v6 { - return netip.AddrPortFrom(netip.AddrFrom4([4]byte(b[:4])), uint16(c.port)) +func liftSocketAddrV6_(c cSocketAddrV6) rt.SocketAddrV6 { + return rt.SocketAddrV6{ + IP: liftIpv6Addr_(c.ip), + Port: uint16(c.port), + FlowInfo: uint32(c.flowinfo), + ScopeID: uint32(c.scope_id), } - if c.flowinfo != 0 { - panic("an IPv6 flow label has no Go form") +} + +func lowerSocketAddr_(a rt.SocketAddr) cSocketAddr { + var c cSocketAddr + if v6, ok := a.V6(); ok { + c.v6 = true + *(*cSocketAddrV6)(unsafe.Pointer(&c.addr)) = lowerSocketAddrV6_(v6) + } else { + v4, _ := a.V4() + *(*cSocketAddrV4)(unsafe.Pointer(&c.addr)) = lowerSocketAddrV4_(v4) } - ip := netip.AddrFrom16([16]byte(b)) - if c.scope_id != 0 { - ip = ip.WithZone(strconv.FormatUint(uint64(c.scope_id), 10)) + return c +} + +func liftSocketAddr_(c cSocketAddr) rt.SocketAddr { + if c.v6 { + return rt.SocketAddrFromV6(liftSocketAddrV6_(*(*cSocketAddrV6)(unsafe.Pointer(&c.addr)))) } - return netip.AddrPortFrom(ip, uint16(c.port)) + return rt.SocketAddrFromV4(liftSocketAddrV4_(*(*cSocketAddrV4)(unsafe.Pointer(&c.addr)))) } -// lowerIp_ is an address as Rust's IpAddr holds it, which has no room for a zone. -func lowerIp_(ip netip.Addr) cIpAddr { - if !ip.IsValid() { - panic("an invalid address has no Rust IpAddr") +// What a call returns before it is made once it is given text that is not UTF-8, a rune +// that is not a Unicode scalar value, or a zero for a NonZero, which Rust's types lack. +var ( + errText_ = rt.Invalid_("a string is not UTF-8") + errRune_ = rt.Invalid_("a rune is not a Unicode scalar value") + errPath_ = rt.Invalid_("a path is not WTF-8, as a Windows path is") + errNonZero_ = rt.Invalid_("a NonZero is zero") +) + +// checkPath_ is the error a call given the path s returns, or nil for any on a platform +// whose paths are bytes, or on Windows, WTF-8: UTF-8 but for a surrogate alone, never a +// lead and a trail apart, which WTF-8 writes as the scalar they make. +func checkPath_(s string) error { + if runtime.GOOS != "windows" || utf8.ValidString(s) { + return nil } - if ip.Zone() != "" { - panic("an IPv6 address with a zone has no Rust IpAddr") + lead := false + for i := 0; i < len(s); { + if r, n := utf8.DecodeRuneInString(s[i:]); r != utf8.RuneError || n != 1 { + i, lead = i+n, false + continue + } + if i+3 > len(s) || s[i] != 0xED || s[i+1] < 0xA0 || s[i+1] > 0xBF || s[i+2] < 0x80 || s[i+2] > 0xBF { + return errPath_ + } + trail := s[i+1] >= 0xB0 + if trail && lead { + return errPath_ + } + i, lead = i+3, !trail } - var c cIpAddr - if ip.Is4() { - b := ip.As4() - copy(unsafe.Slice((*byte)(unsafe.Pointer(&c.ip)), 4), b[:]) - return c - } - b := ip.As16() - copy(unsafe.Slice((*byte)(unsafe.Pointer(&c.ip)), 16), b[:]) - c.v6 = true - return c + return nil } -// lowerIpV4_ is an IPv4 address, for Rust's Ipv4Addr. -func lowerIpV4_(ip netip.Addr) cIpAddr { - if !ip.Is4() { - panic("an address that is not IPv4 has no Rust Ipv4Addr") +// checkString_ is the error a call given s returns, or nil for UTF-8. +func checkString_(s string) error { + if !utf8.ValidString(s) { + return errText_ } - return lowerIp_(ip) + return nil } -// lowerIpV6_ is an IPv6 address, for Rust's Ipv6Addr. -func lowerIpV6_(ip netip.Addr) cIpAddr { - if !ip.Is6() { - panic("an address that is not IPv6 has no Rust Ipv6Addr") +func checkPathOption_(s *string) error { + if s == nil { + return nil } - return lowerIp_(ip) + return checkPath_(*s) } -// liftIp_ is an address Rust returned. -func liftIp_(c cIpAddr) netip.Addr { - b := unsafe.Slice((*byte)(unsafe.Pointer(&c.ip)), 16) - if !c.v6 { - return netip.AddrFrom4([4]byte(b[:4])) +func checkStringOption_(s *string) error { + if s == nil { + return nil } - return netip.AddrFrom16([16]byte(b)) + return checkString_(*s) +} + +// checkRune_ is the error a call given r returns, or nil for a Unicode scalar value. +func checkRune_(r rune) error { + if !utf8.ValidRune(r) { + return errRune_ + } + return nil } // liftList_ is the array Rust returned, each item lifted by lift, which takes what it @@ -314,8 +335,8 @@ type arena_ struct { // call was made. taken []cBuf made bool - // The first object the call takes that was closed, which fails the call before it is - // made. + // What first failed the call before it is made: an object it takes that was closed, or + // a value it takes that Rust's type does not hold. err error } @@ -337,12 +358,49 @@ func (a *arena_) pin(p unsafe.Pointer) { } } +// bufString is s, which C reads during the call, pinned, failing the call unless UTF-8. func (a *arena_) bufString(s string) cBuf { + if err := checkString_(s); err != nil { + a.fail(err) + } + return a.bufPath(s) +} + +// bufPath is a path's bytes, which C reads during the call, pinned: any a string holds, +// but on Windows only WTF-8, failing the call otherwise. +func (a *arena_) bufPath(s string) cBuf { + if err := checkPath_(s); err != nil { + a.fail(err) + } b := bufString_(s) a.pin(unsafe.Pointer(b.ptr)) return b } +// char is r, failing the call unless a Unicode scalar value. +func (a *arena_) char(r rune) C.uint32_t { + if err := checkRune_(r); err != nil { + a.fail(err) + } + return C.uint32_t(r) +} + +// nonZero_ is v, a NonZero's integer, failing the call if it is zero. +func nonZero_[T comparable](a *arena_, v T) T { + var zero T + if v == zero { + a.fail(errNonZero_) + } + return v +} + +// fail fails the call with err before it is made, unless it already failed. +func (a *arena_) fail(err error) { + if a.err == nil { + a.err = err + } +} + // bufView is the buffer of b, which C reads during the call: Go's memory pinned, and // Rust's kept reachable, or where a.take, a second owner of it, which Rust takes. func (a *arena_) bufView(b rt.Bytes) cBuf { @@ -372,6 +430,13 @@ func (a *arena_) bufStringOption(s *string) cBuf { return a.bufString(*s) } +func (a *arena_) bufPathOption(s *string) cBuf { + if s == nil { + return cBuf{} + } + return a.bufPath(*s) +} + func (a *arena_) bufViewOption(b *rt.Bytes) cBuf { if b == nil { return cBuf{} @@ -384,9 +449,7 @@ func (a *arena_) bufViewOption(b *rt.Bytes) cBuf { func (a *arena_) hold(h *rt.Handle_, o any) unsafe.Pointer { raw, s := h.Hold() if raw == nil { - if a.err == nil { - a.err = h.Closed() - } + a.fail(h.Closed()) return nil } a.held = append(a.held, held_{h, s, o}) diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index ca6684bc1..85bee7168 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -140,6 +140,36 @@ pub fn refuse(env: *mut JNIEnv, status: i32, what: &str) -> R { } } +static INVALID: OnceLock = OnceLock::new(); + +/// Throw the `_Native.__Invalid`, an `IllegalArgumentException`, an argument holding a +/// value its Rust type cannot raises, which the native method then returns with, as +/// [`raised`] does. Its own class tells Kotlin the call moved nothing. +#[cold] +fn invalid(env: *mut JNIEnv, message: &CStr) { + let id = method(env, &INVALID, c"__invalid", c"(Ljava/lang/String;)Ljava/lang/Throwable;"); + // SAFETY: `env` is this thread's, and `__invalid` takes the message. + unsafe { + let text = ((**env).v1_2.NewStringUTF)(env, message.as_ptr()); + if text.is_null() { + fail(env, "jni", "a message did not fit"); + } + let args = [jvalue { l: text }]; + let invalid = ((**env).v1_2.CallStaticObjectMethodA)(env, class(), id, args.as_ptr()); + if invalid.is_null() || ((**env).v1_2.ExceptionCheck)(env) { + fail(env, "jni", "a call into Kotlin threw"); + } + ((**env).v1_2.Throw)(env, invalid); + } +} + +/// What a native method returns once it threw, which Kotlin never reads: zeroed, as a +/// JNI scalar or a null reference is. +pub fn raised() -> R { + // SAFETY: a native method returns only JNI scalars and references, all valid zeroed. + unsafe { std::mem::zeroed() } +} + /// Run `f` calling into Kotlin on this thread, freeing the local references it makes, and /// abort if what it calls throws: Rust has nowhere to unwind it to. pub fn upcall(what: &str, f: impl FnOnce(*mut JNIEnv) -> R) -> R { @@ -281,18 +311,24 @@ pub fn new_block(env: *mut JNIEnv, list: rt::List) -> jarray { } } -/// A `String`'s UTF-8, encoded once into a buffer Rust takes, or none for null. -pub fn text(env: *mut JNIEnv, text: jstring) -> rt::Buf { +/// A `String`'s UTF-8, encoded once, or none for null; or else, for one holding a lone +/// surrogate, which UTF-8 has no form of, none at all, having thrown. +pub fn text(env: *mut JNIEnv, text: jstring) -> Option>> { match text.is_null() { - true => rt::Buf::NONE, - false => rt::Buf::vec(utf8(env, text)), + true => Some(None), + false => utf8(env, text).map(Some), } } -/// A live `String`'s UTF-8, as Kotlin's `encodeToByteArray` encodes it: a lone surrogate -/// is `?`. JNI's own UTF-8 is modified, which differs for NUL and past the BMP, so the -/// UTF-16 is read a chunk at a time and encoded here. -fn utf8(env: *mut JNIEnv, text: jstring) -> Vec { +/// Bytes Rust takes, or none. +pub fn buf(bytes: Option>) -> rt::Buf { + bytes.map_or(rt::Buf::NONE, rt::Buf::vec) +} + +/// A live `String`'s UTF-8, or none for one holding a lone surrogate, having thrown. JNI's +/// own UTF-8 is modified, which differs for NUL and past the BMP, so the UTF-16 is read a +/// chunk at a time and encoded here, checked as it is. +fn utf8(env: *mut JNIEnv, text: jstring) -> Option> { const CHUNK: usize = 1024; // SAFETY: `text` is a live `String` the call was given. let len = unsafe { ((**env).v1_2.GetStringLength)(env, text) } as usize; @@ -313,11 +349,14 @@ fn utf8(env: *mut JNIEnv, text: jstring) -> Vec { match c { Ok(c) if c.is_ascii() => out.push(c as u8), Ok(c) => out.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()), - Err(_) => out.push(b'?'), + Err(_) => { + invalid(env, c"a String with a lone surrogate has no UTF-8, as Rust's text is"); + return None; + } } } } - out + Some(out) } /// `len` bytes of a live `byte[]` from `start`, copied, failing loud past its end, which @@ -889,9 +928,12 @@ pub unsafe fn drop_returned(raw: jlong) { pub struct Reader<'a> { bytes: &'a [u8], arrays: Vec>, - /// The call's thread, and the arrays and text the encoding names by place. + /// The call's thread, and the arrays the encoding names by place. env: *mut JNIEnv, refs: jobjectArray, + /// The UTF-8 of each text the encoding names, in order, and how many it has read. + texts: Vec>, + read: usize, /// Whether the value read is one Rust takes, so Rust's own bytes in it, outside an /// array, cross as a second owner of them rather than a view. take: bool, @@ -899,41 +941,90 @@ pub struct Reader<'a> { impl<'a> Reader<'a> { /// A reader of a value Rust only reads, as through a pointer, whose encoding names - /// the objects of `refs` by place. - pub fn new(env: *mut JNIEnv, bytes: &'a [u8], refs: jobjectArray) -> Self { + /// the objects of `refs` by place, with the place of each text after it; or none, + /// having thrown, if a text holds a lone surrogate. Each text is encoded before any of + /// the value is read, so nothing read needs freeing when one throws. + pub fn new(env: *mut JNIEnv, bytes: &'a [u8], refs: jobjectArray) -> Option { + let mut r = Self::plain(bytes); + if refs.is_null() { + return Some(r); + } + (r.env, r.refs) = (env, refs); + let count = r.trailer(1)[0]; + let places = r.trailer(count as usize); + r.texts = Vec::with_capacity(places.len()); + for place in places { + let text = r.object(place, |env, text| utf8(env, text))?; + r.texts.push(text); + } + Some(r) + } + + /// A reader of a value passed by value, which Rust takes. + pub fn taking(env: *mut JNIEnv, bytes: &'a [u8], refs: jobjectArray) -> Option { + Self::new(env, bytes, refs).map(|r| Self { take: true, ..r }) + } + + /// A reader of a value that names no object, as a callback's return. + pub fn plain(bytes: &'a [u8]) -> Self { Self { bytes, arrays: Vec::new(), - env, - refs, + env: std::ptr::null_mut(), + refs: std::ptr::null_mut(), + texts: Vec::new(), + read: 0, take: false, } } - /// A reader of a value passed by value, which Rust takes. - pub fn taking(env: *mut JNIEnv, bytes: &'a [u8], refs: jobjectArray) -> Self { - Self { - take: true, - ..Self::new(env, bytes, refs) - } + /// The last `n` `i32`s of the encoding, taken off its end. + fn trailer(&mut self, n: usize) -> Vec { + let Some(at) = self.bytes.len().checked_sub(4 * n) else { + rt::fail("jni", "an encoded value is truncated"); + }; + let (head, tail) = self.bytes.split_at(at); + self.bytes = head; + tail.chunks_exact(4) + .map(|b| i32::from_le_bytes(b.try_into().expect("4 bytes"))) + .collect() } - /// Bytes made once by `make` from the object at `at` of `refs`: taken where the reader - /// takes, else kept until it drops and read in place. - fn referred(&mut self, at: i32, make: impl FnOnce(*mut JNIEnv, jobject) -> Vec) -> rt::Buf { - let env = self.env; + /// What `make` makes of the object at `at` of `refs`. + fn object(&mut self, at: i32, make: impl FnOnce(*mut JNIEnv, jobject) -> T) -> T { if self.refs.is_null() { - fail(env, "jni", "an encoded value names an object, but none were passed"); + rt::fail("jni", "an encoded value names an object, but none were passed"); } + let env = self.env; // SAFETY: `refs` is the live `Object[]` the call was given; JNI throws past its end. let object = unsafe { ((**env).v1_2.GetObjectArrayElement)(env, self.refs, at) }; // SAFETY: as above. if object.is_null() || unsafe { ((**env).v1_2.ExceptionCheck)(env) } { fail(env, "jni", "an encoded value names no object"); } - let bytes = make(env, object); + let made = make(env, object); // SAFETY: a local reference made here. unsafe { ((**env).v1_2.DeleteLocalRef)(env, object) }; + made + } + + /// The next text the encoding names, as [`Self::kept`] keeps it. + fn text(&mut self) -> rt::Buf { + let Some(text) = self.texts.get_mut(self.read) else { + rt::fail("jni", "an encoded value names more text than it has"); + }; + self.read += 1; + match self.take { + true => rt::Buf::vec(std::mem::take(text)), + // The vector's heap block, which the view reads, stays put until the reader drops. + false => rt::Buf::view(text), + } + } + + /// Bytes made once by `make` from the object at `at` of `refs`: taken where the reader + /// takes, else kept until it drops and read in place. + fn referred(&mut self, at: i32, make: impl FnOnce(*mut JNIEnv, jobject) -> Vec) -> rt::Buf { + let bytes = self.object(at, make); if self.take { return rt::Buf::vec(bytes); } @@ -1115,7 +1206,8 @@ impl Wire for bool { } /// What crosses as 64 bits, at the target's width: on a 32-bit target, a value past it -/// has no `usize` or `isize` to be. +/// has no `usize` or `isize` to be, which Kotlin's `__usize` and `__isize` throw on before +/// it crosses, so this never fails for a value Kotlin passes. pub fn narrow, W>(wide: W) -> T { match T::try_from(wide) { Ok(v) => v, @@ -1203,10 +1295,7 @@ impl Wire for rt::Buf { let (start, len) = (r.get::(), r.get::()); r.referred(at, |env, array| region(env, array, start, len)) } - TEXT => { - let at = r.get::(); - r.referred(at, utf8) - } + TEXT => r.text(), len => match usize::try_from(len) { Ok(len) => rt::Buf::view(r.take(len)), Err(_) => rt::Buf::NONE, @@ -1241,8 +1330,8 @@ const LENT: i32 = -2; /// start and how many, where an encoded buffer's length would. const KOTLIN: i32 = -3; -/// What precedes the place in a call's objects of Kotlin's `String` where an encoded -/// buffer's length would. +/// What marks the next of a call's texts, which its objects hold, where an encoded buffer's +/// length would. const TEXT: i32 = -4; impl Wire for Owned { @@ -1327,37 +1416,95 @@ impl Wire for rt::Duration { } } -impl Wire for rt::SocketAddr { +impl Wire for rt::Ipv4Addr { fn read(r: &mut Reader<'_>) -> Self { Self { - ip: std::array::from_fn(|_| r.get()), + octets: std::array::from_fn(|_| r.get()), + } + } + + fn write(self, w: &mut Writer) { + self.octets.into_iter().for_each(|b| w.put(b)); + } +} + +impl Wire for rt::Ipv6Addr { + fn read(r: &mut Reader<'_>) -> Self { + Self { + octets: std::array::from_fn(|_| r.get()), + } + } + + fn write(self, w: &mut Writer) { + self.octets.into_iter().for_each(|b| w.put(b)); + } +} + +/// Whether it is IPv6, then the address of its family. +impl Wire for rt::IpAddr { + fn read(r: &mut Reader<'_>) -> Self { + match r.get::() { + true => Self::lower(r.get::().lift().into()), + false => Self::lower(r.get::().lift().into()), + } + } + + fn write(self, w: &mut Writer) { + w.put(self.v6); + match self.lift() { + std::net::IpAddr::V4(ip) => w.put(rt::Ipv4Addr::lower(ip)), + std::net::IpAddr::V6(ip) => w.put(rt::Ipv6Addr::lower(ip)), + } + } +} + +impl Wire for rt::SocketAddrV4 { + fn read(r: &mut Reader<'_>) -> Self { + Self { + ip: r.get(), + port: r.get(), + } + } + + fn write(self, w: &mut Writer) { + w.put(self.ip); + w.put(self.port); + } +} + +impl Wire for rt::SocketAddrV6 { + fn read(r: &mut Reader<'_>) -> Self { + Self { + ip: r.get(), port: r.get(), - v6: r.get(), flowinfo: r.get(), scope_id: r.get(), } } fn write(self, w: &mut Writer) { - self.ip.into_iter().for_each(|b| w.put(b)); + w.put(self.ip); w.put(self.port); - w.put(self.v6); w.put(self.flowinfo); w.put(self.scope_id); } } -impl Wire for rt::IpAddr { +/// Whether it is IPv6, then the address of its family. +impl Wire for rt::SocketAddr { fn read(r: &mut Reader<'_>) -> Self { - Self { - ip: std::array::from_fn(|_| r.get()), - v6: r.get(), + match r.get::() { + true => Self::lower(r.get::().lift().into()), + false => Self::lower(r.get::().lift().into()), } } fn write(self, w: &mut Writer) { - self.ip.into_iter().for_each(|b| w.put(b)); w.put(self.v6); + match self.lift() { + std::net::SocketAddr::V4(addr) => w.put(rt::SocketAddrV4::lower(addr)), + std::net::SocketAddr::V6(addr) => w.put(rt::SocketAddrV6::lower(addr)), + } } } diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 6fc05cdfd..50821a85f 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -470,10 +470,10 @@ static TYPES: Gil> = Gil::new(Vec::new()); static MEMBERS: Gil>>> = Gil::new(Vec::new()); /// The runtime's entries, at the start of the registry: the awaitable a pending future -/// is, `datetime.timedelta`, `_resume`, which schedules the running loop's dispatch, +/// is, the runtime's `Duration`, `_resume`, which schedules the running loop's dispatch, /// `Waker`, `_Pollable`, which runs an awaitable Rust polls, and `ipaddress`'s two classes. pub const T_FUTURE: usize = 0; -const T_TIMEDELTA: usize = 1; +const T_DURATION: usize = 1; const T_RESUME: usize = 2; const T_WAKER: usize = 3; const T_POLLABLE: usize = 4; @@ -641,6 +641,15 @@ pub fn int_in>(obj: *mut PyObject) -> R { T::try_from(value).or_else(|_| overflow_int::()) } +/// A `NonZero`'s integer, as `int_in`, with a zero raising `ValueError`. +pub fn nonzero_in + Default + PartialEq>(obj: *mut PyObject) -> R { + let value = int_in::(obj)?; + match value == T::default() { + true => value_error("a NonZero is zero"), + false => Ok(value), + } +} + fn overflow_int() -> R { let name = std::any::type_name::(); overflow(&format!("int out of range for Rust's {name}")) @@ -1096,6 +1105,15 @@ pub fn path_in(obj: *mut PyObject, s: &mut Scope) -> R { if PyBytes_AsStringAndSize(encoded, &mut ptr, &mut len) != 0 { return Err(Raised); } + // Windows' file system encoding passes a surrogate through, but a lead and a trail + // written apart are no path, which WTF-8 writes as the scalar they make. + #[cfg(windows)] + { + let bytes = std::slice::from_raw_parts(ptr.cast::(), len as usize); + if std::str::from_utf8(bytes).is_err() && rt::wtf8(bytes).is_none() { + return value_error("a str with a surrogate pair apart is no Windows path"); + } + } Ok(rt::Buf { ptr: ptr.cast_const().cast(), len: len as usize, @@ -1114,58 +1132,41 @@ pub fn path_out(buf: rt::Buf) -> R { } } -static DAYS: Key = Key::new(c"days"); -static SECONDS: Key = Key::new(c"seconds"); -static MICROSECONDS: Key = Key::new(c"microseconds"); -static MAX: Key = Key::new(c"max"); - -/// The days in `timedelta.max`, past which [`duration_out`] gives it. -const MAX_DAYS: u64 = 999_999_999; - -/// A `Duration`, from a `datetime.timedelta` that is not negative: `timedelta.max`, which -/// [`duration_out`] gives for any longer one, is `Duration::MAX`. +/// A `Duration`, from the runtime's `Duration`: seconds a `u64` holds, and nanoseconds below +/// a billion, else `OverflowError` or `ValueError`. pub fn duration_in(obj: *mut PyObject) -> R { - if !is(obj, T_TIMEDELTA) { - return expected(obj, T_TIMEDELTA); - } - let mut s = Scope::default(); - let days: u64 = match int_in::(attr(obj, &DAYS, &mut s)?)? { - days if days < 0 => return value_error("a negative timedelta has no Rust Duration"), - days => days as u64, + if !is(obj, T_DURATION) { + return expected(obj, T_DURATION); + } + // SAFETY: the GIL is held, and `obj` a `Duration`, a tuple of its two fields. + let (secs, nanos) = unsafe { (PyTuple_GetItem(obj, 0), PyTuple_GetItem(obj, 1)) }; + let duration = rt::Duration { + secs: int_in(secs)?, + nanos: int_in(nanos)?, }; - let seconds: u64 = int_in(attr(obj, &SECONDS, &mut s)?)?; - let micros: u32 = int_in(attr(obj, &MICROSECONDS, &mut s)?)?; - if (days, seconds, micros) == (MAX_DAYS, 86_399, 999_999) { - return Ok(rt::Duration::lower(std::time::Duration::MAX)); - } - Ok(rt::Duration { - secs: days * 86_400 + seconds, - nanos: micros * 1_000, - }) + match duration.nanos < 1_000_000_000 { + true => Ok(duration), + false => value_error("a Duration's nanos are below a billion"), + } } -/// A `datetime.timedelta`, which holds whole microseconds up to 999,999,999 days: Rust's -/// nanoseconds are truncated, and a longer one is `timedelta.max`, as Rust's -/// `Duration::MAX` stands for forever, which [`duration_in`] gives back. +/// The runtime's `Duration`, made by `tuple`'s own constructor, which (unlike a bare +/// allocation) marks the hash CPython 3.14+ caches as not yet computed, and skips the +/// `NamedTuple`'s Python `__new__`. pub fn duration_out(duration: rt::Duration) -> R { - let class = ty(T_TIMEDELTA); - // SAFETY: the GIL is held. + let items = tuple(vec![uint_out(duration.secs), uint_out(duration.nanos.into())]); + let args = tuple(vec![items])?; + // SAFETY: the GIL is held; `tuple`'s `tp_new` takes a subclass and, as `tuple(items)` + // does, its arguments. unsafe { - if duration.secs / 86_400 > MAX_DAYS { - return Obj::new(PyObject_GetAttr(class, MAX.get()?)); - } - let args = tuple(vec![ - int_out(0), - uint_out(duration.secs), - uint_out(u64::from(duration.nanos / 1_000)), - ])?; - Obj::new(PyObject_CallObject(class, args.ptr())) + let new: newfunc = std::mem::transmute(PyType_GetSlot(&raw mut PyTuple_Type, Py_tp_new)); + Obj::new(new(ty(T_DURATION).cast(), args.ptr(), null_mut())) } } /// Which addresses a socket address type holds. #[derive(Clone, Copy, PartialEq, Eq)] -pub enum Family { +enum Family { Any, V4, V6, @@ -1173,7 +1174,28 @@ pub enum Family { /// A socket address, from a tuple as Python's `socket` module writes one: `(host, port)`, /// or for IPv6, `(host, port, flowinfo, scope_id)`, its host a numeric address. -pub fn addr_in(obj: *mut PyObject, family: Family) -> R { +pub fn addr_in(obj: *mut PyObject) -> R { + socket_in(obj, Family::Any).map(rt::SocketAddr::lower) +} + +/// An IPv4 socket address, as [`addr_in`] takes one. +pub fn addr_v4_in(obj: *mut PyObject) -> R { + match socket_in(obj, Family::V4)? { + std::net::SocketAddr::V4(addr) => Ok(rt::SocketAddrV4::lower(addr)), + std::net::SocketAddr::V6(_) => unreachable!("checked IPv4"), + } +} + +/// An IPv6 socket address, as [`addr_in`] takes one. +pub fn addr_v6_in(obj: *mut PyObject) -> R { + match socket_in(obj, Family::V6)? { + std::net::SocketAddr::V6(addr) => Ok(rt::SocketAddrV6::lower(addr)), + std::net::SocketAddr::V4(_) => unreachable!("checked IPv6"), + } +} + +/// A socket address of `family`, as [`addr_in`] takes one. +fn socket_in(obj: *mut PyObject, family: Family) -> R { // SAFETY: the GIL is held, and `obj` is live. let len = unsafe { if PyTuple_Check(obj) == 0 { @@ -1205,18 +1227,30 @@ pub fn addr_in(obj: *mut PyObject, family: Family) -> R { match (family, addr) { (Family::V4, std::net::SocketAddr::V6(_)) => value_error("an IPv6 address has no Rust SocketAddrV4"), (Family::V6, std::net::SocketAddr::V4(_)) => value_error("an IPv4 address has no Rust SocketAddrV6"), - _ => Ok(rt::SocketAddr::lower(addr)), + _ => Ok(addr), } } -/// A socket address as Python's `socket` module writes one. +pub fn addr_v4_out(addr: rt::SocketAddrV4) -> R { + socket_out(addr.lift().into()) +} + +pub fn addr_v6_out(addr: rt::SocketAddrV6) -> R { + socket_out(addr.lift().into()) +} + pub fn addr_out(addr: rt::SocketAddr) -> R { + socket_out(addr.lift()) +} + +/// A socket address as Python's `socket` module writes one. +fn socket_out(addr: std::net::SocketAddr) -> R { let text = |ip: &dyn std::fmt::Display| { let text = ip.to_string(); // SAFETY: the GIL is held. unsafe { Obj::new(PyUnicode_FromStringAndSize(text.as_ptr().cast(), text.len() as Py_ssize_t)) } }; - match addr.lift("python") { + match addr { std::net::SocketAddr::V4(v4) => tuple(vec![text(v4.ip()), uint_out(v4.port().into())]), std::net::SocketAddr::V6(v6) => tuple(vec![ text(v6.ip()), @@ -1232,7 +1266,28 @@ static SCOPE_ID: Key = Key::new(c"scope_id"); /// An IP address, from an `ipaddress.IPv4Address` or an `IPv6Address` without a scope, /// which Rust's `IpAddr` has no room for. -pub fn ip_in(obj: *mut PyObject, family: Family) -> R { +pub fn ip_in(obj: *mut PyObject) -> R { + address_in(obj, Family::Any).map(rt::IpAddr::lower) +} + +/// An IPv4 address, from an `ipaddress.IPv4Address`. +pub fn ipv4_in(obj: *mut PyObject) -> R { + match address_in(obj, Family::V4)? { + std::net::IpAddr::V4(ip) => Ok(rt::Ipv4Addr::lower(ip)), + std::net::IpAddr::V6(_) => unreachable!("checked IPv4"), + } +} + +/// An IPv6 address, from an `ipaddress.IPv6Address` without a scope. +pub fn ipv6_in(obj: *mut PyObject) -> R { + match address_in(obj, Family::V6)? { + std::net::IpAddr::V6(ip) => Ok(rt::Ipv6Addr::lower(ip)), + std::net::IpAddr::V4(_) => unreachable!("checked IPv6"), + } +} + +/// An IP address of `family`, as [`ip_in`] takes one. +fn address_in(obj: *mut PyObject, family: Family) -> R { let v6 = match (is(obj, T_IPV4), is(obj, T_IPV6)) { (true, _) if family == Family::V6 => return value_error("an IPv4 address has no Rust Ipv6Addr"), (_, true) if family == Family::V4 => return value_error("an IPv6 address has no Rust Ipv4Addr"), @@ -1247,20 +1302,27 @@ pub fn ip_in(obj: *mut PyObject, family: Family) -> R { return value_error("an IPv6 address with a scope has no Rust IpAddr"); } let packed = attr(obj, &PACKED, &mut s)?; - let ip = match v6 { - true => array_in::<16>(packed)?, - false => { - let mut ip = [0; 16]; - ip[..4].copy_from_slice(&array_in::<4>(packed)?); - ip - } - }; - Ok(rt::IpAddr { ip, v6 }) + Ok(match v6 { + true => std::net::Ipv6Addr::from(array_in::<16>(packed)?).into(), + false => std::net::Ipv4Addr::from(array_in::<4>(packed)?).into(), + }) } -/// An `ipaddress.IPv4Address` or `IPv6Address`. pub fn ip_out(ip: rt::IpAddr) -> R { - let (class, packed) = match ip.lift("python") { + address_out(ip.lift()) +} + +pub fn ipv4_out(ip: rt::Ipv4Addr) -> R { + address_out(ip.lift().into()) +} + +pub fn ipv6_out(ip: rt::Ipv6Addr) -> R { + address_out(ip.lift().into()) +} + +/// An `ipaddress.IPv4Address` or `IPv6Address`. +fn address_out(ip: std::net::IpAddr) -> R { + let (class, packed) = match ip { std::net::IpAddr::V4(v4) => (T_IPV4, v4.octets().to_vec()), std::net::IpAddr::V6(v6) => (T_IPV6, v6.octets().to_vec()), }; diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 0b12fd6ed..75094d8c1 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -3,35 +3,455 @@ // text across the C ABI, and the port that wakes, dropped wakers, and calls into Dart from // other threads reach the isolate through. -/// An IP socket address, as Rust's `SocketAddr` holds it. -final class SocketAddr { - /// Its IP, IPv4 or IPv6. - final io.InternetAddress address; +/// Rust's `u64` or `usize`: an `int`'s 64 bits read unsigned, so it holds every value +/// exactly, at no cost over the `int`. Its operators compare unsigned, but as an extension +/// type it has the `int`'s own `toString`, signed: print [toBigInt] or [toRadixString]. +extension type const U64.fromBits(int bits) { + /// `value`, throwing a [RangeError] if negative. + U64(int value) : bits = value { + if (value < 0) throw RangeError.range(value, 0, null, 'value'); + } + + /// `value`, throwing a [RangeError] outside `u64`'s range. + factory U64.fromBigInt(BigInt value) { + if (value.isNegative || value.bitLength > 64) { + throw RangeError("$value is out of range for Rust's u64"); + } + return U64.fromBits(value.toSigned(64).toInt()); + } + + /// `u64::MAX`. + static const max = U64.fromBits(-1); + + /// As an `int`, throwing a [RangeError] past its largest. + int toInt() => bits >= 0 ? bits : throw RangeError("${toBigInt()} is past int's largest"); + + BigInt toBigInt() => bits >= 0 ? BigInt.from(bits) : BigInt.from(bits).toUnsigned(64); + + /// Its digits in `radix`, unsigned. + String toRadixString(int radix) { + RangeError.checkValueInInterval(radix, 2, 36, 'radix'); + if (bits >= 0) return bits.toRadixString(radix); + // Halved to fit `int`, so the quotient is at most one short. + var q = ((bits >>> 1) ~/ radix) << 1; + var r = bits - q * radix; + if (r >= radix) { + q++; + r -= radix; + } + return '${q.toRadixString(radix)}${r.toRadixString(radix)}'; + } + + int compareTo(U64 other) => (bits ^ _sign).compareTo(other.bits ^ _sign); + + bool operator <(U64 other) => (bits ^ _sign) < (other.bits ^ _sign); + bool operator <=(U64 other) => (bits ^ _sign) <= (other.bits ^ _sign); + bool operator >(U64 other) => (bits ^ _sign) > (other.bits ^ _sign); + bool operator >=(U64 other) => (bits ^ _sign) >= (other.bits ^ _sign); + + /// The sign bit, flipped to compare the bits unsigned as signed. + static const _sign = -0x8000000000000000; +} + +/// Rust's `Duration`: whole seconds, a `u64`, and the nanoseconds below one, so it holds +/// every value exactly, which a [Duration], of microseconds within `int`'s range, does not. +final class RustDuration implements Comparable { + final U64 secs; + + /// From 0 through 999999999. + final int nanos; + + /// `secs` and `nanos`, throwing a [RangeError] unless `nanos` is below a second. + RustDuration(this.secs, [this.nanos = 0]) { + RangeError.checkValueInInterval(nanos, 0, 999999999, 'nanos'); + } + + const RustDuration._(this.secs, this.nanos); + + static const zero = RustDuration._(U64.fromBits(0), 0); + + /// `duration`, throwing an [ArgumentError] if negative. + factory RustDuration.from(Duration duration) { + final us = duration.inMicroseconds; + if (us < 0) throw ArgumentError.value(duration, 'duration', 'is negative'); + return RustDuration._(U64.fromBits(us ~/ 1000000), us % 1000000 * 1000); + } + + /// As a [Duration], throwing a [RangeError] where it holds part of a microsecond or is + /// past the longest, 9223372036854.775807 seconds. + Duration toDuration() { + final s = secs.bits; + if (nanos % 1000 != 0 || s < 0 || s > 9223372036854 || s == 9223372036854 && nanos > 775807000) { + throw RangeError('$this is no Duration'); + } + return Duration(microseconds: s * 1000000 + nanos ~/ 1000); + } + + @override + bool operator ==(Object other) => other is RustDuration && other.secs == secs && other.nanos == nanos; + + @override + int get hashCode => Object.hash(secs.bits, nanos); + + @override + int compareTo(RustDuration other) { + final bySecs = secs.compareTo(other.secs); + return bySecs != 0 ? bySecs : nanos.compareTo(other.nanos); + } + + @override + String toString() => '${secs.toRadixString(10)}.${nanos.toString().padLeft(9, '0')}s'; + + void _put(_Duration c) { + c.secs = secs.bits; + c.nanos = nanos; + } + + static RustDuration _of(_Duration c) => RustDuration._(U64.fromBits(c.secs), c.nanos); +} + +/// Rust's `PathBuf`: the path's bytes as the OS holds them, which on Unix may be any, so +/// it holds every path exactly, which a [String] does not. On Windows they are WTF-8. +final class PathBuf { + final typed.Uint8List _bytes; + + /// `path`'s UTF-8, throwing an [ArgumentError] where it holds a lone surrogate. + PathBuf(String path) : _bytes = _utf8(path); + + /// The path of the OS's [bytes], copied, throwing an [ArgumentError] where one is not a + /// byte or, on Windows, they are not WTF-8. + PathBuf.fromBytes(List bytes) : _bytes = _osPath(bytes); + + PathBuf._(this._bytes); + + /// Its bytes, which never change. + typed.Uint8List get bytes => _bytes.asUnmodifiableView(); + + /// As a [String], throwing a [FormatException] where it is not UTF-8. + String toFilePath() => convert.utf8.decode(_bytes); + + @override + bool operator ==(Object other) { + if (other is! PathBuf || other._bytes.length != _bytes.length) return false; + for (var i = 0; i < _bytes.length; i++) { + if (other._bytes[i] != _bytes[i]) return false; + } + return true; + } + + @override + int get hashCode => Object.hashAll(_bytes); + + /// Its text, as Rust's `Path::display` shows it: what is not UTF-8 is replaced. + @override + String toString() => convert.utf8.decode(_bytes, allowMalformed: true); +} + +/// Rust's `IpAddr`: an [Ipv4Addr] or an [Ipv6Addr]. +sealed class IpAddr { + const IpAddr._(); + + /// `address`'s IP, throwing an [ArgumentError] for a Unix address, or an IPv6 one with a + /// scope, which Rust's has none of. + factory IpAddr.from(io.InternetAddress address) => switch (address.type) { + io.InternetAddressType.IPv4 => Ipv4Addr.from(address), + io.InternetAddressType.IPv6 => Ipv6Addr.from(address), + _ => throw ArgumentError.value(address, 'address', 'is no IP address'), + }; + + /// Its bytes in network order: 4 of IPv4, or 16 of IPv6. + typed.Uint8List get octets; + + io.InternetAddress toInternetAddress() => io.InternetAddress.fromRawAddress(octets); + + @override + String toString() => toInternetAddress().address; + + static void _put(_IpAddr c, IpAddr v) { + switch (v) { + case Ipv4Addr(): + c.v6 = false; + v._put(c.ip.v4); + case Ipv6Addr(): + c.v6 = true; + v._put(c.ip.v6); + } + } + + static IpAddr _of(_IpAddr c) => c.v6 ? Ipv6Addr._of(c.ip.v6) : Ipv4Addr._of(c.ip.v4); +} + +/// The octets of `list`, which must be `n`, each from 0 through 255. +typed.Uint8List _ipOctets(List list, int n) { + if (list.length != n || list.any((b) => b < 0 || b > 255)) { + throw ArgumentError.value(list, 'octets', 'are not $n bytes'); + } + return typed.Uint8List.fromList(list); +} + +/// The IP of `address` of the family `type`, throwing an [ArgumentError] for another. +typed.Uint8List _ipRaw(io.InternetAddress address, io.InternetAddressType type) { + if (address.type != type) throw ArgumentError.value(address, 'address', 'is no $type address'); + // A scope shows only in its text. + if (address.address.contains('%')) { + throw ArgumentError.value(address, 'address', "has a scope, which Rust's IPv6 address has none of"); + } + return address.rawAddress; +} + +/// Rust's `Ipv4Addr`. +final class Ipv4Addr extends IpAddr { + /// Its octets, the first highest. + final int _bits; + + const Ipv4Addr._(this._bits) : super._(); + + /// The address of `octets`, throwing an [ArgumentError] unless they are 4 bytes. + factory Ipv4Addr.fromOctets(List octets) => Ipv4Addr._(_ipWord(_ipOctets(octets, 4), 0, 4)); + + /// `address`, throwing an [ArgumentError] unless it is IPv4. + factory Ipv4Addr.from(io.InternetAddress address) => + Ipv4Addr._(_ipWord(_ipRaw(address, io.InternetAddressType.IPv4), 0, 4)); + + @override + typed.Uint8List get octets => typed.Uint8List(4)..buffer.asByteData().setUint32(0, _bits); + + @override + bool operator ==(Object other) => other is Ipv4Addr && other._bits == _bits; + + @override + int get hashCode => _bits.hashCode; + + void _put(_Ipv4Addr c) { + final o = c.octets; + for (var i = 0; i < 4; i++) { + o[i] = _bits >>> (24 - 8 * i) & 0xff; + } + } + + static Ipv4Addr _of(_Ipv4Addr c) { + final o = c.octets; + return Ipv4Addr._(o[0] << 24 | o[1] << 16 | o[2] << 8 | o[3]); + } +} + +/// The big-endian word of `bytes`' `n` from `at`. +int _ipWord(typed.Uint8List bytes, int at, int n) { + var word = 0; + for (var i = at; i < at + n; i++) { + word = word << 8 | bytes[i]; + } + return word; +} + +/// Rust's `Ipv6Addr`. +final class Ipv6Addr extends IpAddr { + /// Its first 8 octets and its last, each word's first highest. + final int _hi, _lo; + + const Ipv6Addr._(this._hi, this._lo) : super._(); + + factory Ipv6Addr._bytes(typed.Uint8List bytes) => Ipv6Addr._(_ipWord(bytes, 0, 8), _ipWord(bytes, 8, 8)); + + /// The address of `octets`, throwing an [ArgumentError] unless they are 16 bytes. + factory Ipv6Addr.fromOctets(List octets) => Ipv6Addr._bytes(_ipOctets(octets, 16)); + + /// `address`, throwing an [ArgumentError] unless it is IPv6 with no scope. + factory Ipv6Addr.from(io.InternetAddress address) => Ipv6Addr._bytes(_ipRaw(address, io.InternetAddressType.IPv6)); + + @override + typed.Uint8List get octets => typed.Uint8List(16) + ..buffer.asByteData().setUint64(0, _hi) + ..buffer.asByteData().setUint64(8, _lo); + + @override + bool operator ==(Object other) => other is Ipv6Addr && other._hi == _hi && other._lo == _lo; + + @override + int get hashCode => Object.hash(_hi, _lo); + + void _put(_Ipv6Addr c) { + final o = c.octets; + for (var i = 0; i < 8; i++) { + o[i] = _hi >>> (56 - 8 * i) & 0xff; + o[i + 8] = _lo >>> (56 - 8 * i) & 0xff; + } + } + + static Ipv6Addr _of(_Ipv6Addr c) { + final o = c.octets; + var hi = 0, lo = 0; + for (var i = 0; i < 8; i++) { + hi = hi << 8 | o[i]; + lo = lo << 8 | o[i + 8]; + } + return Ipv6Addr._(hi, lo); + } +} + +/// Rust's `SocketAddr`: a [SocketAddrV4] or a [SocketAddrV6]. +sealed class SocketAddr { + const SocketAddr._(); + + /// `ip` at `port`, which IPv6 has with no flow label or scope, throwing a [RangeError] + /// for a port past 65535. + factory SocketAddr(IpAddr ip, int port) => switch (ip) { + Ipv4Addr() => SocketAddrV4(ip, port), + Ipv6Addr() => SocketAddrV6(ip, port), + }; + + IpAddr get ip; + int get port; + + static void _put(_SocketAddr c, SocketAddr v) { + switch (v) { + case SocketAddrV4(): + c.v6 = false; + v._put(c.addr.v4); + case SocketAddrV6(): + c.v6 = true; + v._put(c.addr.v6); + } + } + + static SocketAddr _of(_SocketAddr c) => c.v6 ? SocketAddrV6._of(c.addr.v6) : SocketAddrV4._of(c.addr.v4); +} + +/// Rust's `SocketAddrV4`. +final class SocketAddrV4 extends SocketAddr { + @override + final Ipv4Addr ip; + @override final int port; - /// IPv6's flow label, zero for IPv4. - final int flowinfo; + /// `ip` at `port`, throwing a [RangeError] for a port past 65535. + SocketAddrV4(this.ip, this.port) : super._() { + RangeError.checkValueInInterval(port, 0, 0xffff, 'port'); + } + + const SocketAddrV4._(this.ip, this.port) : super._(); - /// IPv6's scope, zero for IPv4. + @override + bool operator ==(Object other) => other is SocketAddrV4 && other.ip == ip && other.port == port; + + @override + int get hashCode => Object.hash(ip, port); + + @override + String toString() => '$ip:$port'; + + void _put(_SocketAddrV4 c) { + ip._put(c.ip); + c.port = port; + } + + static SocketAddrV4 _of(_SocketAddrV4 c) => SocketAddrV4._(Ipv4Addr._of(c.ip), c.port); +} + +/// Rust's `SocketAddrV6`. +final class SocketAddrV6 extends SocketAddr { + @override + final Ipv6Addr ip; + @override + final int port; + + /// Its flow label. + final int flowinfo; final int scopeId; - const SocketAddr(this.address, this.port, {this.flowinfo = 0, this.scopeId = 0}); + /// `ip` at `port`, throwing a [RangeError] for a port past 65535, or a flow label or + /// scope past `u32`'s range. + SocketAddrV6(this.ip, this.port, {this.flowinfo = 0, this.scopeId = 0}) : super._() { + RangeError.checkValueInInterval(port, 0, 0xffff, 'port'); + RangeError.checkValueInInterval(flowinfo, 0, 0xffffffff, 'flowinfo'); + RangeError.checkValueInInterval(scopeId, 0, 0xffffffff, 'scopeId'); + } + + const SocketAddrV6._(this.ip, this.port, this.flowinfo, this.scopeId) : super._(); @override bool operator ==(Object other) => - other is SocketAddr && - other.address == address && + other is SocketAddrV6 && + other.ip == ip && other.port == port && other.flowinfo == flowinfo && other.scopeId == scopeId; @override - int get hashCode => Object.hash(address, port, flowinfo, scopeId); + int get hashCode => Object.hash(ip, port, flowinfo, scopeId); @override - String toString() => address.type == io.InternetAddressType.IPv6 - ? '[${address.address}]:$port' - : '${address.address}:$port'; + String toString() => scopeId == 0 ? '[$ip]:$port' : '[$ip%$scopeId]:$port'; + + void _put(_SocketAddrV6 c) { + ip._put(c.ip); + c.port = port; + c.flowinfo = flowinfo; + c.scope_id = scopeId; + } + + static SocketAddrV6 _of(_SocketAddrV6 c) => SocketAddrV6._(Ipv6Addr._of(c.ip), c.port, c.flowinfo, c.scope_id); +} + +/// A copy of `bytes`, throwing an [ArgumentError] where one is not a byte or, on Windows, +/// they are not WTF-8. +typed.Uint8List _osPath(List bytes) { + if (bytes is! typed.Uint8List && bytes.any((b) => b < 0 || b > 255)) { + throw ArgumentError.value(bytes, 'bytes', 'are not all bytes'); + } + final copy = typed.Uint8List.fromList(bytes); + if (io.Platform.isWindows && !_wtf8(copy)) { + throw ArgumentError.value(bytes, 'bytes', 'is not WTF-8, so names no Windows path'); + } + return copy; +} + +/// Whether `bytes` are WTF-8, as Rust encodes a Windows path: UTF-8, but for a surrogate +/// alone, encoded as a scalar would be, though never a lead then a trail. +bool _wtf8(typed.Uint8List bytes) { + var at = 0; + var lead = false; + while (at < bytes.length) { + final b = bytes[at]; + final (n, min) = switch (b) { + < 0x80 => (1, 0), + >= 0xc2 && <= 0xdf => (2, 0x80), + >= 0xe0 && <= 0xef => (3, 0x800), + >= 0xf0 && <= 0xf4 => (4, 0x10000), + _ => (0, 0), + }; + if (n == 0) return false; + if (at + n > bytes.length) return false; + var c = n == 1 ? b : b & (0x7f >> n); + for (var k = 1; k < n; k++) { + final next = bytes[at + k]; + if (next & 0xc0 != 0x80) return false; + c = (c << 6) | (next & 0x3f); + } + if (c < min || c > 0x10ffff) return false; + final surrogate = c >= 0xd800 && c <= 0xdfff; + if (surrogate && c >= 0xdc00 && lead) return false; + lead = surrogate && c < 0xdc00; + at += n; + } + return true; +} + +/// `text`'s UTF-8, throwing an [ArgumentError] where it holds a lone surrogate, which no +/// Rust string does: Dart's encoder would replace it. +typed.Uint8List _utf8(String text) { + final n = text.length; + for (var i = 0; i < n; i++) { + final unit = text.codeUnitAt(i); + if (unit & 0xf800 != 0xd800) continue; + // A lead followed by a trail is one scalar. + if (unit < 0xdc00 && i + 1 < n && text.codeUnitAt(i + 1) & 0xfc00 == 0xdc00) { + i++; + continue; + } + throw ArgumentError.value(text, 'text', 'holds a lone surrogate at $i, which no Rust string does'); + } + return convert.utf8.encode(text); } /// A value a call reads and writes back, as Rust's `&mut` of a value: the call leaves @@ -188,24 +608,52 @@ final class _Duration extends ffi.Struct { external int nanos; } -final class _Addr extends ffi.Struct { +final class _Ipv4Addr extends ffi.Struct { + @ffi.Array(4) + external ffi.Array octets; +} + +final class _Ipv6Addr extends ffi.Struct { @ffi.Array(16) - external ffi.Array ip; - @ffi.Uint16() - external int port; + external ffi.Array octets; +} + +final class _Ip extends ffi.Union { + external _Ipv4Addr v4; + external _Ipv6Addr v6; +} + +final class _IpAddr extends ffi.Struct { @ffi.Bool() external bool v6; + external _Ip ip; +} + +final class _SocketAddrV4 extends ffi.Struct { + external _Ipv4Addr ip; + @ffi.Uint16() + external int port; +} + +final class _SocketAddrV6 extends ffi.Struct { + external _Ipv6Addr ip; + @ffi.Uint16() + external int port; @ffi.Uint32() external int flowinfo; @ffi.Uint32() external int scope_id; } -final class _IpAddr extends ffi.Struct { - @ffi.Array(16) - external ffi.Array ip; +final class _Socket extends ffi.Union { + external _SocketAddrV4 v4; + external _SocketAddrV6 v6; +} + +final class _SocketAddr extends ffi.Struct { @ffi.Bool() external bool v6; + external _Socket addr; } final class _Waker extends ffi.Struct { @@ -243,8 +691,8 @@ abstract interface class SeekableReader { int readInto(typed.Uint8List buffer); /// Move to `offset` from `from`, returning the position from the start. From - /// [SeekFrom.start], `offset` and the position are a `u64`'s 64 bits, as every `u64` in - /// Dart is, so one past `i64`'s range is negative. + /// [SeekFrom.start], `offset` and the position are a `u64`'s 64 bits, as [U64.bits] holds + /// them, so one past `i64`'s range is negative. int seek(int offset, SeekFrom from); } @@ -293,7 +741,7 @@ bool _readerRead( n.value = got; return true; } on Exception catch (e) { - _n.setBuf(err, _n.copied(e.toString())); + _n.setBuf(err, _n.copied(e.toString(), message: true)); return false; } catch (e, s) { _n.abort("a reader's read", e, s); @@ -312,7 +760,7 @@ bool _readerSeek( position.value = (_n.lent[data.address] as SeekableReader).seek(offset, SeekFrom.values[whence]); return true; } on Exception catch (e) { - _n.setBuf(err, _n.copied(e.toString())); + _n.setBuf(err, _n.copied(e.toString(), message: true)); return false; } catch (e, s) { _n.abort("a reader's seek", e, s); @@ -580,7 +1028,7 @@ final class _Arena implements ffi.Allocator { } } if (!ascii) { - bytes(dst, convert.utf8.encode(text), take); + bytes(dst, _utf8(text), take); return; } if (take && n >= _takeFrom) { @@ -816,12 +1264,16 @@ final class _Native { ..data = a.lend(reader, _readers); /// `text` as a buffer Rust owns, as `buf_copy` makes it: what a closure or a reader - /// hands Rust. - _Buf copied(String text) { + /// hands Rust. A lone surrogate throws, or in a `message` is replaced. + _Buf copied(String text, {bool message = false}) { final a = _Arena(); try { final view = a<_Buf>(); - a.string(view.ref, text, false); + if (message) { + a.bytes(view.ref, convert.utf8.encode(text), false); + } else { + a.string(view.ref, text, false); + } return _bufCopy(view.ref); } finally { a.end(); @@ -841,7 +1293,7 @@ final class _Native { Never fail(String text) { final a = _Arena(); final buf = a<_Buf>(); - a.string(buf.ref, text, false); + a.bytes(buf.ref, convert.utf8.encode(text), false); _dartAbort(buf.ref); throw StateError('unreachable'); } @@ -883,30 +1335,6 @@ final class _Native { String? stringLentOrNull(_Buf buf) => buf.ptr == ffi.nullptr ? null : stringLent(buf); - /// A path Rust returned, copied, then freed. - String path(_Buf buf) { - try { - return pathLent(buf); - } finally { - _bufFree(buf); - } - } - - String? pathOrNull(_Buf buf) => buf.ptr == ffi.nullptr ? null : path(buf); - - /// A path from Rust, copied, which on Unix may hold bytes no `String` holds: those - /// abort, since a throw would leave the rest of a list or record being lifted unfreed. - String pathLent(_Buf buf) { - if (buf.len == 0) return ''; - try { - return const convert.Utf8Decoder(allowMalformed: false).convert(buf.ptr.asTypedList(buf.len)); - } on FormatException { - fail('a path from Rust is not UTF-8'); - } - } - - String? pathLentOrNull(_Buf buf) => buf.ptr == ffi.nullptr ? null : pathLent(buf); - /// A `char`, one Unicode scalar. int char(String text) { final runes = text.runes; @@ -931,85 +1359,6 @@ final class _Native { BigInt u128Of(_U128 c) => BigInt.from(c.hi).toUnsigned(64) << 64 | BigInt.from(c.lo).toUnsigned(64); - /// Rust's `Duration::MAX`, and any longer than Dart's longest, is that. - static const _longest = Duration(microseconds: 0x7fffffffffffffff); - - void duration(_Duration dst, Duration value) { - if (value.isNegative) { - throw ArgumentError.value(value, 'duration', 'a negative Duration has no Rust Duration'); - } - if (value == _longest) { - dst.secs = -1; - dst.nanos = 999999999; - return; - } - final us = value.inMicroseconds; - dst.secs = us ~/ 1000000; - dst.nanos = (us % 1000000) * 1000; - } - - /// A Rust `Duration`, truncated to the microsecond. - Duration durationOf(_Duration c) { - final secs = c.secs; - // Past Dart's longest, 9223372036854.775807 seconds, read as `u64`: negative is past - // `i64`. - if (secs < 0 || secs > 9223372036854 || secs == 9223372036854 && c.nanos ~/ 1000 >= 775807) { - return _longest; - } - return Duration(microseconds: secs * 1000000 + c.nanos ~/ 1000); - } - - /// Writes `value`'s IP into `dst`, returning whether it is IPv6, for the Rust type `rust`, - /// which holds only IPv4 when it ends in `V4` or `v4Addr`, and only IPv6 in `V6` or `v6Addr`. - bool _ip(ffi.Array dst, io.InternetAddress value, String rust) { - final v6 = switch (value.type) { - io.InternetAddressType.IPv4 => false, - io.InternetAddressType.IPv6 => true, - _ => throw ArgumentError.value(value, 'address', 'a Unix address has no Rust $rust'), - }; - final v4Only = rust.endsWith('V4') || rust.endsWith('v4Addr'); - final v6Only = rust.endsWith('V6') || rust.endsWith('v6Addr'); - if (v4Only && v6 || v6Only && !v6) { - throw ArgumentError.value(value, 'address', 'an IP${v6 ? 'v6' : 'v4'} address has no Rust $rust'); - } - final ip = value.rawAddress; - for (var i = 0; i < 16; i++) { - dst[i] = i < ip.length ? ip[i] : 0; - } - return v6; - } - - io.InternetAddress _ipOf(ffi.Array src, bool v6) { - final n = v6 ? 16 : 4; - final ip = typed.Uint8List(n); - for (var i = 0; i < n; i++) { - ip[i] = src[i]; - } - return io.InternetAddress.fromRawAddress(ip); - } - - void ip(_IpAddr dst, io.InternetAddress value, String rust) { - dst.v6 = _ip(dst.ip, value, rust); - } - - io.InternetAddress ipOf(_IpAddr c) => _ipOf(c.ip, c.v6); - - void addr(_Addr dst, SocketAddr value, String rust) { - dst.v6 = _ip(dst.ip, value.address, rust); - dst.port = u16(value.port); - dst.flowinfo = u32(value.flowinfo); - dst.scope_id = u32(value.scopeId); - } - - SocketAddr addrOf(_Addr c) { - return SocketAddr( - _ipOf(c.ip, c.v6), - c.port, - flowinfo: c.flowinfo, - scopeId: c.scope_id, - ); - } - int u8(int v) => v >= 0 && v <= 0xff ? v : throw RangeError.range(v, 0, 0xff); int u16(int v) => v >= 0 && v <= 0xffff ? v : throw RangeError.range(v, 0, 0xffff); int u32(int v) => v >= 0 && v <= 0xffffffff ? v : throw RangeError.range(v, 0, 0xffffffff); @@ -1017,6 +1366,18 @@ final class _Native { int i16(int v) => v >= -0x8000 && v <= 0x7fff ? v : throw RangeError.range(v, -0x8000, 0x7fff); int i32(int v) => v >= -0x80000000 && v <= 0x7fffffff ? v : throw RangeError.range(v, -0x80000000, 0x7fffffff); + + /// A `NonZero`'s integer, which Rust's type holds only if it is not zero. + int nonZero(int v) => v != 0 ? v : throw ArgumentError.value(v, 'NonZero', 'a NonZero is zero'); + + /// Whether the library's target has 64-bit words, else 32-bit ones. + final bool _wide = ffi.sizeOf() == 8; + + /// A `usize`, which on a 32-bit target holds only 32 bits. + int usize(U64 v) => _wide || v.bits >>> 32 == 0 ? v.bits : throw RangeError("${v.toBigInt()} is past this target's usize"); + + /// An `isize`, which on a 32-bit target holds only 32 bits. + int isize(int v) => _wide ? v : i32(v); } /// What Rust calls on the isolate's thread once it drops an implementation. diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index 4b992d33f..1448cd7ee 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -784,3 +784,292 @@ func (p *pollable_) Close() error { } return nil } + +// ErrInvalid is what a checked conversion returns, wrapped with what failed, for a value one +// type holds and the other does not: text that is not UTF-8 or a rune that is not a Unicode +// scalar value, which a call taking one returns before it is made, or a value outside the +// type converted to. +var ErrInvalid = errors.New("invalid") + +type invalidError_ string + +func (e invalidError_) Error() string { + return string(e) +} + +func (e invalidError_) Is(target error) bool { + return target == ErrInvalid +} + +// Invalid_ is the error, matching ErrInvalid, that what says. For the generated packages. +func Invalid_(what string) error { + return invalidError_(what) +} + +// Duration is Rust's Duration: whole seconds, and the nanoseconds past them, below a +// second. The zero Duration is zero, and == compares two. +type Duration struct { + secs uint64 + nanos uint32 +} + +// NewDuration is secs seconds and nanos nanoseconds, nanoseconds past a second carrying into +// the seconds as Rust's Duration::new does, failing past Rust's longest. +func NewDuration(secs uint64, nanos uint32) (Duration, error) { + carry := uint64(nanos / 1e9) + if secs > math.MaxUint64-carry { + return Duration{}, Invalid_("a duration past Rust's longest") + } + return Duration{secs: secs + carry, nanos: nanos % 1e9}, nil +} + +// DurationOf is d as Rust's Duration, failing on a negative one, which Rust's has none of. +func DurationOf(d time.Duration) (Duration, error) { + if d < 0 { + return Duration{}, Invalid_("a negative duration has no Rust Duration") + } + return Duration{secs: uint64(d / time.Second), nanos: uint32(d % time.Second)}, nil +} + +// Secs is d's whole seconds. +func (d Duration) Secs() uint64 { + return d.secs +} + +// Nanos is d's nanoseconds past its whole seconds, below a second. +func (d Duration) Nanos() uint32 { + return d.nanos +} + +// Std is d as a time.Duration, failing past its longest, about 292 years. +func (d Duration) Std() (time.Duration, error) { + if d.secs > uint64((math.MaxInt64-time.Duration(d.nanos))/time.Second) { + return 0, Invalid_("a duration past time.Duration's longest") + } + return time.Duration(d.secs)*time.Second + time.Duration(d.nanos), nil +} + +// U128 is Rust's u128: its high and its low 64 bits. +type U128 struct { + Hi, Lo uint64 +} + +// U128Of is v as a U128, failing outside 0 through 2^128 - 1. +func U128Of(v *big.Int) (U128, error) { + if v.Sign() < 0 || v.BitLen() > 128 { + return U128{}, Invalid_("an int outside Rust's u128") + } + var b [16]byte + v.FillBytes(b[:]) + return U128{Hi: binary.BigEndian.Uint64(b[:8]), Lo: binary.BigEndian.Uint64(b[8:])}, nil +} + +// Big is v as a big.Int. +func (v U128) Big() *big.Int { + var b [16]byte + binary.BigEndian.PutUint64(b[:8], v.Hi) + binary.BigEndian.PutUint64(b[8:], v.Lo) + return new(big.Int).SetBytes(b[:]) +} + +// Ipv4Addr is Rust's Ipv4Addr: its octets, in network order. +type Ipv4Addr [4]byte + +// Ipv4AddrOf is ip as an Ipv4Addr, failing unless it is IPv4. +func Ipv4AddrOf(ip netip.Addr) (Ipv4Addr, error) { + if !ip.Is4() { + return Ipv4Addr{}, Invalid_("an address that is not IPv4 has no Rust Ipv4Addr") + } + return ip.As4(), nil +} + +// Netip is ip as a netip.Addr. +func (ip Ipv4Addr) Netip() netip.Addr { + return netip.AddrFrom4(ip) +} + +// Ipv6Addr is Rust's Ipv6Addr: its octets, in network order. +type Ipv6Addr [16]byte + +// Ipv6AddrOf is ip as an Ipv6Addr, failing unless it is IPv6 without a zone, which Rust's +// has no room for. +func Ipv6AddrOf(ip netip.Addr) (Ipv6Addr, error) { + if !ip.Is6() { + return Ipv6Addr{}, Invalid_("an address that is not IPv6 has no Rust Ipv6Addr") + } + if ip.Zone() != "" { + return Ipv6Addr{}, Invalid_("an IPv6 address with a zone has no Rust Ipv6Addr") + } + return ip.As16(), nil +} + +// Netip is ip as a netip.Addr, IPv6 even where it maps IPv4. +func (ip Ipv6Addr) Netip() netip.Addr { + return netip.AddrFrom16(ip) +} + +// IpAddr is Rust's IpAddr: an Ipv4Addr or an Ipv6Addr. The zero IpAddr is IPv4's 0.0.0.0, +// and == compares two. +type IpAddr struct { + v4 Ipv4Addr + v6 Ipv6Addr + is6 bool +} + +// IpAddrFromV4 is ip as an IpAddr. +func IpAddrFromV4(ip Ipv4Addr) IpAddr { + return IpAddr{v4: ip} +} + +// IpAddrFromV6 is ip as an IpAddr. +func IpAddrFromV6(ip Ipv6Addr) IpAddr { + return IpAddr{v6: ip, is6: true} +} + +// IpAddrOf is ip as an IpAddr, of its family, failing on an invalid one or a zone, which +// Rust's has no room for. +func IpAddrOf(ip netip.Addr) (IpAddr, error) { + if ip.Is4() { + return IpAddrFromV4(ip.As4()), nil + } + if !ip.IsValid() { + return IpAddr{}, Invalid_("an invalid address has no Rust IpAddr") + } + v6, err := Ipv6AddrOf(ip) + if err != nil { + return IpAddr{}, err + } + return IpAddrFromV6(v6), nil +} + +// V4 is a's IPv4 address, if it is one. +func (a IpAddr) V4() (Ipv4Addr, bool) { + return a.v4, !a.is6 +} + +// V6 is a's IPv6 address, if it is one. +func (a IpAddr) V6() (Ipv6Addr, bool) { + return a.v6, a.is6 +} + +// Netip is a as a netip.Addr. +func (a IpAddr) Netip() netip.Addr { + if a.is6 { + return a.v6.Netip() + } + return a.v4.Netip() +} + +// SocketAddrV4 is Rust's SocketAddrV4: an IPv4 address and a port. +type SocketAddrV4 struct { + IP Ipv4Addr + Port uint16 +} + +// SocketAddrV4Of is a as a SocketAddrV4, failing unless it is IPv4. +func SocketAddrV4Of(a netip.AddrPort) (SocketAddrV4, error) { + ip, err := Ipv4AddrOf(a.Addr()) + if err != nil { + return SocketAddrV4{}, Invalid_("an address that is not IPv4 has no Rust SocketAddrV4") + } + return SocketAddrV4{IP: ip, Port: a.Port()}, nil +} + +// Netip is a as a netip.AddrPort. +func (a SocketAddrV4) Netip() netip.AddrPort { + return netip.AddrPortFrom(a.IP.Netip(), a.Port) +} + +// SocketAddrV6 is Rust's SocketAddrV6: an IPv6 address, a port, a flow label, and the scope +// a link-local address is in, a zone's interface number, or 0 for none. +type SocketAddrV6 struct { + IP Ipv6Addr + Port uint16 + FlowInfo uint32 + ScopeID uint32 +} + +// SocketAddrV6Of is a as a SocketAddrV6, failing unless it is IPv6 with no zone or one naming +// an interface, by its number or its name, which it holds as that interface's number. +func SocketAddrV6Of(a netip.AddrPort) (SocketAddrV6, error) { + ip := a.Addr() + if !ip.Is6() { + return SocketAddrV6{}, Invalid_("an address that is not IPv6 has no Rust SocketAddrV6") + } + v := SocketAddrV6{IP: ip.As16(), Port: a.Port()} + if zone := ip.Zone(); zone != "" { + if id, err := strconv.ParseUint(zone, 10, 32); err == nil { + v.ScopeID = uint32(id) + } else if iface, err := net.InterfaceByName(zone); err == nil { + v.ScopeID = uint32(iface.Index) + } else { + return SocketAddrV6{}, Invalid_("an IPv6 zone that names no interface: " + zone) + } + } + return v, nil +} + +// Netip is a as a netip.AddrPort, its scope a zone of its number, failing on a flow label, +// which netip has no room for. +func (a SocketAddrV6) Netip() (netip.AddrPort, error) { + if a.FlowInfo != 0 { + return netip.AddrPort{}, Invalid_("an IPv6 flow label has no netip form") + } + ip := a.IP.Netip() + if a.ScopeID != 0 { + ip = ip.WithZone(strconv.FormatUint(uint64(a.ScopeID), 10)) + } + return netip.AddrPortFrom(ip, a.Port), nil +} + +// SocketAddr is Rust's SocketAddr: a SocketAddrV4 or a SocketAddrV6. The zero SocketAddr is +// IPv4's 0.0.0.0, port 0, and == compares two. +type SocketAddr struct { + v4 SocketAddrV4 + v6 SocketAddrV6 + is6 bool +} + +// SocketAddrFromV4 is a as a SocketAddr. +func SocketAddrFromV4(a SocketAddrV4) SocketAddr { + return SocketAddr{v4: a} +} + +// SocketAddrFromV6 is a as a SocketAddr. +func SocketAddrFromV6(a SocketAddrV6) SocketAddr { + return SocketAddr{v6: a, is6: true} +} + +// SocketAddrOf is a as a SocketAddr, of its family, failing on an invalid one or a zone +// SocketAddrV6Of fails on. +func SocketAddrOf(a netip.AddrPort) (SocketAddr, error) { + if a.Addr().Is4() { + return SocketAddrFromV4(SocketAddrV4{IP: a.Addr().As4(), Port: a.Port()}), nil + } + if !a.Addr().IsValid() { + return SocketAddr{}, Invalid_("an invalid address has no Rust SocketAddr") + } + v6, err := SocketAddrV6Of(a) + if err != nil { + return SocketAddr{}, err + } + return SocketAddrFromV6(v6), nil +} + +// V4 is a's IPv4 socket address, if it is one. +func (a SocketAddr) V4() (SocketAddrV4, bool) { + return a.v4, !a.is6 +} + +// V6 is a's IPv6 socket address, if it is one. +func (a SocketAddr) V6() (SocketAddrV6, bool) { + return a.v6, a.is6 +} + +// Netip is a as a netip.AddrPort, failing as SocketAddrV6's does. +func (a SocketAddr) Netip() (netip.AddrPort, error) { + if a.is6 { + return a.v6.Netip() + } + return a.v4.Netip(), nil +} diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index c69441986..1632505ed 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -121,15 +121,30 @@ class __Writer { b.putInt(v.size) } - /** Text as [TEXT] and its place in [refs], which Rust encodes once from, or -1 for none. */ + /** + * Text as [TEXT], added to [refs], or -1 for none: Rust encodes each in [refs] once, + * before it reads any of the value, so a lone surrogate throws before anything moves. + */ fun string(v: String?) { if (v == null) { i32(-1) return } - room(8) - b.putInt(TEXT) - b.putInt(ref(v)) + i32(TEXT) + ref(v) + } + + /** A path's bytes as [bytes] encodes Kotlin's, or -1 for none. */ + fun path(v: PathBuf?) { + if (v == null) { + i32(-1) + return + } + room(16) + b.putInt(KOTLIN) + b.putInt(ref(v.bytes)) + b.putInt(0) + b.putInt(v.bytes.size) } private var objects: java.util.ArrayList? = null @@ -144,66 +159,60 @@ class __Writer { /** The arrays and text the encoding refers to, which Rust reads in the call, or null for none. */ fun refs(): kotlin.Array? = objects?.toArray() - /** An address as Rust's `SocketAddr` holds it, which an unresolved one has no form of. */ - fun socketAddr(v: java.net.InetSocketAddress) { - val ip = kotlin.requireNotNull(v.address) { "an unresolved address has no Rust SocketAddr" } - room(16) - b.put(ip.address) - repeat(16 - ip.address.size) { b.put(0) } - i16(v.port.toShort()) - bool(ip is java.net.Inet6Address) - // Java keeps no flow label. - i32(0) - i32((ip as? java.net.Inet6Address)?.scopeId ?: 0) + /** Whether it is IPv6, then the address of its family. */ + fun socketAddr(v: SocketAddr) = when (v) { + is SocketAddrV4 -> { + bool(false) + socketAddrV4(v) + } + is SocketAddrV6 -> { + bool(true) + socketAddrV6(v) + } } - /** An IPv4 address, for Rust's `SocketAddrV4`. */ - fun socketAddrV4(v: java.net.InetSocketAddress) { - kotlin.require(v.address !is java.net.Inet6Address) { "an IPv6 address has no Rust SocketAddrV4" } - socketAddr(v) + fun socketAddrV4(v: SocketAddrV4) { + ipv4Addr(v.ip) + i16(v.port.toShort()) } - /** An IPv6 address, for Rust's `SocketAddrV6`. */ - fun socketAddrV6(v: java.net.InetSocketAddress) { - kotlin.require(v.address !is java.net.Inet4Address) { "an IPv4 address has no Rust SocketAddrV6" } - socketAddr(v) + fun socketAddrV6(v: SocketAddrV6) { + ipv6Addr(v.ip) + i16(v.port.toShort()) + i32(v.flowinfo.toInt()) + i32(v.scopeId.toInt()) } - /** An address as Rust's `IpAddr` holds it, which has no IPv6 scope. */ - fun ipAddr(v: java.net.InetAddress) { - val scope = (v as? java.net.Inet6Address)?.scopeId ?: 0 - kotlin.require(scope == 0) { "an IPv6 address with a scope has no Rust IpAddr" } - room(16) - b.put(v.address) - repeat(16 - v.address.size) { b.put(0) } - bool(v is java.net.Inet6Address) + /** Whether it is IPv6, then the address of its family. */ + fun ipAddr(v: IpAddr) = when (v) { + is Ipv4Addr -> { + bool(false) + ipv4Addr(v) + } + is Ipv6Addr -> { + bool(true) + ipv6Addr(v) + } } - /** An IPv4 address, for Rust's `Ipv4Addr`. */ - fun ipAddrV4(v: java.net.InetAddress) { - kotlin.require(v !is java.net.Inet6Address) { "an IPv6 address has no Rust Ipv4Addr" } - ipAddr(v) - } + /** Its octets, the first highest. */ + fun ipv4Addr(v: Ipv4Addr) = i32(java.lang.Integer.reverseBytes(v.bits.toInt())) - /** An IPv6 address, for Rust's `Ipv6Addr`. */ - fun ipAddrV6(v: java.net.InetAddress) { - kotlin.require(v !is java.net.Inet4Address) { "an IPv4 address has no Rust Ipv6Addr" } - ipAddr(v) + fun ipv6Addr(v: Ipv6Addr) { + i64(java.lang.Long.reverseBytes(v.hi)) + i64(java.lang.Long.reverseBytes(v.lo)) } /** A `u128`, from 0 through 2^128 - 1. */ fun u128(v: java.math.BigInteger) { - kotlin.require(v.signum() >= 0 && v.bitLength() <= 128) { "$v is out of range for Rust's u128" } + if (v.signum() < 0 || v.bitLength() > 128) throw __Invalid("$v is out of range for Rust's u128") i64(v.toLong()) i64(v.shiftRight(64).toLong()) } - fun duration(v: kotlin.time.Duration) { - kotlin.require(!v.isNegative()) { "a negative duration has no Rust Duration" } - v.toComponents { secs, nanos -> - i64(secs) - i32(nanos) - } + fun duration(v: Duration) { + i64(v.secs.toLong()) + i32(v.nanos.toInt()) } private var held: java.util.ArrayList<__Handle>? = null @@ -228,7 +237,22 @@ class __Writer { held.add(v) } - fun finish(): ByteArray = b.array().copyOf(b.position()) + /** + * The encoding, then where [refs] names any, the place in it of each text, in order, and + * how many, which Rust reads first. + */ + fun finish(): ByteArray { + val objects = objects ?: return b.array().copyOf(b.position()) + var texts = 0 + for (at in objects.indices) { + if (objects[at] is String) { + i32(at) + texts++ + } + } + i32(texts) + return b.array().copyOf(b.position()) + } /** Release the handles encoded, once the call that reads them returns. */ fun release() { @@ -325,32 +349,23 @@ class __Reader(private val b: java.nio.ByteBuffer) { return v } - /** A path, which on Unix may hold bytes no String holds: those throw. */ - fun path(): String? = bytes()?.decodeToString(throwOnInvalidSequence = true) + /** A path, as Rust's bytes of it, or null for none. */ + fun path(): PathBuf? = bytes()?.let { PathBuf.of(it) } - fun socketAddr(): java.net.InetSocketAddress { - val ip = ByteArray(16) - b.get(ip) - val port = b.getShort().toInt() and 0xffff - val v6 = bool() - val flowinfo = b.getInt() - val scope = b.getInt() - kotlin.check(flowinfo == 0) { "an IPv6 flow label has no Java form" } - return java.net.InetSocketAddress(address(ip, v6, scope), port) - } + /** Whether it is IPv6, then the address of its family. */ + fun socketAddr(): SocketAddr = if (bool()) socketAddrV6() else socketAddrV4() - fun ipAddr(): java.net.InetAddress { - val ip = ByteArray(16) - b.get(ip) - return address(ip, bool(), 0) - } + fun socketAddrV4(): SocketAddrV4 = SocketAddrV4(ipv4Addr(), b.getShort().toUShort()) - // Built as Inet6Address directly, as InetAddress would read an IPv4-mapped one as IPv4. - private fun address(ip: ByteArray, v6: Boolean, scope: Int): java.net.InetAddress = when { - !v6 -> java.net.InetAddress.getByAddress(ip.copyOf(4)) - scope == 0 -> java.net.Inet6Address.getByAddress(null, ip, null as java.net.NetworkInterface?) - else -> java.net.Inet6Address.getByAddress(null, ip, scope) - } + fun socketAddrV6(): SocketAddrV6 = SocketAddrV6(ipv6Addr(), b.getShort().toUShort(), b.getInt().toUInt(), b.getInt().toUInt()) + + /** Whether it is IPv6, then the address of its family. */ + fun ipAddr(): IpAddr = if (bool()) ipv6Addr() else ipv4Addr() + + /** Its octets, the first highest. */ + fun ipv4Addr(): Ipv4Addr = Ipv4Addr(java.lang.Integer.reverseBytes(b.getInt()).toUInt()) + + fun ipv6Addr(): Ipv6Addr = Ipv6Addr(java.lang.Long.reverseBytes(b.getLong()), java.lang.Long.reverseBytes(b.getLong())) fun u128(): java.math.BigInteger { val lo = b.getLong() @@ -358,13 +373,38 @@ class __Reader(private val b: java.nio.ByteBuffer) { return java.math.BigInteger(1, java.nio.ByteBuffer.allocate(16).putLong(hi).putLong(lo).array()) } - fun duration(): kotlin.time.Duration { - val secs = b.getLong() - val nanos = b.getInt() - // Past Long.MAX_VALUE seconds, as no Kotlin Duration but its infinite one holds. - if (secs < 0) return kotlin.time.Duration.INFINITE - return secs.toDuration(kotlin.time.DurationUnit.SECONDS) + nanos.toDuration(kotlin.time.DurationUnit.NANOSECONDS) - } + fun duration(): Duration = Duration(b.getLong().toULong(), b.getInt().toUInt()) +} + +/** The most a `usize` holds on the library's target, whose `isize` holds half. */ +private val USIZE_MAX: ULong = __usize_max().toULong() + +/** A `usize` as JNI passes it, throwing past the library's target's, which may be 32-bit. */ +fun __usize(v: ULong): Long { + if (v > USIZE_MAX) throw __Invalid("$v is out of range for this target's usize") + return v.toLong() +} + +/** An `isize`, throwing past the library's target's, which may be 32-bit. */ +fun __isize(v: Long): Long { + val max = (USIZE_MAX shr 1).toLong() + if (v > max || v < -max - 1) throw __Invalid("$v is out of range for this target's isize") + return v +} + +/** A `NonZero`'s integer as JNI passes it, throwing if it is zero, as no `NonZero` is. */ +fun __nonZero(v: Byte): Byte = if (v != 0.toByte()) v else throw __Invalid("a NonZero is zero") + +fun __nonZero(v: Short): Short = if (v != 0.toShort()) v else throw __Invalid("a NonZero is zero") + +fun __nonZero(v: Int): Int = if (v != 0) v else throw __Invalid("a NonZero is zero") + +fun __nonZero(v: Long): Long = if (v != 0L) v else throw __Invalid("a NonZero is zero") + +/** A `char` as JNI passes it, throwing unless [v] is a Unicode scalar value, as every Rust `char` is. */ +fun __char(v: Int): Int { + if (v.toUInt() > 0x10ffffu || v ushr 11 == 0x1b) throw __Invalid("$v is no Unicode scalar value, as a Rust char is") + return v } /** @@ -689,7 +729,7 @@ const val LENT = -2 /** What precedes the place of Kotlin's array of bytes in a writer's `refs`, where an encoded one's length would. */ const val KOTLIN = -3 -/** What precedes the place of Kotlin's text in a writer's `refs`, where an encoded one's length would. */ +/** What marks Kotlin's text, which a writer's `refs` holds, where an encoded one's length would. */ const val TEXT = -4 /** Bytes a call returned: copied, lent by the owner and length a `LongArray` holds, or null for none. */ diff --git a/ffi/runtime/rt.py b/ffi/runtime/rt.py index 7061aca46..b051716b4 100644 --- a/ffi/runtime/rt.py +++ b/ffi/runtime/rt.py @@ -1,7 +1,7 @@ # The runtime every generated Python module includes: the awaitable a future that pends # is, the queue each event loop reads its wakes from, `Waker`, what runs an awaitable Rust -# polls, `Mut`, and `ForeignReader`. Its names start with `_` but `Waker`, `Mut`, and -# `ForeignReader`, so no exported name clashes with them. +# polls, `Mut`, `ForeignReader`, and `Duration`. Its names start with `_` but `Waker`, +# `Mut`, `ForeignReader`, and `Duration`, so no exported name clashes with them. class _Future(_n.Future, _typing.Generic[_T]): @@ -186,6 +186,31 @@ def readinto(self, buffer: _memoryview, /) -> _int | None: ... def seek(self, offset: _int, whence: _int = 0, /) -> _int: ... +class Duration(_typing.NamedTuple): + """Rust's `Duration`, as it holds one: whole seconds below 2**64, and the nanoseconds past + them, below a billion, which Rust checks a call's are. A `datetime.timedelta` holds only + whole microseconds, and none past 999,999,999 days, so each conversion checks it loses + nothing.""" + + secs: _int + nanos: _int = 0 + + @_staticmethod + def from_timedelta(delta: _datetime.timedelta) -> Duration: + """`delta`, raising `ValueError` if it is negative, which no Rust `Duration` is.""" + if delta < _datetime.timedelta(0): + raise _ValueError(f"{delta!r} is negative, which no Rust Duration is") + return Duration(delta.days * 86_400 + delta.seconds, delta.microseconds * 1_000) + + def to_timedelta(self) -> _datetime.timedelta: + """This as a `datetime.timedelta`, raising `ValueError` if it has a part of a + microsecond, and `OverflowError` if it is past `timedelta.max`.""" + micros = self.nanos // 1_000 + if self.nanos % 1_000: + raise _ValueError(f"{self!r} has a part of a microsecond, which no timedelta holds") + return _datetime.timedelta(seconds=self.secs, microseconds=micros) + + class _Const: """A constant a class names, made anew each time it is read, as Rust makes it.""" diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 202462e15..7c35f56f8 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -19,6 +19,12 @@ use std::task::{Context, Wake}; mod borrow; use borrow::{Borrow, EXCLUSIVE, RESERVED, TAKEN, Table}; +/// `value`'s `Hash`, through std's hasher with its fixed keys, so a value hashes alike on +/// every call. +pub fn hash(value: &T) -> u64 { + std::hash::BuildHasher::hash_one(&std::hash::BuildHasherDefault::::default(), value) +} + /// Abort with a message naming the C function whose contract was broken. #[cold] pub fn fail(what: &str, msg: &str) -> ! { @@ -340,24 +346,40 @@ impl Buf { } } - /// As [`Self::vec_in`], as a path: its bytes as the OS holds them on Unix, else UTF-8. + /// As [`Self::vec_in`], as a path: its bytes as the OS holds them on Unix, WTF-8 on + /// Windows, else UTF-8. /// /// # Safety /// As [`Self::bytes_in`]. pub unsafe fn path_in(self, what: &str, take: bool) -> std::path::PathBuf { + // SAFETY: the caller's contract. + let bytes = unsafe { self.vec_in(what, take) }; #[cfg(unix)] { use std::os::unix::ffi::OsStringExt; - // SAFETY: the caller's contract. - std::path::PathBuf::from(std::ffi::OsString::from_vec(unsafe { self.vec_in(what, take) })) + std::path::PathBuf::from(std::ffi::OsString::from_vec(bytes)) } #[cfg(not(unix))] - { - // SAFETY: the caller's contract. - std::path::PathBuf::from(unsafe { self.string_in(what, take) }) + match String::from_utf8(bytes) { + Ok(s) => std::path::PathBuf::from(s), + Err(err) => decoded(err.as_bytes(), what), } } + /// A path a caller passed in for the call alone, as [`Self::hold`] holds its bytes. + /// + /// # Safety + /// As [`Self::hold`]. + pub unsafe fn hold_path<'a>(self, what: &str) -> HeldPath<'a> { + // SAFETY: the caller's contract. + let held = unsafe { self.hold(what) }; + #[cfg(not(unix))] + if std::str::from_utf8(held.bytes()).is_err() { + return HeldPath::Decoded(decoded(held.bytes(), what)); + } + HeldPath::Held(held) + } + /// The bytes a caller passed in for the call alone: taken where it has an owner, which /// they free after, else borrowed. /// @@ -444,27 +466,9 @@ impl Buf { Self::owned(vec.into()) } - /// The path a caller passed in, which it keeps: its bytes as the OS holds them on - /// Unix, which may be any, else UTF-8. - /// - /// # Safety - /// As [`Self::bytes`]. - pub unsafe fn path(&self, what: &str) -> &std::path::Path { - #[cfg(unix)] - { - use std::os::unix::ffi::OsStrExt; - // SAFETY: the caller's contract. - std::path::Path::new(std::ffi::OsStr::from_bytes(unsafe { self.bytes(what) })) - } - #[cfg(not(unix))] - { - // SAFETY: the caller's contract. - std::path::Path::new(unsafe { self.str(what) }) - } - } - /// A path handed to the caller: its bytes as the OS holds them on Unix, else as - /// Rust encodes them, which is UTF-8 for any path that has a UTF-8 form. + /// Rust encodes them: WTF-8 on Windows, which is UTF-8 for any path that has a UTF-8 + /// form. pub fn path_buf(path: std::path::PathBuf) -> Self { Self::vec(path.into_os_string().into_encoded_bytes()) } @@ -528,19 +532,77 @@ impl Hold<'_> { Err(_) => fail(what, "a string is not UTF-8"), } } +} - /// The path these hold: its bytes as the OS holds them on Unix, else UTF-8. - pub fn path(&self, what: &str) -> &std::path::Path { - #[cfg(unix)] - { - use std::os::unix::ffi::OsStrExt; - let _ = what; - std::path::Path::new(std::ffi::OsStr::from_bytes(self.bytes())) +/// A path passed in for a call: the bytes [`Hold`] holds, or on a platform whose paths +/// are not bytes, what they decode to where they are not UTF-8. +pub enum HeldPath<'a> { + Held(Hold<'a>), + #[cfg(not(unix))] + Decoded(std::path::PathBuf), +} + +impl HeldPath<'_> { + pub fn path(&self) -> &std::path::Path { + match self { + #[cfg(unix)] + Self::Held(held) => { + use std::os::unix::ffi::OsStrExt; + std::path::Path::new(std::ffi::OsStr::from_bytes(held.bytes())) + } + // UTF-8, or else decoded. + #[cfg(not(unix))] + Self::Held(held) => std::path::Path::new(std::str::from_utf8(held.bytes()).unwrap_or_default()), + #[cfg(not(unix))] + Self::Decoded(path) => path, } - #[cfg(not(unix))] - { - std::path::Path::new(self.str(what)) + } +} + +/// The path a caller's bytes encode that are not UTF-8: on Windows, WTF-8, as Rust +/// encodes a name that is not UTF-16, any other aborting; elsewhere, none. +#[cfg(not(unix))] +#[cold] +fn decoded(bytes: &[u8], what: &str) -> std::path::PathBuf { + #[cfg(windows)] + if let Some(wide) = wtf8(bytes) { + use std::os::windows::ffi::OsStringExt; + return std::ffi::OsString::from_wide(&wide).into(); + } + let _ = bytes; + fail(what, "a path is not as this platform encodes one") +} + +/// The UTF-16 units of WTF-8 `bytes`, if they are: UTF-8 but for a surrogate alone, +/// encoded on its own as UTF-8 would a scalar, but never a lead and a trail together, +/// which WTF-8 encodes as the scalar they make. +pub fn wtf8(bytes: &[u8]) -> Option> { + let mut wide = Vec::with_capacity(bytes.len()); + let (mut rest, mut lead) = (bytes, false); + loop { + let (text, after) = match std::str::from_utf8(rest) { + Ok(text) => (text, &[][..]), + Err(err) => { + let (valid, after) = rest.split_at(err.valid_up_to()); + // SAFETY: UTF-8 up to there. + (unsafe { std::str::from_utf8_unchecked(valid) }, after) + } + }; + if !text.is_empty() { + wide.extend(text.encode_utf16()); + lead = false; + } + let [0xED, b @ 0xA0..=0xBF, c @ 0x80..=0xBF, ..] = *after else { + return after.is_empty().then_some(wide); + }; + let unit = 0xD000 | u16::from(b & 0x3F) << 6 | u16::from(c & 0x3F); + let trail = unit >= 0xDC00; + if trail && lead { + return None; } + wide.push(unit); + lead = !trail; + rest = &after[3..]; } } @@ -588,121 +650,165 @@ impl Duration { } } -/// `std::net::SocketAddr`: an IPv4 address in the first 4 bytes of `ip`, with the rest of -/// `ip`, `flowinfo`, and `scope_id` zero, or an IPv6 address. `ip` is in network order and -/// `port` in the host's. +/// `std::net::Ipv4Addr`, in network order. #[repr(C)] #[derive(Clone, Copy)] -pub struct SocketAddr { - pub ip: [u8; 16], - pub port: u16, - pub v6: bool, - pub flowinfo: u32, - pub scope_id: u32, +pub struct Ipv4Addr { + pub octets: [u8; 4], } -impl SocketAddr { - pub fn lift(self, what: &str) -> std::net::SocketAddr { - if self.v6 { - let ip = std::net::Ipv6Addr::from(self.ip); - return std::net::SocketAddrV6::new(ip, self.port, self.flowinfo, self.scope_id).into(); - } - let [a, b, c, d, rest @ ..] = self.ip; - if rest != [0; 12] || self.flowinfo != 0 || self.scope_id != 0 { - fail(what, "an IPv4 socket address has IPv6 bytes or fields"); - } - std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(a, b, c, d), self.port).into() +impl Ipv4Addr { + pub fn lift(self) -> std::net::Ipv4Addr { + self.octets.into() } - /// An IPv4 address, for `SocketAddrV4`: an IPv6 one aborts. - pub fn lift_v4(self, what: &str) -> std::net::SocketAddrV4 { - match self.lift(what) { - std::net::SocketAddr::V4(addr) => addr, - std::net::SocketAddr::V6(_) => fail(what, "an IPv6 socket address where IPv4 is taken"), - } + pub fn lower(ip: std::net::Ipv4Addr) -> Self { + Self { octets: ip.octets() } } +} - /// An IPv6 address, for `SocketAddrV6`: an IPv4 one aborts. - pub fn lift_v6(self, what: &str) -> std::net::SocketAddrV6 { - match self.lift(what) { - std::net::SocketAddr::V6(addr) => addr, - std::net::SocketAddr::V4(_) => fail(what, "an IPv4 socket address where IPv6 is taken"), - } +/// `std::net::Ipv6Addr`, in network order. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct Ipv6Addr { + pub octets: [u8; 16], +} + +impl Ipv6Addr { + pub fn lift(self) -> std::net::Ipv6Addr { + self.octets.into() } - pub fn lower(addr: std::net::SocketAddr) -> Self { - match addr { - std::net::SocketAddr::V4(v4) => { - let mut ip = [0; 16]; - ip[..4].copy_from_slice(&v4.ip().octets()); - Self { - ip, - port: v4.port(), - v6: false, - flowinfo: 0, - scope_id: 0, - } - } - std::net::SocketAddr::V6(v6) => Self { - ip: v6.ip().octets(), - port: v6.port(), - v6: true, - flowinfo: v6.flowinfo(), - scope_id: v6.scope_id(), - }, - } + pub fn lower(ip: std::net::Ipv6Addr) -> Self { + Self { octets: ip.octets() } } } -/// `std::net::IpAddr`: an IPv4 address in the first 4 bytes of `ip`, with the rest zero, or -/// an IPv6 address, in network order. +/// An [`IpAddr`]'s address, of the family its `v6` names. +#[repr(C)] +#[derive(Clone, Copy)] +pub union Ip { + pub v4: Ipv4Addr, + pub v6: Ipv6Addr, +} + +/// `std::net::IpAddr`: `ip.v6` if `v6`, else `ip.v4`. #[repr(C)] #[derive(Clone, Copy)] pub struct IpAddr { - pub ip: [u8; 16], pub v6: bool, + pub ip: Ip, } impl IpAddr { - pub fn lift(self, what: &str) -> std::net::IpAddr { - if self.v6 { - return std::net::Ipv6Addr::from(self.ip).into(); + pub fn lift(self) -> std::net::IpAddr { + // SAFETY: `v6` names the member the caller wrote. + match self.v6 { + true => unsafe { self.ip.v6 }.lift().into(), + false => unsafe { self.ip.v4 }.lift().into(), } - let [a, b, c, d, rest @ ..] = self.ip; - if rest != [0; 12] { - fail(what, "an IPv4 address has IPv6 bytes"); + } + + pub fn lower(ip: std::net::IpAddr) -> Self { + // The other family's bytes are zero, not uninitialized, for a caller copying them. + let mut c = Self { + v6: ip.is_ipv6(), + ip: Ip { + v6: Ipv6Addr { octets: [0; 16] }, + }, + }; + match ip { + std::net::IpAddr::V4(v4) => c.ip.v4 = Ipv4Addr::lower(v4), + std::net::IpAddr::V6(v6) => c.ip.v6 = Ipv6Addr::lower(v6), } - std::net::Ipv4Addr::new(a, b, c, d).into() + c + } +} + +/// `std::net::SocketAddrV4`, its port in the host's order. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SocketAddrV4 { + pub ip: Ipv4Addr, + pub port: u16, +} + +impl SocketAddrV4 { + pub fn lift(self) -> std::net::SocketAddrV4 { + std::net::SocketAddrV4::new(self.ip.lift(), self.port) } - /// An IPv4 address, for `Ipv4Addr`: an IPv6 one aborts. - pub fn lift_v4(self, what: &str) -> std::net::Ipv4Addr { - match self.lift(what) { - std::net::IpAddr::V4(ip) => ip, - std::net::IpAddr::V6(_) => fail(what, "an IPv6 address where IPv4 is taken"), + pub fn lower(addr: std::net::SocketAddrV4) -> Self { + Self { + ip: Ipv4Addr::lower(*addr.ip()), + port: addr.port(), } } +} - /// An IPv6 address, for `Ipv6Addr`: an IPv4 one aborts. - pub fn lift_v6(self, what: &str) -> std::net::Ipv6Addr { - match self.lift(what) { - std::net::IpAddr::V6(ip) => ip, - std::net::IpAddr::V4(_) => fail(what, "an IPv4 address where IPv6 is taken"), +/// `std::net::SocketAddrV6`, its port in the host's order. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SocketAddrV6 { + pub ip: Ipv6Addr, + pub port: u16, + pub flowinfo: u32, + pub scope_id: u32, +} + +impl SocketAddrV6 { + pub fn lift(self) -> std::net::SocketAddrV6 { + std::net::SocketAddrV6::new(self.ip.lift(), self.port, self.flowinfo, self.scope_id) + } + + pub fn lower(addr: std::net::SocketAddrV6) -> Self { + Self { + ip: Ipv6Addr::lower(*addr.ip()), + port: addr.port(), + flowinfo: addr.flowinfo(), + scope_id: addr.scope_id(), } } +} - pub fn lower(ip: std::net::IpAddr) -> Self { - match ip { - std::net::IpAddr::V4(v4) => { - let mut ip = [0; 16]; - ip[..4].copy_from_slice(&v4.octets()); - Self { ip, v6: false } - } - std::net::IpAddr::V6(v6) => Self { - ip: v6.octets(), - v6: true, +/// A [`SocketAddr`]'s address, of the family its `v6` names. +#[repr(C)] +#[derive(Clone, Copy)] +pub union Socket { + pub v4: SocketAddrV4, + pub v6: SocketAddrV6, +} + +/// `std::net::SocketAddr`: `addr.v6` if `v6`, else `addr.v4`. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SocketAddr { + pub v6: bool, + pub addr: Socket, +} + +impl SocketAddr { + pub fn lift(self) -> std::net::SocketAddr { + // SAFETY: `v6` names the member the caller wrote. + match self.v6 { + true => unsafe { self.addr.v6 }.lift().into(), + false => unsafe { self.addr.v4 }.lift().into(), + } + } + + pub fn lower(addr: std::net::SocketAddr) -> Self { + // The other family's bytes are zero, not uninitialized, for a caller copying them. + let mut c = Self { + v6: addr.is_ipv6(), + addr: Socket { + v6: SocketAddrV6::lower(std::net::SocketAddrV6::new(std::net::Ipv6Addr::UNSPECIFIED, 0, 0, 0)), }, + }; + match addr { + std::net::SocketAddr::V4(v4) => c.addr.v4 = SocketAddrV4::lower(v4), + std::net::SocketAddr::V6(v6) => c.addr.v6 = SocketAddrV6::lower(v6), } + c } } diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index cf4509eb0..d40bb7229 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -3,17 +3,81 @@ // bytes and text across the C ABI, and the signal a future parks on. Each helper that // returns what its body does takes a noncopyable result, an object's struct. -/// An IP socket address, as Rust's `SocketAddr` holds it. -public struct SocketAddr: Hashable, Sendable { - /// The IP in network order: 4 bytes for IPv4, or 16 for IPv6. - public var ip: [UInt8] +/// An IPv4 address, as Rust's `Ipv4Addr` holds it. +public struct Ipv4Addr: Hashable, Sendable { + /// Its 32 bits, the first octet highest, as Rust's `to_bits` gives them. + public var bits: UInt32 + + public init(bits: UInt32) { + self.bits = bits + } + + /// The address `a.b.c.d`. + public init(_ a: UInt8, _ b: UInt8, _ c: UInt8, _ d: UInt8) { + bits = UInt32(a) << 24 | UInt32(b) << 16 | UInt32(c) << 8 | UInt32(d) + } + + /// The address of `octets`, in network order, or nil unless it holds exactly 4. + public init?(octets: some Swift.Collection) { + guard octets.count == 4 else { return nil } + bits = octets.reduce(0) { $0 << 8 | UInt32($1) } + } + + /// Its 4 octets, in network order. + public var octets: [UInt8] { + [24, 16, 8, 0].map { UInt8(truncatingIfNeeded: bits >> $0) } + } +} + +/// An IPv6 address, as Rust's `Ipv6Addr` holds it. +public struct Ipv6Addr: Hashable, Sendable { + /// Its 128 bits, the first octet highest, as Rust's `to_bits` gives them. + public var bits: U128 + + public init(bits: U128) { + self.bits = bits + } + + /// The address of `octets`, in network order, or nil unless it holds exactly 16. + public init?(octets: some Swift.Collection) { + guard octets.count == 16 else { return nil } + let high = octets.prefix(8).reduce(0) { $0 << 8 | UInt64($1) } + bits = U128(high: high, low: octets.dropFirst(8).reduce(0) { $0 << 8 | UInt64($1) }) + } + + /// Its 16 octets, in network order. + public var octets: [UInt8] { + [bits.high, bits.low].flatMap { half in stride(from: 56, through: 0, by: -8).map { UInt8(truncatingIfNeeded: half >> $0) } } + } +} + +/// An IP address of either family, as Rust's `IpAddr`. +public enum IpAddr: Hashable, Sendable { + case v4(Ipv4Addr) + case v6(Ipv6Addr) +} + +/// An IPv4 socket address, as Rust's `SocketAddrV4`. +public struct SocketAddrV4: Hashable, Sendable { + public var ip: Ipv4Addr + public var port: UInt16 + + public init(ip: Ipv4Addr, port: UInt16) { + self.ip = ip + self.port = port + } +} + +/// An IPv6 socket address, as Rust's `SocketAddrV6`. +public struct SocketAddrV6: Hashable, Sendable { + public var ip: Ipv6Addr public var port: UInt16 - /// IPv6's flow label, zero for IPv4. + /// Its flow label. public var flowinfo: UInt32 - /// IPv6's scope, zero for IPv4. + /// Its scope, as an interface index. public var scopeId: UInt32 - public init(ip: [UInt8], port: UInt16, flowinfo: UInt32 = 0, scopeId: UInt32 = 0) { + public init(ip: Ipv6Addr, port: UInt16, flowinfo: UInt32 = 0, scopeId: UInt32 = 0) { self.ip = ip self.port = port self.flowinfo = flowinfo @@ -21,13 +85,147 @@ public struct SocketAddr: Hashable, Sendable { } } -/// An IP address, as Rust's `IpAddr` holds it. -public struct IpAddr: Hashable, Sendable { - /// The IP in network order: 4 bytes for IPv4, or 16 for IPv6. - public var ip: [UInt8] +/// A socket address of either family, as Rust's `SocketAddr`. +public enum SocketAddr: Hashable, Sendable { + case v4(SocketAddrV4) + case v6(SocketAddrV6) - public init(ip: [UInt8]) { - self.ip = ip + /// `ip` at `port`, with no flow label or scope for IPv6, as Rust's `SocketAddr::new`. + public init(ip: IpAddr, port: UInt16) { + switch ip { + case .v4(let ip): self = .v4(SocketAddrV4(ip: ip, port: port)) + case .v6(let ip): self = .v6(SocketAddrV6(ip: ip, port: port)) + } + } + + /// Its IP, of its family. + public var ip: IpAddr { + switch self { + case .v4(let addr): return .v4(addr.ip) + case .v6(let addr): return .v6(addr.ip) + } + } + + /// Its port. + public var port: UInt16 { + switch self { + case .v4(let addr): return addr.port + case .v6(let addr): return addr.port + } + } +} + +/// A Rust `Duration`: whole seconds and the nanoseconds past them, below a second, so it +/// holds every value Rust's does and no other. +public struct RustDuration: Hashable, Comparable, Sendable { + public let secs: UInt64 + /// Below 1,000,000,000. + public let nanos: UInt32 + + init(_c: Native.CDuration) { + secs = _c.secs + nanos = _c.nanos + } + + /// `secs` seconds and `nanos` nanoseconds, throwing unless `nanos` is below a second. + public init(secs: UInt64, nanos: UInt32) throws { + guard nanos < 1_000_000_000 else { + throw CheckError(function: "RustDuration(secs:nanos:)", message: "\(nanos) nanoseconds are a second or more") + } + self.init(_c: Native.CDuration(secs: secs, nanos: nanos)) + } + + /// `secs` whole seconds. + public static func seconds(_ secs: UInt64) -> RustDuration { + RustDuration(_c: Native.CDuration(secs: secs, nanos: 0)) + } + + /// `millis` milliseconds, as Rust's `from_millis`. + public static func milliseconds(_ millis: UInt64) -> RustDuration { + RustDuration(_c: Native.CDuration(secs: millis / 1000, nanos: UInt32(millis % 1000) * 1_000_000)) + } + + /// `micros` microseconds, as Rust's `from_micros`. + public static func microseconds(_ micros: UInt64) -> RustDuration { + RustDuration(_c: Native.CDuration(secs: micros / 1_000_000, nanos: UInt32(micros % 1_000_000) * 1000)) + } + + /// `nanos` nanoseconds, as Rust's `from_nanos`. + public static func nanoseconds(_ nanos: UInt64) -> RustDuration { + RustDuration(_c: Native.CDuration(secs: nanos / 1_000_000_000, nanos: UInt32(nanos % 1_000_000_000))) + } + + /// `duration`, throwing for one that is negative, has a part below a nanosecond, or is + /// past Rust's longest, `UInt64.max` seconds and 999,999,999 nanoseconds. + public init(_ duration: Swift.Duration) throws { + let what = "RustDuration(_:)" + guard duration >= .zero else { + throw CheckError(function: what, message: "a negative duration") + } + guard duration <= RustDuration.longest else { + throw CheckError(function: what, message: "a duration past Rust's longest") + } + // In halves, since `components` gives Int64's seconds, and Rust's are UInt64's. + let half = duration / 2 + let (secs, attos) = half.components + let odd = (duration - half * 2).components.attoseconds + let (whole, part) = (attos * 2 + odd).quotientAndRemainder(dividingBy: 1_000_000_000) + guard part == 0 else { + throw CheckError(function: what, message: "a duration with a part below a nanosecond") + } + // `whole` is below two seconds' nanoseconds. + let carry = whole >= 1_000_000_000 + self.init(_c: Native.CDuration(secs: UInt64(secs) * 2 + (carry ? 1 : 0), nanos: UInt32(carry ? whole - 1_000_000_000 : whole))) + } + + /// Rust's longest `Duration`, which `Swift.Duration` holds. + static var longest: Swift.Duration { + .seconds(Int64.max) * 2 + .seconds(1) + .nanoseconds(999_999_999) + } + + /// It as a `Swift.Duration`, which holds every Rust `Duration`. + public var duration: Swift.Duration { + // Past Int64's seconds, as Rust's may be, in halves. + Swift.Duration.seconds(Int64(secs >> 1)) * 2 + .seconds(Int64(secs & 1)) + .nanoseconds(Int64(nanos)) + } + + public static func < (a: RustDuration, b: RustDuration) -> Bool { + (a.secs, a.nanos) < (b.secs, b.nanos) + } +} + +/// An OS path, as Rust's `PathBuf` holds it: its bytes, which on Unix need not be UTF-8, +/// shared with Rust's buffer as `Bytes` are. +public struct PathBuf: Hashable, Sendable, Swift.ExpressibleByStringLiteral { + /// Its bytes as the OS holds them on Unix, or WTF-8 on Windows: UTF-8 for any path + /// that has a UTF-8 form. + public let bytes: Bytes + + /// The path of the OS's `bytes`, which on Unix may be any. + public init(bytes: Bytes) { + self.bytes = bytes + } + + /// The path `string` names. + public init(_ string: Swift.String) { + self.init(bytes: Bytes(Data(string.utf8))) + } + + public init(stringLiteral string: Swift.String) { + self.init(string) + } + + /// Its text, or nil if its bytes are not UTF-8. + public var string: Swift.String? { + var iterator = bytes.makeIterator() + var parser = Unicode.UTF8.ForwardParser() + while true { + switch parser.parseScalar(from: &iterator) { + case .valid: continue + case .emptyInput: return Swift.String(decoding: bytes, as: UTF8.self) + case .error: return nil + } + } } } @@ -128,11 +326,34 @@ public struct U128: Hashable, Comparable, Sendable, CustomStringConvertible { } } +/// Rust's `NonZero`: an integer that is never zero. +public struct NonZero: Hashable, Comparable, Sendable, CustomStringConvertible { + /// The integer, never zero, as Rust's `get`. + public let value: T + + init(_unchecked value: T) { + self.value = value + } + + /// `value`, throwing for a zero. + public init(_ value: T) throws { + guard value != 0 else { + throw CheckError(function: "NonZero(_:)", message: "a NonZero is zero") + } + self.value = value + } + + public static func < (a: NonZero, b: NonZero) -> Swift.Bool { a.value < b.value } + + public var description: Swift.String { value.description } +} + /// A call Rust's rules refuse, thrown before any Rust runs: a handle borrowed in a way /// the borrow checker forbids beside another call's, used once its value moved out or its -/// lend ended, or an item called on a library whose target lacks it. +/// lend ended, an item called on a library whose target lacks it, or a value Rust's type +/// cannot hold given a runtime type's initializer, such as a zero given `NonZero`'s. public struct CheckError: Swift.Error, CustomStringConvertible, Sendable { - /// The function called, by its C name. + /// The function called, by its C name, or the runtime initializer. public let function: Swift.String /// What broke. public let message: Swift.String @@ -377,6 +598,17 @@ extension Native { try view(text, body) } + /// A path Rust takes during `body`, as its bytes are. + @inline(__always) + static func take(_ path: PathBuf, _ body: (Buf) throws -> R) rethrows -> R { + try take(path.bytes, body) + } + + @inline(__always) + static func take(_ path: PathBuf?, _ body: (Buf) throws -> R) rethrows -> R { + try take(path?.bytes, body) + } + /// Bytes C lends a callback for its call, copied. static func bytes(lent buf: Buf) -> Bytes { buf.len == 0 ? Bytes() : Bytes(Data(bytes: buf.ptr!, count: buf.len)) @@ -405,33 +637,29 @@ extension Native { buf.ptr == nil ? nil : string(lent: buf) } - /// A path Rust returned, copied, then freed. - static func path(_ buf: Buf) -> Swift.String { - defer { free(buf) } - return path(lent: buf) + /// A path Rust returned, its buffer shared as `Bytes` share it. + static func path(_ buf: Buf) -> PathBuf { + PathBuf(bytes: bytes(buf)) } - static func path(option buf: Buf) -> Swift.String? { + static func path(option buf: Buf) -> PathBuf? { buf.ptr == nil ? nil : path(buf) } - /// A path from Rust, copied, which on Unix may hold bytes no `String` holds: those trap. - static func path(lent buf: Buf) -> Swift.String { - var bytes = UnsafeBufferPointer(start: buf.ptr, count: buf.len).makeIterator() - var parser = Unicode.UTF8.ForwardParser() - while true { - switch parser.parseScalar(from: &bytes) { - case .valid: continue - case .emptyInput: return string(lent: buf) - case .error: fatalError("a path from Rust is not UTF-8") - } - } + /// A path C lends a callback for its call, copied. + static func path(lent buf: Buf) -> PathBuf { + PathBuf(bytes: bytes(lent: buf)) } - static func path(lentOption buf: Buf) -> Swift.String? { + static func path(lentOption buf: Buf) -> PathBuf? { buf.ptr == nil ? nil : path(lent: buf) } + /// `path`'s bytes in a buffer Rust owns, as what a closure returns. + static func owned(_ path: PathBuf) -> Buf { + view(path.bytes) { copy($0) } + } + static func scalar(_ code: UInt32) -> Unicode.Scalar { guard let scalar = Unicode.Scalar(code) else { fatalError("a char is not a Unicode scalar") @@ -439,80 +667,97 @@ extension Native { return scalar } - /// Rust's longest `Duration`: `u64::MAX` seconds and 999,999,999 nanoseconds. - static var longest: Swift.Duration { - .seconds(Int64.max) * 2 + .seconds(1) + .nanoseconds(999_999_999) + static func u128(_ value: U128) -> CU128 { + CU128(lo: value.low, hi: value.high) } - static func duration(_ value: Swift.Duration) -> CDuration { - if value < .zero { - fatalError("a negative duration has no Rust Duration") - } - if value > longest { - fatalError("a duration past Rust's Duration::MAX") - } - // In halves, since `components` gives Int64's seconds, and Rust's are UInt64's. - let half = value / 2 - let (secs, attos) = half.components - let odd = (value - half * 2).components.attoseconds - var nanos = (attos * 2 + odd) / 1_000_000_000 - var whole = UInt64(secs) * 2 - if nanos >= 1_000_000_000 { - nanos -= 1_000_000_000 - whole += 1 - } - return CDuration(secs: whole, nanos: UInt32(nanos)) + static func u128(_ value: CU128) -> U128 { + U128(high: value.hi, low: value.lo) } - static func duration(_ value: CDuration) -> Swift.Duration { - // Past Int64's seconds, as Rust's may be, in halves. - let half = Swift.Duration.seconds(Int64(value.secs >> 1)) - return half * 2 + .seconds(Int64(value.secs & 1)) + .nanoseconds(Int64(value.nanos)) + static func duration(_ value: RustDuration) -> CDuration { + CDuration(secs: value.secs, nanos: value.nanos) } - static func u128(_ value: U128) -> CU128 { - CU128(lo: value.low, hi: value.high) + static func duration(_ value: CDuration) -> RustDuration { + RustDuration(_c: value) } - static func u128(_ value: CU128) -> U128 { - U128(high: value.hi, low: value.lo) + // Each address as its C struct, in network order, and back: every value of either + // is one of the other. + + static func addr(_ ip: Ipv4Addr) -> CIpv4Addr { + var c = CIpv4Addr() + withUnsafeMutableBytes(of: &c.octets) { $0.storeBytes(of: ip.bits.bigEndian, as: UInt32.self) } + return c } - /// Whether `ip` is IPv6, by its length. - static func v6(_ ip: [UInt8]) -> Bool { - switch ip.count { - case 4: return false - case 16: return true - default: fatalError("an IP address is 4 bytes or 16, not \(ip.count)") - } + static func addr(_ c: CIpv4Addr) -> Ipv4Addr { + Ipv4Addr(bits: UInt32(bigEndian: withUnsafeBytes(of: c.octets) { $0.loadUnaligned(as: UInt32.self) })) } - static func socketAddr(_ addr: SocketAddr) -> CAddr { - var c = CAddr() - c.v6 = v6(addr.ip) - withUnsafeMutableBytes(of: &c.ip) { ip in ip.copyBytes(from: addr.ip) } - c.port = addr.port - c.flowinfo = addr.flowinfo - c.scope_id = addr.scopeId + static func addr(_ ip: Ipv6Addr) -> CIpv6Addr { + var c = CIpv6Addr() + withUnsafeMutableBytes(of: &c.octets) { octets in + octets.storeBytes(of: ip.bits.high.bigEndian, as: UInt64.self) + octets.storeBytes(of: ip.bits.low.bigEndian, toByteOffset: 8, as: UInt64.self) + } return c } - static func socketAddr(_ c: CAddr) -> SocketAddr { - var c = c - let ip = withUnsafeBytes(of: &c.ip) { ip in Array(ip.prefix(c.v6 ? 16 : 4)) } - return SocketAddr(ip: ip, port: c.port, flowinfo: c.flowinfo, scopeId: c.scope_id) + static func addr(_ c: CIpv6Addr) -> Ipv6Addr { + withUnsafeBytes(of: c.octets) { octets in + Ipv6Addr( + bits: U128( + high: UInt64(bigEndian: octets.loadUnaligned(as: UInt64.self)), + low: UInt64(bigEndian: octets.loadUnaligned(fromByteOffset: 8, as: UInt64.self)))) + } } - static func ipAddr(_ addr: IpAddr) -> CIpAddr { + static func addr(_ ip: IpAddr) -> CIpAddr { var c = CIpAddr() - c.v6 = v6(addr.ip) - withUnsafeMutableBytes(of: &c.ip) { ip in ip.copyBytes(from: addr.ip) } + switch ip { + case .v4(let ip): c.ip.v4 = addr(ip) + case .v6(let ip): + c.v6 = true + c.ip.v6 = addr(ip) + } return c } - static func ipAddr(_ c: CIpAddr) -> IpAddr { - var c = c - return IpAddr(ip: withUnsafeBytes(of: &c.ip) { ip in Array(ip.prefix(c.v6 ? 16 : 4)) }) + static func addr(_ c: CIpAddr) -> IpAddr { + c.v6 ? .v6(addr(c.ip.v6)) : .v4(addr(c.ip.v4)) + } + + static func addr(_ addr: SocketAddrV4) -> CSocketAddrV4 { + CSocketAddrV4(ip: Native.addr(addr.ip), port: addr.port) + } + + static func addr(_ c: CSocketAddrV4) -> SocketAddrV4 { + SocketAddrV4(ip: addr(c.ip), port: c.port) + } + + static func addr(_ addr: SocketAddrV6) -> CSocketAddrV6 { + CSocketAddrV6(ip: Native.addr(addr.ip), port: addr.port, flowinfo: addr.flowinfo, scope_id: addr.scopeId) + } + + static func addr(_ c: CSocketAddrV6) -> SocketAddrV6 { + SocketAddrV6(ip: addr(c.ip), port: c.port, flowinfo: c.flowinfo, scopeId: c.scope_id) + } + + static func addr(_ addr: SocketAddr) -> CSocketAddr { + var c = CSocketAddr() + switch addr { + case .v4(let addr): c.addr.v4 = Native.addr(addr) + case .v6(let addr): + c.v6 = true + c.addr.v6 = Native.addr(addr) + } + return c + } + + static func addr(_ c: CSocketAddr) -> SocketAddr { + c.v6 ? .v6(addr(c.addr.v6)) : .v4(addr(c.addr.v4)) } /// The C array Rust returned, as its items each lifted by `lift`, which takes what @@ -816,6 +1061,14 @@ extension Native { } } + func buf(_ path: PathBuf) -> Buf { + buf(path.bytes) + } + + func buf(_ path: PathBuf?) -> Buf { + buf(path?.bytes) + } + private func give(_ buf: Buf) -> Buf { given.append(buf) return buf diff --git a/ffi/runtime/types.kt b/ffi/runtime/types.kt new file mode 100644 index 000000000..9cb2778a2 --- /dev/null +++ b/ffi/runtime/types.kt @@ -0,0 +1,298 @@ +// The runtime's public types, which every generated Kotlin file declares at its top level: +// Rust's std values no Kotlin or Java type holds exactly, each exact by construction, with +// checked conversions to and from the idiomatic type that throw `IllegalArgumentException` +// for a value the other cannot hold. Every free function is qualified, as an export may +// take its name. + +/** + * A Rust `Duration`: whole seconds, any `u64`, and nanoseconds below one second, so it holds + * every Rust duration exactly, where a `kotlin.time.Duration` past about 146 years drops + * what is below a millisecond and one past `Long.MAX_VALUE` seconds is infinite. + */ +class Duration(val secs: ULong, val nanos: UInt = 0u) : kotlin.Comparable { + init { + kotlin.require(nanos < NANOS) { "$nanos nanoseconds is not below a second" } + } + + /** [duration] exactly, throwing `IllegalArgumentException` for a negative or infinite one, which Rust's has no form of. */ + constructor(duration: kotlin.time.Duration) : this(seconds(duration), duration.toComponents { _, nanos -> nanos.toUInt() }) + + /** This as a `kotlin.time.Duration`, throwing `IllegalArgumentException` where one cannot hold it exactly. */ + fun toKotlinDuration(): kotlin.time.Duration { + val d = when { + secs > Long.MAX_VALUE.toULong() -> kotlin.time.Duration.INFINITE + else -> secs.toLong().toDuration(kotlin.time.DurationUnit.SECONDS) + nanos.toInt().toDuration(kotlin.time.DurationUnit.NANOSECONDS) + } + val exact = d.isFinite() && d.toComponents { s, n -> s.toULong() == secs && n.toUInt() == nanos } + kotlin.require(exact) { "$this has no exact kotlin.time.Duration" } + return d + } + + override fun compareTo(other: Duration): Int = when (val c = secs.compareTo(other.secs)) { + 0 -> nanos.compareTo(other.nanos) + else -> c + } + + override fun equals(other: Any?): Boolean = other is Duration && secs == other.secs && nanos == other.nanos + + override fun hashCode(): Int = 31 * secs.hashCode() + nanos.hashCode() + + /** Its seconds and nanoseconds, as `1.500000000s`. */ + override fun toString(): String = "$secs.${nanos.toString().padStart(9, '0')}s" + + companion object { + private const val NANOS = 1_000_000_000u + + /** No time. */ + val ZERO = Duration(0u) + + /** The longest, as Rust's `Duration::MAX`. */ + val MAX = Duration(ULong.MAX_VALUE, NANOS - 1u) + + private fun seconds(duration: kotlin.time.Duration): ULong { + kotlin.require(duration.isFinite() && !duration.isNegative()) { "$duration has no Rust Duration" } + return duration.inWholeSeconds.toULong() + } + } +} + +/** A Rust `IpAddr`: an [Ipv4Addr] or an [Ipv6Addr], each of its own family, as Rust keeps it. */ +sealed interface IpAddr { + /** This as a `java.net.InetAddress` of its family: an IPv4-mapped IPv6 one stays IPv6. */ + fun toInetAddress(): java.net.InetAddress + + companion object { + /** [address] exactly, throwing `IllegalArgumentException` for an IPv6 one with a scope, which Rust's `IpAddr` has no room for. */ + fun of(address: java.net.InetAddress): IpAddr = when (address) { + is java.net.Inet6Address -> Ipv6Addr(address) + else -> Ipv4Addr(address.address) + } + } +} + +/** A Rust `Ipv4Addr`: its 32 bits, the first octet highest, as Rust's `to_bits` has them. */ +@kotlin.jvm.JvmInline +value class Ipv4Addr(val bits: UInt) : IpAddr { + /** The address of [octets], which throws `IllegalArgumentException` unless there are 4. */ + constructor(octets: ByteArray) : this(_octets(octets, 4).let { java.nio.ByteBuffer.wrap(it).getInt().toUInt() }) + + /** The address of [a].[b].[c].[d]. */ + constructor(a: UByte, b: UByte, c: UByte, d: UByte) : this((a.toUInt() shl 24) or (b.toUInt() shl 16) or (c.toUInt() shl 8) or d.toUInt()) + + /** Its 4 octets. */ + fun octets(): ByteArray = java.nio.ByteBuffer.allocate(4).putInt(bits.toInt()).array() + + override fun toInetAddress(): java.net.InetAddress = java.net.InetAddress.getByAddress(octets()) + + /** As Rust prints it: `127.0.0.1`. */ + override fun toString(): String = octets().joinToString(".") { (it.toInt() and 0xff).toString() } +} + +/** A Rust `Ipv6Addr`: its 16 octets. */ +class Ipv6Addr internal constructor(internal val hi: Long, internal val lo: Long) : IpAddr { + /** The address of [octets], which throws `IllegalArgumentException` unless there are 16. */ + constructor(octets: ByteArray) : this(java.nio.ByteBuffer.wrap(_octets(octets, 16))) + + /** [address], throwing `IllegalArgumentException` for one with a scope, which Rust's `Ipv6Addr` has no room for. */ + constructor(address: java.net.Inet6Address) : this(_unscoped(address).address) + + private constructor(b: java.nio.ByteBuffer) : this(b.getLong(), b.getLong()) + + /** Its 16 octets. */ + fun octets(): ByteArray = java.nio.ByteBuffer.allocate(16).putLong(hi).putLong(lo).array() + + override fun toInetAddress(): java.net.Inet6Address = java.net.Inet6Address.getByAddress(null, octets(), null as java.net.NetworkInterface?) + + override fun equals(other: Any?): Boolean = other is Ipv6Addr && hi == other.hi && lo == other.lo + + override fun hashCode(): Int = 31 * hi.hashCode() + lo.hashCode() + + /** As Rust prints it: the longest run of zero segments, if more than one, as `::`, and an IPv4-mapped one as `::ffff:1.2.3.4`. */ + override fun toString(): String { + if (hi == 0L && lo ushr 32 == 0xffffL) return "::ffff:${Ipv4Addr(lo.toUInt())}" + val segments = IntArray(8) { k -> ((if (k < 4) hi else lo) ushr (48 - 16 * (k % 4))).toInt() and 0xffff } + var (start, len) = 0 to 0 + var at = 0 + while (at < 8) { + var end = at + while (end < 8 && segments[end] == 0) end++ + if (end - at > len) { + start = at + len = end - at + } + at = end + 1 + } + val hex = { from: Int, until: Int -> (from until until).joinToString(":") { segments[it].toString(16) } } + return if (len > 1) "${hex(0, start)}::${hex(start + len, 8)}" else hex(0, 8) + } +} + +/** A Rust `SocketAddr`: a [SocketAddrV4] or a [SocketAddrV6]. */ +sealed interface SocketAddr { + /** Its port. */ + val port: UShort + + /** This as a `java.net.InetSocketAddress`, throwing `IllegalArgumentException` for an IPv6 one with a flow label, which Java has no room for. */ + fun toInetSocketAddress(): java.net.InetSocketAddress + + companion object { + /** [address], throwing `IllegalArgumentException` for an unresolved one, which Rust's `SocketAddr` has no form of. */ + fun of(address: java.net.InetSocketAddress): SocketAddr = when (_resolved(address)) { + is java.net.Inet6Address -> SocketAddrV6(address) + else -> SocketAddrV4(address) + } + } +} + +/** A Rust `SocketAddrV4`: an [Ipv4Addr] and a port. */ +@kotlin.jvm.JvmInline +value class SocketAddrV4 private constructor(private val bits: Long) : SocketAddr { + constructor(ip: Ipv4Addr, port: UShort) : this((ip.bits.toLong() shl 16) or port.toLong()) + + /** [address], throwing `IllegalArgumentException` unless it is resolved and IPv4. */ + constructor(address: java.net.InetSocketAddress) : this( + Ipv4Addr((_resolved(address) as? java.net.Inet4Address ?: throw kotlin.IllegalArgumentException("$address is not IPv4")).address), + address.port.toUShort(), + ) + + val ip: Ipv4Addr get() = Ipv4Addr((bits ushr 16).toUInt()) + + override val port: UShort get() = bits.toUShort() + + override fun toInetSocketAddress(): java.net.InetSocketAddress = java.net.InetSocketAddress(ip.toInetAddress(), port.toInt()) + + /** As Rust prints it: `127.0.0.1:443`. */ + override fun toString(): String = "$ip:$port" +} + +/** A Rust `SocketAddrV6`: an [Ipv6Addr], a port, a flow label, and a scope, which Java keeps no flow label of. */ +class SocketAddrV6(val ip: Ipv6Addr, override val port: UShort, val flowinfo: UInt = 0u, val scopeId: UInt = 0u) : SocketAddr { + /** [address], with no flow label, throwing `IllegalArgumentException` unless it is resolved and IPv6. */ + constructor(address: java.net.InetSocketAddress) : this( + (_resolved(address) as? java.net.Inet6Address ?: throw kotlin.IllegalArgumentException("$address is not IPv6")).let { Ipv6Addr(it.address) }, + address.port.toUShort(), + 0u, + (address.address as java.net.Inet6Address).scopeId.toUInt(), + ) + + override fun toInetSocketAddress(): java.net.InetSocketAddress { + kotlin.require(flowinfo == 0u) { "$this has a flow label, which a java.net.InetSocketAddress has no room for" } + // Java takes a scope of an `Int` and ignores a negative one. + kotlin.require(scopeId <= Int.MAX_VALUE.toUInt()) { "$this has a scope past Int.MAX_VALUE, which a java.net.Inet6Address has no room for" } + val ip = when (scopeId) { + 0u -> ip.toInetAddress() + else -> java.net.Inet6Address.getByAddress(null, ip.octets(), scopeId.toInt()) + } + return java.net.InetSocketAddress(ip, port.toInt()) + } + + override fun equals(other: Any?): Boolean = + other is SocketAddrV6 && ip == other.ip && port == other.port && flowinfo == other.flowinfo && scopeId == other.scopeId + + override fun hashCode(): Int = ((ip.hashCode() * 31 + port.hashCode()) * 31 + flowinfo.hashCode()) * 31 + scopeId.hashCode() + + /** As Rust prints it: `[::1%3]:443`, its scope shown unless 0, its flow label never. */ + override fun toString(): String = if (scopeId == 0u) "[$ip]:$port" else "[$ip%$scopeId]:$port" +} + +/** + * A Rust `PathBuf`: the bytes the OS holds a path as, any bytes on Unix and WTF-8 on Windows, + * so it holds every path exactly, where a `String` holds only one that is UTF-8. + */ +class PathBuf private constructor(bytes: ByteArray, given: Boolean) { + // Rust's own bytes are kept as they are; a caller's are copied, so it never changes. + internal val bytes: ByteArray = if (given) bytes.copyOf() else bytes + + init { + kotlin.require(!given || !_windows || _wtf8(bytes)) { "a path that is not WTF-8 has no Windows form" } + } + + /** The path of the OS's [bytes], copied: on Windows they must be WTF-8, else it throws `IllegalArgumentException`. */ + constructor(bytes: ByteArray) : this(bytes, true) + + /** The path [path] spells, throwing `IllegalArgumentException` for a lone surrogate, which UTF-8 has no form of. */ + constructor(path: String) : this(_utf8(path), false) + + /** Its bytes, copied. */ + fun toByteArray(): ByteArray = bytes.copyOf() + + /** Its text, throwing `IllegalArgumentException` where its bytes are not UTF-8. */ + fun toText(): String = try { + bytes.decodeToString(throwOnInvalidSequence = true) + } catch (e: java.nio.charset.CharacterCodingException) { + throw kotlin.IllegalArgumentException("$this is not UTF-8", e) + } + + /** This as a `java.nio.file.Path`, throwing `IllegalArgumentException` where its bytes are not UTF-8. */ + fun toPath(): java.nio.file.Path = java.nio.file.Paths.get(toText()) + + override fun equals(other: Any?): Boolean = other is PathBuf && bytes.contentEquals(other.bytes) + + override fun hashCode(): Int = bytes.contentHashCode() + + /** Its text, as Rust's `display` shows it: what is not UTF-8 as U+FFFD. */ + override fun toString(): String = bytes.decodeToString() + + internal companion object { + /** The path of bytes Rust returned, kept without a copy. */ + fun of(bytes: ByteArray): PathBuf = PathBuf(bytes, false) + } +} + +/** [octets], throwing `IllegalArgumentException` unless there are [n]. */ +internal fun _octets(octets: ByteArray, n: Int): ByteArray { + kotlin.require(octets.size == n) { "${octets.size} octets, not $n" } + return octets +} + +/** The address of [address], throwing `IllegalArgumentException` for an unresolved one. */ +internal fun _resolved(address: java.net.InetSocketAddress): java.net.InetAddress = + address.address ?: throw kotlin.IllegalArgumentException("$address is unresolved, which Rust's SocketAddr has no form of") + +/** [address], throwing `IllegalArgumentException` for one with a scope. */ +internal fun _unscoped(address: java.net.Inet6Address): java.net.Inet6Address { + kotlin.require(address.scopeId == 0 && address.scopedInterface == null) { "$address has a scope, which Rust's Ipv6Addr has no room for" } + return address +} + +/** [text]'s UTF-8, throwing `IllegalArgumentException` for a lone surrogate, which UTF-8 has no form of. */ +internal fun _utf8(text: String): ByteArray = try { + text.encodeToByteArray(throwOnInvalidSequence = true) +} catch (e: java.nio.charset.CharacterCodingException) { + throw kotlin.IllegalArgumentException("a String with a lone surrogate has no UTF-8", e) +} + +/** Whether the library runs on Windows, whose paths Rust holds as WTF-8. */ +internal val _windows: Boolean = java.io.File.separatorChar == '\\' + +/** + * Whether [bytes] are WTF-8, as Rust encodes a Windows path: UTF-8, but for a surrogate + * alone, encoded as a scalar would be, though never a lead then a trail. + */ +internal fun _wtf8(bytes: ByteArray): Boolean { + var at = 0 + var lead = false + while (at < bytes.size) { + val b = bytes[at].toInt() and 0xff + val (n, min) = when { + b < 0x80 -> 1 to 0 + b in 0xc2..0xdf -> 2 to 0x80 + b in 0xe0..0xef -> 3 to 0x800 + b in 0xf0..0xf4 -> 4 to 0x10000 + else -> return false + } + if (at + n > bytes.size) return false + var c = if (n == 1) b else b and (0x7f shr n) + for (k in 1 until n) { + val next = bytes[at + k].toInt() and 0xff + if (next and 0xc0 != 0x80) return false + c = (c shl 6) or (next and 0x3f) + } + if (c < min || c > 0x10ffff) return false + val surrogate = c in 0xd800..0xdfff + if (surrogate && c >= 0xdc00 && lead) return false + lead = surrogate && c < 0xdc00 + at += n + } + return true +} diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index c4766d2a1..b46673f65 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -16,7 +16,7 @@ use std::collections::{BTreeMap, HashMap}; use rustc_span::Span; -use crate::ir::{Access, Api, Closure, Dyn, Function, Home, Int, Jni, Literal, Mutability, On, Receiver, Type}; +use crate::ir::{Access, Api, Closure, Dyn, Family, Function, Home, Int, Jni, Literal, Mutability, On, Receiver, Type}; /// A C name one item claims twice, which no rename can tell apart: the name, then what /// claimed it first and second. @@ -67,10 +67,10 @@ pub enum Ty { U128, /// `_duration` Duration, - /// `_socket_addr` - SocketAddr, - /// `_ip_addr` - IpAddr, + /// `_socket_addr`, or `_socket_addr_v4` or `_socket_addr_v6` for one family. + SocketAddr(Family), + /// `_ip_addr`, or `_ipv4_addr` or `_ipv6_addr` for one family. + IpAddr(Family), /// A struct: a record, a data enum, an option, or a callback's table and data. Struct(String), /// A plain enum, passed as its C enum, which is an `int`. @@ -124,6 +124,19 @@ impl Sig { } } +/// The runtime's C struct of a socket address, if `socket`, or else an IP address, of +/// `family`, after the prefix: a struct per family, so each holds exactly its Rust type. +pub fn addr(socket: bool, family: Family) -> &'static str { + match (socket, family) { + (true, Family::Any) => "socket_addr", + (true, Family::V4) => "socket_addr_v4", + (true, Family::V6) => "socket_addr_v6", + (false, Family::Any) => "ip_addr", + (false, Family::V4) => "ipv4_addr", + (false, Family::V6) => "ipv6_addr", + } +} + /// Whether `ty` is a `status` parameter's. pub fn is_status(ty: &Ty) -> bool { matches!(ty, Ty::Out(inner) if **inner == Ty::Status) @@ -581,8 +594,6 @@ impl<'a> Abi<'a> { Type::Enum(i) => (&self.api.enums[*i].shown, &self.api.enums[*i].args), Type::Object(i) => (&self.api.objects[*i].shown, &self.api.objects[*i].args), Type::Ref(_, inner) | Type::Box(inner) => return self.arg_forms(inner), - // Apart from its integer's, which an instance of the same item may also take. - Type::NonZero(int) => return vec![format!("non_zero_{}", int.rust())], ty => return vec![self.key(ty)], }; let segments = self.segments(shown); @@ -765,15 +776,17 @@ impl<'a> Abi<'a> { Type::Str | Type::String => "string".to_string(), Type::Slice | Type::Vec | Type::Bytes => "bytes".to_string(), Type::Path | Type::PathBuf => "path".to_string(), - Type::Int(int) | Type::NonZero(int) => int.rust().to_string(), + Type::Int(int) => int.rust().to_string(), + // Apart from its integer's, as a binding checks it is not zero. + Type::NonZero(int) => format!("non_zero_{}", int.rust()), Type::F32 => "f32".to_string(), Type::F64 => "f64".to_string(), Type::Char => "char".to_string(), Type::U128 => "u128".to_string(), Type::Duration => "duration".to_string(), Type::Array(n) => format!("array_u8_{n}"), - Type::SocketAddr(_) => "socket_addr".to_string(), - Type::IpAddr(_) => "ip_addr".to_string(), + Type::SocketAddr(family) => addr(true, *family).to_string(), + Type::IpAddr(family) => addr(false, *family).to_string(), Type::Option(inner) => format!("option_{}", self.key(inner)), Type::List(_, inner) => format!("vec_{}", self.key(inner)), Type::Tuple(elements) => strip(self.tuple(elements)), @@ -834,8 +847,8 @@ impl<'a> Abi<'a> { Type::U128 => Ty::U128, Type::Duration => Ty::Duration, Type::Array(n) => Ty::Struct(self.array(*n)), - Type::SocketAddr(_) => Ty::SocketAddr, - Type::IpAddr(_) => Ty::IpAddr, + Type::SocketAddr(family) => Ty::SocketAddr(*family), + Type::IpAddr(family) => Ty::IpAddr(*family), Type::Waker => Ty::Waker, Type::Future => Ty::ForeignFuture, Type::Reader => Ty::ForeignReader, @@ -1215,7 +1228,11 @@ impl<'a> Abi<'a> { "u128", "duration", "socket_addr", + "socket_addr_v4", + "socket_addr_v6", "ip_addr", + "ipv4_addr", + "ipv6_addr", "waker", "foreign_future", "foreign_reader", @@ -1548,6 +1565,9 @@ impl<'a> Abi<'a> { if traits.eq { claim("eq"); } + if traits.hash { + claim("hash"); + } if traits.display { claim("to_string"); } diff --git a/ffi/src/c.rs b/ffi/src/c.rs index c3737cbcf..9b99976eb 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -94,10 +94,7 @@ impl Header<'_> { "typedef struct {p}_duration {{\n\tuint64_t secs;\n\tuint32_t nanos;\n}} {p}_duration;\n" )); self.line(format!( - "/* A socket address: IPv4 in the first 4 bytes of ip, with the rest of ip, flowinfo,\n * and scope_id zero, or IPv6 if v6. ip is in network order and port in the host's. */\ntypedef struct {p}_socket_addr {{\n\tuint8_t ip[16];\n\tuint16_t port;\n\tbool v6;\n\tuint32_t flowinfo;\n\tuint32_t scope_id;\n}} {p}_socket_addr;\n" - )); - self.line(format!( - "/* An IP address: IPv4 in the first 4 bytes of ip, with the rest zero, or IPv6 if v6.\n * ip is in network order. */\ntypedef struct {p}_ip_addr {{\n\tuint8_t ip[16];\n\tbool v6;\n}} {p}_ip_addr;\n" + "/* An IPv4 address, in network order. */\ntypedef struct {p}_ipv4_addr {{\n\tuint8_t octets[4];\n}} {p}_ipv4_addr;\n\n/* An IPv6 address, in network order. */\ntypedef struct {p}_ipv6_addr {{\n\tuint8_t octets[16];\n}} {p}_ipv6_addr;\n\n/* An IP address: ip.v6 if v6, else ip.v4. */\ntypedef struct {p}_ip_addr {{\n\tbool v6;\n\tunion {{\n\t\t{p}_ipv4_addr v4;\n\t\t{p}_ipv6_addr v6;\n\t}} ip;\n}} {p}_ip_addr;\n\n/* An IPv4 socket address, its port in the host's order. */\ntypedef struct {p}_socket_addr_v4 {{\n\t{p}_ipv4_addr ip;\n\tuint16_t port;\n}} {p}_socket_addr_v4;\n\n/* An IPv6 socket address, its port in the host's order. */\ntypedef struct {p}_socket_addr_v6 {{\n\t{p}_ipv6_addr ip;\n\tuint16_t port;\n\tuint32_t flowinfo;\n\tuint32_t scope_id;\n}} {p}_socket_addr_v6;\n\n/* A socket address: addr.v6 if v6, else addr.v4. */\ntypedef struct {p}_socket_addr {{\n\tbool v6;\n\tunion {{\n\t\t{p}_socket_addr_v4 v4;\n\t\t{p}_socket_addr_v6 v6;\n\t}} addr;\n}} {p}_socket_addr;\n" )); self.line(format!( "/* Wakes a future's poller: wake may be called from any thread, any number of\n * times, until drop releases data. A poll passing the same wake and data again\n * reuses the waker. Either function may be null. One Rust returns is the caller's,\n * to drop once or pass back into Rust. */\ntypedef struct {p}_waker {{\n\tvoid (*wake)(void *data);\n\tvoid (*drop)(void *data);\n\tvoid *data;\n}} {p}_waker;\n" @@ -275,6 +272,9 @@ impl Header<'_> { if t.eq { self.line(format!("bool {name}_eq(const {name} *a, const {name} *b{status});")); } + if t.hash { + self.line(format!("uint64_t {name}_hash(const {name} *self{status});")); + } if t.display { self.line(format!("{p}_buf {name}_to_string(const {name} *self{status});")); } @@ -440,6 +440,9 @@ impl Header<'_> { if traits.eq { self.line(format!("bool {name}_eq({a}, {b}{status});")); } + if traits.hash { + self.line(format!("uint64_t {name}_hash({this}{status});")); + } if traits.display { self.line(format!("{p}_buf {name}_to_string({this}{status});")); } @@ -559,8 +562,8 @@ pub fn decl(abi: &Abi<'_>, ty: &Ty, name: &str) -> String { Ty::Buf => space(&abi.rt("buf")), Ty::U128 => space(&abi.rt("u128")), Ty::Duration => space(&abi.rt("duration")), - Ty::SocketAddr => space(&abi.rt("socket_addr")), - Ty::IpAddr => space(&abi.rt("ip_addr")), + Ty::SocketAddr(family) => space(&abi.rt(crate::abi::addr(true, *family))), + Ty::IpAddr(family) => space(&abi.rt(crate::abi::addr(false, *family))), Ty::Struct(n) | Ty::Enum(n) => space(n), Ty::Handle { name: n, constant } => match constant { true => format!("const {n} *{name}"), diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 0b1a5e8c1..652e5254c 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1812,6 +1812,7 @@ impl<'tcx> Collector<'tcx> { traits: Traits { clone: true, eq: true, + hash: true, display: true, debug: false, default: false, @@ -1928,6 +1929,7 @@ impl<'tcx> Collector<'tcx> { Traits { clone: true, eq: true, + hash: true, debug: true, ..Traits::default() }, @@ -1938,6 +1940,7 @@ impl<'tcx> Collector<'tcx> { Traits { clone: true, eq: true, + hash: true, display: true, debug: true, default: false, @@ -2735,7 +2738,7 @@ impl<'tcx> Collector<'tcx> { .must_apply_modulo_regions() }) }; - let traits = Traits { + let mut traits = Traits { clone: implements(sym::Clone), eq: tcx.lang_items().eq_trait().is_some_and(|tr| { let (infcx, env) = tcx.infer_ctxt().build_with_typing_env(env); @@ -2743,10 +2746,13 @@ impl<'tcx> Collector<'tcx> { .type_implements_trait(tr, [self_ty, self_ty], env) .must_apply_modulo_regions() }), + hash: implements(sym::Hash), display: implements(sym::Display), debug: implements(sym::Debug), default: implements(sym::Default), }; + // A hash is only of use beside the equality it agrees with. + traits.hash &= traits.eq; (methods, traits) } diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 313e73b1f..ef669edcb 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -25,7 +25,7 @@ use std::collections::{BTreeMap, BTreeSet}; use crate::abi::{self, Abi, Ty}; use crate::c::decl; -use crate::ir::{Closure, Function, Int, Literal, Receiver, Traits, Type}; +use crate::ir::{Closure, Family, Function, Int, Literal, Receiver, Traits, Type}; const RUNTIME: &str = include_str!("../runtime/rt.hpp"); @@ -48,6 +48,7 @@ pub fn headers(abi: &Abi<'_>, packages: &str) -> Result { files: crate::go::packages(abi), out: String::new(), defs: String::new(), + hashes: String::new(), defined: BTreeSet::new(), globals: runtime_names().map(str::to_string).collect(), errors: Vec::new(), @@ -107,6 +108,8 @@ struct Header<'a> { out: String, /// The definitions, after every type is complete. defs: String, + /// `std::hash` of each class Rust hashes, after the namespace. + hashes: String, /// The value types written so far, so each is written after what it holds. defined: BTreeSet, /// The names declared in the namespace, which a parameter named alike would hide. @@ -173,8 +176,8 @@ impl<'a> Header<'a> { } Type::U128 => format!("::{}::u128", self.ns), Type::Duration => format!("::{}::duration", self.ns), - Type::SocketAddr(_) => format!("::{}::socket_addr", self.ns), - Type::IpAddr(_) => format!("::{}::ip_addr", self.ns), + Type::SocketAddr(family) => format!("::{}::{}", self.ns, crate::abi::addr(true, *family)), + Type::IpAddr(family) => format!("::{}::{}", self.ns, crate::abi::addr(false, *family)), Type::List(_, inner) => format!("::std::vector<{}>", self.owned(inner)), Type::Waker => format!("::{}::waker", self.ns), Type::Option(inner) => format!("::std::optional<{}>", self.owned(inner)), @@ -535,6 +538,7 @@ impl<'a> Header<'a> { "cstdio", "cstdlib", "cstring", + "functional", "initializer_list", "ios", "memory", @@ -565,9 +569,13 @@ impl<'a> Header<'a> { let mut aliases = vec![ ("u128".to_string(), format!("{p}_u128")), ("duration".to_string(), format!("{p}_duration")), - ("socket_addr".to_string(), format!("{p}_socket_addr")), - ("ip_addr".to_string(), format!("{p}_ip_addr")), ]; + for socket in [true, false] { + for family in [Family::Any, Family::V4, Family::V6] { + let name = crate::abi::addr(socket, family); + aliases.push((name.to_string(), format!("{p}_{name}"))); + } + } if abi.ranges.values().any(|ty| matches!(ty, Type::Bound(_))) { aliases.push(("bound".to_string(), abi.rt("bound"))); } @@ -761,6 +769,8 @@ impl<'a> Header<'a> { let defs = std::mem::take(&mut self.defs); self.out.push_str(&defs); self.line(format!("}} // namespace {ns}\n")); + let hashes = std::mem::take(&mut self.hashes); + self.out.push_str(&hashes); self.line(format!("#endif // {guard}")); std::mem::take(&mut self.out) } @@ -934,6 +944,20 @@ impl<'a> Header<'a> { "\tfriend bool operator==(const {name} &{a}, const {name} &{b}) {{\n\t\treturn ::{c}_eq({a}.raw_.get(), {b}.raw_.get(){status});\n\t}}\n\tfriend bool operator!=(const {name} &{a}, const {name} &{b}) {{\n\t\treturn !({a} == {b});\n\t}}" )); } + if t.hash { + let hash = apart("hash", &name); + body.push(format!( + "\t/// Rust's `Hash`, agreeing with `==`, as `std::hash` hashes it.\n\tRUST2_FFI_CALLABLE ::std::uint64_t {hash}() const;" + )); + self.def(format!( + "inline ::std::uint64_t {name}::{hash}() const {{\n\treturn ::{c}_hash(raw_.get(){status});\n}}\n" + )); + members.push(hash.clone()); + let q = format!("::{}::{name}", self.ns); + self.hashes.push_str(&format!( + "namespace std {{\ntemplate <>\nstruct hash<{q}> {{\n\tsize_t operator()(const {q} &value) const noexcept {{\n\t\treturn static_cast(value.{hash}());\n\t}}\n}};\n}} // namespace std\n\n" + )); + } self.traits(&name, &c, t, Ok(status), &mut body, &mut members); let owner = Ty::Handle { name: c.clone(), diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 2a448dc50..4b1c16605 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -329,23 +329,33 @@ const TAKEN: &[&str] = &[ "BigInt", "Cancelable", "Cancelled", + "Comparable", "Duration", "Exception", + "Expando", "FormatException", "Function", "Future", + "IpAddr", + "Ipv4Addr", + "Ipv6Addr", "List", "Map", "Mut", "Never", "Object", + "PathBuf", "RangeError", + "RustDuration", "SeekFrom", "SeekableReader", "SocketAddr", + "SocketAddrV4", + "SocketAddrV6", "StackTrace", "StateError", "String", + "U64", "UnsupportedError", "Waker", "bool", @@ -553,14 +563,17 @@ impl<'a> Gen<'a> { Type::Array(n) => Abi::array_class(*n), Type::Unit => "void".to_string(), Type::Bool => "bool".to_string(), - Type::Int(_) | Type::NonZero(_) => "int".to_string(), + Type::Int(int) | Type::NonZero(int) => int_dt(*int).to_string(), Type::F32 | Type::F64 => "double".to_string(), - Type::Char | Type::Str | Type::String | Type::Path | Type::PathBuf => "String".to_string(), + Type::Char | Type::Str | Type::String => "String".to_string(), + Type::Path | Type::PathBuf => "PathBuf".to_string(), Type::Slice | Type::Vec | Type::Bytes => "typed.Uint8List".to_string(), Type::U128 => "BigInt".to_string(), - Type::Duration => "Duration".to_string(), - Type::SocketAddr(_) => "SocketAddr".to_string(), - Type::IpAddr(_) => "io.InternetAddress".to_string(), + Type::Duration => "RustDuration".to_string(), + Type::SocketAddr(family) => addr_class(true, *family).to_string(), + Type::IpAddr(family) => addr_class(false, *family).to_string(), + // `u64`'s block is a `Uint64List` read as `U64`, which an `int` stands for. + Type::List(..) if ty.block() == Some(&Type::Int(Int::U64)) => "List".to_string(), Type::List(..) if let Some(elem) = ty.block() => format!("typed.{}", typed_list(elem)), Type::List(_, inner) => format!("List<{}>", self.dt(inner)), Type::Waker => "void Function()".to_string(), @@ -641,8 +654,8 @@ impl<'a> Gen<'a> { Ty::Buf | Ty::U128 | Ty::Duration - | Ty::SocketAddr - | Ty::IpAddr + | Ty::SocketAddr(_) + | Ty::IpAddr(_) | Ty::Struct(_) | Ty::Waker | Ty::ForeignFuture @@ -657,8 +670,8 @@ impl<'a> Gen<'a> { Ty::Buf => return "_Buf".to_string(), Ty::U128 => return "_U128".to_string(), Ty::Duration => return "_Duration".to_string(), - Ty::SocketAddr => return "_Addr".to_string(), - Ty::IpAddr => return "_IpAddr".to_string(), + Ty::SocketAddr(family) => return format!("_{}", addr_class(true, *family)), + Ty::IpAddr(family) => return format!("_{}", addr_class(false, *family)), Ty::Waker => return "_Waker".to_string(), Ty::ForeignFuture => return "_ForeignFuture".to_string(), Ty::ForeignReader => return "_ForeignReader".to_string(), @@ -930,14 +943,18 @@ impl<'a> Gen<'a> { fn lower(&mut self, ty: &Type, e: &str) -> String { match ty { Type::Bool | Type::F32 | Type::F64 => e.to_string(), - Type::Int(int) | Type::NonZero(int) => match int { + Type::NonZero(int) => format!("_n.nonZero({})", self.lower(&Type::Int(*int), e)), + Type::Int(int) => match int { Int::U8 => format!("_n.u8({e})"), Int::U16 => format!("_n.u16({e})"), Int::U32 => format!("_n.u32({e})"), Int::I8 => format!("_n.i8({e})"), Int::I16 => format!("_n.i16({e})"), Int::I32 => format!("_n.i32({e})"), - _ => e.to_string(), + Int::U64 => format!("{e}.bits"), + Int::Usize => format!("_n.usize({e})"), + Int::Isize => format!("_n.isize({e})"), + Int::I64 => e.to_string(), }, Type::Char => format!("_n.char({e})"), Type::Enum(_) => format!("{e}._c"), @@ -958,31 +975,19 @@ impl<'a> Gen<'a> { return format!("{dst} = {};", self.lower(ty, e)); } match ty.base() { - Type::Str | Type::String | Type::Path | Type::PathBuf => format!("a_.string({dst}, {e}, {take});"), + Type::Str | Type::String => format!("a_.string({dst}, {e}, {take});"), + Type::Path | Type::PathBuf => format!("a_.bytes({dst}, {e}._bytes, {take});"), Type::Slice | Type::Vec | Type::Bytes => format!("a_.bytes({dst}, {e}, {take});"), Type::U128 => format!("_n.u128({dst}, {e});"), - Type::Duration => format!("_n.duration({dst}, {e});"), + Type::Duration => format!("{e}._put({dst});"), Type::Array(_) => format!("{e}._write({dst});"), - Type::SocketAddr(family) => { - let family = match family { - Family::Any => "SocketAddr", - Family::V4 => "SocketAddrV4", - Family::V6 => "SocketAddrV6", - }; - format!("_n.addr({dst}, {e}, '{family}');") - } - Type::IpAddr(family) => { - let family = match family { - Family::Any => "IpAddr", - Family::V4 => "Ipv4Addr", - Family::V6 => "Ipv6Addr", - }; - format!("_n.ip({dst}, {e}, '{family}');") - } + // The sealed class of either family writes which it is, and each family itself. + Type::SocketAddr(Family::Any) => format!("SocketAddr._put({dst}, {e});"), + Type::IpAddr(Family::Any) => format!("IpAddr._put({dst}, {e});"), + Type::SocketAddr(_) | Type::IpAddr(_) => format!("{e}._put({dst});"), Type::Option(inner) => match inner.base() { - Type::Str | Type::String | Type::Path | Type::PathBuf => { - format!("a_.string({dst}, {e}, {take});") - } + Type::Str | Type::String => format!("a_.string({dst}, {e}, {take});"), + Type::Path | Type::PathBuf => format!("a_.bytes({dst}, {e}?._bytes, {take});"), Type::Slice | Type::Vec | Type::Bytes => format!("a_.bytes({dst}, {e}, {take});"), Type::List(..) => { let c = self.c_name(inner); @@ -1080,11 +1085,15 @@ impl<'a> Gen<'a> { let some = self.write(inner, "v", "c.$value", "t"); format!("if (v == null) {{\n\t\tc.$some = false;\n\t}} else {{\n\t\tc.$some = true;\n\t\t{some}\n\t}}") } - // A block is copied whole into the arena. + // A block is copied whole into the arena: `U64`s as the `int`s they are. Type::List(_, elem) if ty.block().is_some() => { let nt = self.nt(&abi.ty(elem)); + let v = match **elem { + Type::Int(Int::U64) => "v as List", + _ => "v", + }; format!( - "if (v == null) {{\n\t\tc.$ptr = ffi.nullptr;\n\t\tc.$len = 0;\n\t\tc.$owner = ffi.nullptr;\n\t\treturn;\n\t}}\n\tfinal p = a_<{nt}>(v.isEmpty ? 1 : v.length);\n\tp.asTypedList(v.length).setAll(0, v);\n\tc.$ptr = p;\n\tc.$len = v.length;\n\tc.$owner = ffi.nullptr;" + "if (v == null) {{\n\t\tc.$ptr = ffi.nullptr;\n\t\tc.$len = 0;\n\t\tc.$owner = ffi.nullptr;\n\t\treturn;\n\t}}\n\tfinal p = a_<{nt}>(v.isEmpty ? 1 : v.length);\n\tp.asTypedList(v.length).setAll(0, {v});\n\tc.$ptr = p;\n\tc.$len = v.length;\n\tc.$owner = ffi.nullptr;" ) } Type::List(_, elem) => { @@ -1225,10 +1234,14 @@ impl<'a> Gen<'a> { let class = self.struct_class(&Ty::Struct(c.to_string())); let list = typed_list(block); let free = self.native_fn(&format!("{c}_free"), vec![Ty::Struct(c.to_string())], Ty::Void); + let cast = match block { + Type::Int(Int::U64) => " as List", + _ => "", + }; return self.finish_reader( name, format!( - "{dt} {{NAME}}({class} c) {{\n\tfinal out = typed.{list}.fromList(c.$ptr.asTypedList(c.$len));\n\t{free}(c);\n\treturn out;\n}}\n\n" + "{dt} {{NAME}}({class} c) {{\n\tfinal out = typed.{list}.fromList(c.$ptr.asTypedList(c.$len));\n\t{free}(c);\n\treturn out{cast};\n}}\n\n" ), ); } @@ -1255,16 +1268,19 @@ impl<'a> Gen<'a> { }; (class, body) } - Type::Range(_) => { + Type::Range(int) => { let class = self.struct_class(&Ty::Struct(c.to_string())); - (class, format!("{dt}(c.$start, c.$end)")) + let int = Type::Int(*int); + let (start, end) = (self.lift(&int, "c.$start"), self.lift(&int, "c.$end")); + (class, format!("{dt}({start}, {end})")) } - Type::Bound(_) => { + Type::Bound(int) => { let class = self.struct_class(&Ty::Struct(c.to_string())); + let value = self.lift(&Type::Int(*int), "c.$value"); ( class, format!( - "switch (c.$kind) {{\n\t\t0 => const {dt}Unbounded(),\n\t\t1 => {dt}Included(c.$value),\n\t\t2 => {dt}Excluded(c.$value),\n\t\t_ => throw StateError('{c}: an invalid kind'),\n\t}}" + "switch (c.$kind) {{\n\t\t0 => const {dt}Unbounded(),\n\t\t1 => {dt}Included({value}),\n\t\t2 => {dt}Excluded({value}),\n\t\t_ => throw StateError('{c}: an invalid kind'),\n\t}}" ), ) } @@ -1291,22 +1307,23 @@ impl<'a> Gen<'a> { let abi = self.abi; match ty { Type::Str | Type::String => format!("_n.string({e})"), - Type::Path | Type::PathBuf => format!("_n.path({e})"), + Type::Path | Type::PathBuf => format!("PathBuf._(_n.bytes({e}))"), Type::Slice | Type::Vec | Type::Bytes => format!("_n.bytes({e})"), + Type::Int(Int::U64 | Int::Usize) | Type::NonZero(Int::U64 | Int::Usize) => format!("U64.fromBits({e})"), Type::Bool | Type::F32 | Type::F64 | Type::Int(_) | Type::NonZero(_) => e.to_string(), Type::Char => format!("_n.scalar({e})"), Type::U128 => format!("_n.u128Of({e})"), - Type::Duration => format!("_n.durationOf({e})"), + Type::Duration => format!("RustDuration._of({e})"), Type::Array(n) => format!("{}._of({e})", Abi::array_class(*n)), - Type::SocketAddr(_) => format!("_n.addrOf({e})"), - Type::IpAddr(_) => format!("_n.ipOf({e})"), + Type::SocketAddr(family) => format!("{}._of({e})", addr_class(true, *family)), + Type::IpAddr(family) => format!("{}._of({e})", addr_class(false, *family)), Type::Option(inner) => match inner.base() { Type::Object(_) => { let dt = self.dt(inner); format!("({e} == ffi.nullptr ? null : {dt}._({e}))") } Type::Str | Type::String => format!("_n.stringOrNull({e})"), - Type::Path | Type::PathBuf => format!("_n.pathOrNull({e})"), + Type::Path | Type::PathBuf => format!("({e}.ptr == ffi.nullptr ? null : PathBuf._(_n.bytes({e})))"), Type::Slice | Type::Vec | Type::Bytes => format!("_n.bytesOrNull({e})"), Type::List(..) => { let lifted = self.lift(inner, e); @@ -1344,11 +1361,13 @@ impl<'a> Gen<'a> { fn lift_lent(&mut self, ty: &Type, e: &str) -> String { match ty { Type::Str | Type::String => format!("_n.stringLent({e})"), - Type::Path | Type::PathBuf => format!("_n.pathLent({e})"), + Type::Path | Type::PathBuf => format!("PathBuf._(_n.bytesLent({e}))"), Type::Slice | Type::Vec | Type::Bytes => format!("_n.bytesLent({e})"), Type::Option(inner) => match inner.base() { Type::Str | Type::String => format!("_n.stringLentOrNull({e})"), - Type::Path | Type::PathBuf => format!("_n.pathLentOrNull({e})"), + Type::Path | Type::PathBuf => { + format!("({e}.ptr == ffi.nullptr ? null : PathBuf._(_n.bytesLent({e})))") + } Type::Slice | Type::Vec | Type::Bytes => format!("_n.bytesLentOrNull({e})"), _ => self.lift(ty, e), }, @@ -1378,6 +1397,7 @@ impl<'a> Gen<'a> { .insert(format!("const {}", constant.path.join("::")), name.clone()); let value = match constant.value { Literal::Bool(b) => b.to_string(), + Literal::Int(Int::U64 | Int::Usize, v) => format!("U64.fromBits({})", int_literal(v)), Literal::Int(_, v) => int_literal(v), }; let _ = write!(self.top, "{}const {name} = {value};\n\n", docs(&constant.docs, "")); @@ -1426,15 +1446,24 @@ impl<'a> Gen<'a> { fn range(&mut self, c: &str, ty: &Type) { let name = self.class(c); let code = match ty { - Type::Range(_) => format!( - "/// Rust's `Range`: from `start`, up to but not including `end`.\nfinal class {name} {{\n\tfinal int start;\n\tfinal int end;\n\n\tconst {name}(this.start, this.end);\n\n\t@override\n\tbool operator ==(Object other) => other is {name} && other.start == start && other.end == end;\n\n\t@override\n\tint get hashCode => Object.hash(start, end);\n\n\t@override\n\tString toString() => '$start..$end';\n}}\n\n" - ), - Type::Bound(_) => { + Type::Range(int) => { + let dt = int_dt(*int); + // A `U64` prints unsigned only through its own method. + let (start, end) = match dt { + "U64" => ("${start.toRadixString(10)}", "${end.toRadixString(10)}"), + _ => ("$start", "$end"), + }; + format!( + "/// Rust's `Range`: from `start`, up to but not including `end`.\nfinal class {name} {{\n\tfinal {dt} start;\n\tfinal {dt} end;\n\n\tconst {name}(this.start, this.end);\n\n\t@override\n\tbool operator ==(Object other) => other is {name} && other.start == start && other.end == end;\n\n\t@override\n\tint get hashCode => Object.hash(start, end);\n\n\t@override\n\tString toString() => '{start}..{end}';\n}}\n\n" + ) + } + Type::Bound(int) => { + let dt = int_dt(*int); format!( "/// Rust's `Bound`: what a range's start or end bound does.\nsealed class {name} {{\n\tconst {name}();\n}}\n\n\ final class {name}Unbounded extends {name} {{\n\tconst {name}Unbounded();\n\n\t@override\n\tbool operator ==(Object other) => other is {name}Unbounded;\n\n\t@override\n\tint get hashCode => 0;\n}}\n\n\ - final class {name}Included extends {name} {{\n\tfinal int value;\n\n\tconst {name}Included(this.value);\n\n\t@override\n\tbool operator ==(Object other) => other is {name}Included && other.value == value;\n\n\t@override\n\tint get hashCode => Object.hash(1, value);\n}}\n\n\ - final class {name}Excluded extends {name} {{\n\tfinal int value;\n\n\tconst {name}Excluded(this.value);\n\n\t@override\n\tbool operator ==(Object other) => other is {name}Excluded && other.value == value;\n\n\t@override\n\tint get hashCode => Object.hash(2, value);\n}}\n\n" + final class {name}Included extends {name} {{\n\tfinal {dt} value;\n\n\tconst {name}Included(this.value);\n\n\t@override\n\tbool operator ==(Object other) => other is {name}Included && other.value == value;\n\n\t@override\n\tint get hashCode => Object.hash(1, value);\n}}\n\n\ + final class {name}Excluded extends {name} {{\n\tfinal {dt} value;\n\n\tconst {name}Excluded(this.value);\n\n\t@override\n\tbool operator ==(Object other) => other is {name}Excluded && other.value == value;\n\n\t@override\n\tint get hashCode => Object.hash(2, value);\n}}\n\n" ) } Type::Bounds(int) => { @@ -1449,8 +1478,8 @@ impl<'a> Gen<'a> { } /// The members a type's std traits export: `Display` is `toString()`, `Debug` - /// `debug()`, `PartialEq` `==`, `Default` a constructor `default_()`, and `Clone` an - /// object's `clone()`. + /// `debug()`, `PartialEq` `==` and with `Hash` `hashCode`, `Default` a constructor + /// `default_()`, and `Clone` an object's `clone()`. fn traits(&mut self, ty: &Type, c: &str, t: crate::ir::Traits, body: &mut Vec, members: &mut Vec) { let name = self.dt(ty); let object = matches!(ty, Type::Object(_)); @@ -1525,10 +1554,24 @@ impl<'a> Gen<'a> { let f = self.served_fn(&what, params(vec![recv.clone(), recv.clone()]), Ty::Bool); let (pa, a) = this(self, "this"); let (pb, b) = this(self, "other"); - let call = call(&f, &format!("{a}, {b}"), &what, &|r| r.to_string()); + let eq = call(&f, &format!("{a}, {b}"), &what, &|r| r.to_string()); body.push(format!( - "\t/// Rust's `PartialEq`.\n\t@override\n\tbool operator ==(Object other) {{\n\t\tif (other is! {name}) return false;\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pa}{pb}{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n\n\t/// Rust has no `Hash` for it, so it keys no `Set` or `Map`.\n\t@override\n\tint get hashCode => throw UnsupportedError('{name} has no Rust Hash');\n" + "\t/// Rust's `PartialEq`.\n\t@override\n\tbool operator ==(Object other) {{\n\t\tif (other is! {name}) return false;\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pa}{pb}{eq}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); + // Equal values hash alike only by Rust's own `Hash`, which a type without one lacks. + if t.hash { + let what = format!("{c}_hash"); + let f = self.served_fn(&what, params(vec![recv.clone()]), Ty::Int(Int::U64)); + let (pre, arg) = this(self, "this"); + let hash = call(&f, &arg, &what, &|r| r.to_string()); + body.push(format!( + "\t/// Rust's `Hash`.\n\t@override\n\tint get hashCode {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}{hash}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + )); + } else { + body.push(format!( + "\t/// Rust has no `Hash` for it, so it keys no `Set` or `Map`.\n\t@override\n\tint get hashCode => throw UnsupportedError('{name} has no Rust Hash');\n" + )); + } } if t.default { members.push("new default_".to_string()); @@ -2551,8 +2594,8 @@ fn thrown_value(thrown: &Thrown, lifted: &str) -> String { } /// The `dart:typed_data` list holding a block's element exactly, bit for bit: a -/// `Uint64List` reads a `u64` past `i64::MAX` as the negative `int` of its bits, as every -/// `u64` crosses Dart. +/// `Uint64List` reads a `u64` past `i64::MAX` as the negative `int` of its bits, which a +/// `List` it stands for reads unsigned. fn typed_list(elem: &Type) -> &'static str { match elem { Type::Int(Int::U8) => "Uint8List", @@ -2569,6 +2612,27 @@ fn typed_list(elem: &Type) -> &'static str { } } +/// The Dart type of a Rust integer: `int` but for `u64` and `usize`, whose values past +/// `i64::MAX` it holds as negative, which `U64` reads unsigned. +fn int_dt(int: Int) -> &'static str { + match int { + Int::U64 | Int::Usize => "U64", + _ => "int", + } +} + +/// The runtime's class of a socket address, or else an IP address, of `family`. +fn addr_class(socket: bool, family: Family) -> &'static str { + match (socket, family) { + (true, Family::Any) => "SocketAddr", + (true, Family::V4) => "SocketAddrV4", + (true, Family::V6) => "SocketAddrV6", + (false, Family::Any) => "IpAddr", + (false, Family::V4) => "Ipv4Addr", + (false, Family::V6) => "Ipv6Addr", + } +} + /// The `dart:ffi` type of a C integer. fn native_int(int: Int) -> &'static str { match int { @@ -2678,3 +2742,52 @@ fn indent(code: &str) -> String { } out } + +#[cfg(test)] +mod tests { + use super::*; + + /// Every type the runtime names unqualified is its own or [`TAKEN`], so no exported + /// item shadows it. + #[test] + fn runtime_names_taken() { + let mut code = String::new(); + for line in RUNTIME.lines() { + let line = line.split_once("//").map_or(line, |(code, _)| code); + // A string literal names nothing. + let mut quote = None; + for c in line.chars() { + match quote { + Some(q) if c == q => quote = None, + Some(_) => {} + None if c == '\'' || c == '"' => quote = Some(c), + None => code.push(c), + } + } + code.push('\n'); + } + // A capitalized word no `.` qualifies, nor part of a longer one. + let words: Vec<&str> = (code.char_indices()) + .filter(|&(i, c)| { + c.is_ascii_uppercase() && !code[..i].ends_with(|p: char| p == '.' || p == '_' || p.is_alphanumeric()) + }) + .map(|(i, _)| { + let rest = &code[i..]; + &rest[..rest + .find(|c: char| !(c == '_' || c.is_alphanumeric())) + .unwrap_or(rest.len())] + }) + .collect(); + let declared: BTreeSet<&str> = ["class ", "enum ", "type ", "typedef "] + .iter() + .flat_map(|kind| code.split(kind).skip(1)) + .filter_map(|rest| rest.split(|c: char| !(c == '_' || c.is_alphanumeric())).next()) + .collect(); + for word in words { + // A one-letter name is a type parameter. + if word.len() > 1 && !declared.contains(word) { + assert!(TAKEN.contains(&word), "the runtime's `{word}` is not in TAKEN"); + } + } + } +} diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 339bf54bc..8ede644a3 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -104,8 +104,12 @@ pub fn rs(ty: &Ty) -> String { Ty::Buf => "rt::Buf".to_string(), Ty::U128 => "rt::U128".to_string(), Ty::Duration => "rt::Duration".to_string(), - Ty::SocketAddr => "rt::SocketAddr".to_string(), - Ty::IpAddr => "rt::IpAddr".to_string(), + Ty::SocketAddr(Family::Any) => "rt::SocketAddr".to_string(), + Ty::SocketAddr(Family::V4) => "rt::SocketAddrV4".to_string(), + Ty::SocketAddr(Family::V6) => "rt::SocketAddrV6".to_string(), + Ty::IpAddr(Family::Any) => "rt::IpAddr".to_string(), + Ty::IpAddr(Family::V4) => "rt::Ipv4Addr".to_string(), + Ty::IpAddr(Family::V6) => "rt::Ipv6Addr".to_string(), Ty::Struct(name) => name.clone(), // A C enum is an `int`. Ty::Enum(_) | Ty::Poll | Ty::Status => "i32".to_string(), @@ -864,9 +868,8 @@ impl Gen<'_> { Type::Path | Type::PathBuf => format!("unsafe {{ {e}.path_in(w, {take}) }}"), Type::U128 => format!("{e}.lift()"), Type::Array(_) => format!("{e}.items"), - Type::Duration | Type::SocketAddr(Family::Any) | Type::IpAddr(Family::Any) => format!("{e}.lift(w)"), - Type::SocketAddr(Family::V4) | Type::IpAddr(Family::V4) => format!("{e}.lift_v4(w)"), - Type::SocketAddr(Family::V6) | Type::IpAddr(Family::V6) => format!("{e}.lift_v6(w)"), + Type::Duration => format!("{e}.lift(w)"), + Type::SocketAddr(_) | Type::IpAddr(_) => format!("{e}.lift()"), Type::Waker => format!("{e}.lift()"), Type::Future | Type::Reader => format!("{e}.lift(w)"), Type::Jni(_) => unreachable!("a JNI value is lent by `function`"), @@ -1108,8 +1111,7 @@ impl Gen<'_> { Type::U128 => format!("rt::U128::lower({e})"), Type::Duration => format!("rt::Duration::lower({e})"), Type::Array(n) => format!("{} {{ items: {e} }}", self.abi.array(*n)), - Type::SocketAddr(_) => format!("rt::SocketAddr::lower(::std::net::SocketAddr::from({e}))"), - Type::IpAddr(_) => format!("rt::IpAddr::lower(::std::net::IpAddr::from({e}))"), + Type::SocketAddr(_) | Type::IpAddr(_) => format!("{}::lower({e})", rs(&self.abi.ty(ty))), // A block hands over its own allocation. Type::List(Collection::Box | Collection::Array(_), _) if ty.block().is_some() => { format!("rt::List::owned(::std::vec::Vec::from({e}))") @@ -1389,6 +1391,12 @@ impl Gen<'_> { "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" )); } + if traits.hash { + let body = wrap(status, "u64", &format!("let this = {this};\n\trt::hash(&*this)")); + self.export(format!( + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_hash(this: {handle}{param}) -> u64 {{\n\tlet w = \"{name}_hash\";\n\t{body}\n}}\n" + )); + } if traits.display { let body = wrap( status, @@ -1438,6 +1446,12 @@ impl Gen<'_> { "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: *const {c}, b: *const {c}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" )); } + if traits.hash { + let body = wrap(status, "u64", &format!("rt::hash::<{path}>(&{lift})")); + self.export(format!( + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_hash(this: *const {c}{param}) -> u64 {{\n\tlet w = \"{name}_hash\";\n\t{body}\n}}\n" + )); + } if traits.display { let body = wrap( status, @@ -2043,8 +2057,8 @@ impl Gen<'_> { format!("{local}.bytes()"), ), (Type::Path, Mutability::Not, false) => ( - format!("let {local} = unsafe {{ {c}.hold(w) }};"), - format!("{local}.path(w)"), + format!("let {local} = unsafe {{ {c}.hold_path(w) }};"), + format!("{local}.path()"), ), // A slice of a block is borrowed in place, as bytes are. (list @ Type::List(Collection::Slice, _), Mutability::Not, false) if list.block().is_some() => { @@ -2082,13 +2096,15 @@ impl Gen<'_> { ) } (Type::Str | Type::Slice | Type::Path, false) => { - let view = match &**inner { - Type::Str => "v.str(w)", - Type::Slice => "v.bytes()", - _ => "v.path(w)", + let (hold, view) = match &**inner { + Type::Str => ("hold", "v.str(w)"), + Type::Slice => ("hold", "v.bytes()"), + _ => ("hold_path", "v.path()"), }; ( - format!("let {local} = if {c}.is_none() {{ None }} else {{ Some(unsafe {{ {c}.hold(w) }}) }};"), + format!( + "let {local} = if {c}.is_none() {{ None }} else {{ Some(unsafe {{ {c}.{hold}(w) }}) }};" + ), format!("{local}.as_ref().map(|v| {view})"), ) } diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 53771c5d2..87807da1b 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -40,20 +40,17 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::fmt::Write; use crate::abi::{self, Abi, Ty, pascal}; -use crate::ir::{Access, Closure, Collection, Family, Function, Home, Int, Literal, On, Param, Receiver, Type}; +use crate::ir::{Access, Closure, Collection, Function, Home, Int, Literal, On, Param, Receiver, Type}; const RUNTIME: &str = include_str!("../runtime/rt.go"); const CONVERSIONS: &str = include_str!("../runtime/ffi_cgo.go"); /// The standard library's packages the generated items name. const STD: &[(&str, &str)] = &[ - ("big", "math/big"), ("context", "context"), ("cgo", "runtime/cgo"), ("io", "io"), - ("netip", "net/netip"), ("runtime", "runtime"), - ("time", "time"), ("unsafe", "unsafe"), ]; @@ -366,6 +363,29 @@ const TAKEN: &[&str] = &[ "NewBytes", "ErrBorrowed", "ErrClosed", + "ErrInvalid", + "Invalid_", + "Duration", + "NewDuration", + "DurationOf", + "U128", + "U128Of", + "Ipv4Addr", + "Ipv4AddrOf", + "Ipv6Addr", + "Ipv6AddrOf", + "IpAddr", + "IpAddrOf", + "IpAddrFromV4", + "IpAddrFromV6", + "SocketAddrV4", + "SocketAddrV4Of", + "SocketAddrV6", + "SocketAddrV6Of", + "SocketAddr", + "SocketAddrOf", + "SocketAddrFromV4", + "SocketAddrFromV6", "Handle_", "Slot_", "LiftBytes_", @@ -573,9 +593,9 @@ impl<'a> Gen<'a> { "// Code generated by rust2-ffi. DO NOT EDIT.\n\n\ package {name}\n\n\ /*\n#cgo CFLAGS: -I${{SRCDIR}}/../../include\n#include \n#include \"{p}.h\"\n*/\nimport \"C\"\n\n\ - import (\n\t\"context\"\n\t\"math\"\n\t\"math/big\"\n\t\"net\"\n\t\"net/netip\"\n\t\"runtime\"\n\t\"runtime/cgo\"\n\t\"strconv\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"{rt}\"\n)\n\n\ + import (\n\t\"context\"\n\t\"runtime\"\n\t\"runtime/cgo\"\n\t\"unicode/utf8\"\n\t\"unsafe\"\n\n\t\"{rt}\"\n)\n\n\ // The runtime's names for the export's C types and functions.\n\ - type (\n\tcBuf = C.{p}_buf\n\tcWaker = C.{p}_waker\n\tcForeignFuture = C.{p}_foreign_future\n\tcForeignReader = C.{p}_foreign_reader\n\tcU128 = C.{p}_u128\n\tcDuration = C.{p}_duration\n\tcAddr = C.{p}_socket_addr\n\tcIpAddr = C.{p}_ip_addr\n)\n\n\ + type (\n\tcBuf = C.{p}_buf\n\tcWaker = C.{p}_waker\n\tcForeignFuture = C.{p}_foreign_future\n\tcForeignReader = C.{p}_foreign_reader\n\tcU128 = C.{p}_u128\n\tcDuration = C.{p}_duration\n\tcIpv4Addr = C.{p}_ipv4_addr\n\tcIpv6Addr = C.{p}_ipv6_addr\n\tcIpAddr = C.{p}_ip_addr\n\tcSocketAddrV4 = C.{p}_socket_addr_v4\n\tcSocketAddrV6 = C.{p}_socket_addr_v6\n\tcSocketAddr = C.{p}_socket_addr\n)\n\n\ var (\n\tgoCall = (*[0]byte)(rt.GoCall_())\n\tgoRelease = (*[0]byte)(rt.GoRelease_())\n\tgoPoll = (*[0]byte)(rt.GoPoll_())\n\tgoRead = (*[0]byte)(rt.GoRead_())\n\tgoSeek = (*[0]byte)(rt.GoSeek_())\n)\n\n\ func cBufFree(buf cBuf) {{\n\tC.{p}_buf_free(buf)\n}}\n\n\ // cBufCopy is a copy of s Rust owns, as a closure returns it.\n\ @@ -603,7 +623,7 @@ impl<'a> Gen<'a> { static void {go}_waker_wake({p}_waker w) {{\n\tif (w.wake != NULL) {{\n\t\tw.wake(w.data);\n\t}}\n}}\n\n\ static void {go}_waker_drop({p}_waker w) {{\n\tif (w.drop != NULL) {{\n\t\tw.drop(w.data);\n\t}}\n}}\n\ */\nimport \"C\"\n\n\ - import (\n\t\"context\"\n\t\"errors\"\n\t\"io\"\n\t\"runtime\"\n\t\"runtime/cgo\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"unsafe\"\n)\n\n\ + import (\n\t\"context\"\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"io\"\n\t\"math\"\n\t\"math/big\"\n\t\"net\"\n\t\"net/netip\"\n\t\"runtime\"\n\t\"runtime/cgo\"\n\t\"strconv\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"time\"\n\t\"unsafe\"\n)\n\n\ type (\n\tcBuf = C.{p}_buf\n\tcWaker = C.{p}_waker\n)\n\n\ var (\n\tgoCall = (*[0]byte)(C.{go}_call)\n\tgoRelease = (*[0]byte)(C.{go}_release)\n\tgoPoll = (*[0]byte)(C.{go}_poll)\n\tgoRead = (*[0]byte)(C.{go}_read)\n\tgoSeek = (*[0]byte)(C.{go}_seek)\n)\n\n\ func cBufFree(buf cBuf) {{\n\tC.{p}_buf_free(buf)\n}}\n\n\ @@ -792,17 +812,17 @@ impl<'a> Gen<'a> { Type::Char => "rune".to_string(), Type::Str | Type::String | Type::Path | Type::PathBuf => "string".to_string(), Type::Slice | Type::Vec | Type::Bytes => self.qualify(Packages::RT, "Bytes"), - Type::U128 => "*big.Int".to_string(), - Type::Duration => "time.Duration".to_string(), - Type::SocketAddr(_) => "netip.AddrPort".to_string(), - Type::IpAddr(_) => "netip.Addr".to_string(), + Type::U128 => self.qualify(Packages::RT, "U128"), + Type::Duration => self.qualify(Packages::RT, "Duration"), + Type::SocketAddr(family) => self.qualify(Packages::RT, &pascal(abi::addr(true, *family))), + Type::IpAddr(family) => self.qualify(Packages::RT, &pascal(abi::addr(false, *family))), Type::List(_, inner) => format!("[]{}", self.go(inner)), Type::Waker => "func()".to_string(), Type::Reader => "io.ReadSeeker".to_string(), Type::Future => "func(context.Context)".to_string(), Type::Option(inner) => match inner.base() { // Already a pointer, nil for none. - Type::Object(_) | Type::U128 => self.go(inner), + Type::Object(_) => self.go(inner), _ => format!("*{}", self.go(inner)), }, Type::Box(inner) | Type::Ref(_, inner) => self.go(inner), @@ -858,7 +878,7 @@ impl<'a> Gen<'a> { fn zero(&mut self, ty: &Type) -> String { match ty { Type::Bool => "false".to_string(), - Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::F64 | Type::Char | Type::Duration => "0".to_string(), + Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::F64 | Type::Char => "0".to_string(), Type::Str | Type::String | Type::Path | Type::PathBuf => "\"\"".to_string(), Type::Slice | Type::Vec | Type::Bytes => format!("{}{{}}", self.qualify(Packages::RT, "Bytes")), Type::Enum(i) if self.abi.api.enums[*i].plain() => "0".to_string(), @@ -866,6 +886,8 @@ impl<'a> Gen<'a> { Type::Tuple(elements) if elements.len() == 1 => self.zero(&elements[0]), Type::Record(_) | Type::Array(_) + | Type::U128 + | Type::Duration | Type::SocketAddr(_) | Type::IpAddr(_) | Type::Range(_) @@ -889,8 +911,8 @@ impl<'a> Gen<'a> { Ty::Buf => "cBuf".to_string(), Ty::U128 => "cU128".to_string(), Ty::Duration => "cDuration".to_string(), - Ty::SocketAddr => "cAddr".to_string(), - Ty::IpAddr => "cIpAddr".to_string(), + Ty::SocketAddr(family) => format!("c{}", pascal(abi::addr(true, *family))), + Ty::IpAddr(family) => format!("c{}", pascal(abi::addr(false, *family))), Ty::Struct(n) | Ty::Enum(n) => format!("C.{n}"), Ty::Handle { name, .. } | Ty::Future(name) => format!("*C.{name}"), Ty::Waker => "cWaker".to_string(), @@ -907,7 +929,7 @@ impl<'a> Gen<'a> { let api = self.abi.api; match ty { ty if Abi::is_buf(ty) => true, - Type::List(..) | Type::Object(_) => true, + Type::List(..) | Type::Object(_) | Type::Char | Type::NonZero(_) => true, Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) => self.arena(inner), Type::Tuple(elements) => elements.iter().any(|e| self.arena(e)), Type::Record(i) => api.records[*i].fields.iter().any(|f| self.arena(&f.ty)), @@ -930,6 +952,23 @@ impl<'a> Gen<'a> { } } + /// Whether lowering a `ty` can fail the call before it is made, which then returns an + /// error: it holds an object, which may be closed, or text, a char, a path, or a + /// `NonZero`, whose Go types hold values Rust's do not, a path's only on Windows. + fn fails(&self, ty: &Type) -> bool { + let api = self.abi.api; + match ty { + Type::Object(_) | Type::Str | Type::String | Type::Char | Type::Path | Type::PathBuf | Type::NonZero(_) => { + true + } + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => self.fails(inner), + Type::Tuple(elements) => elements.iter().any(|e| self.fails(e)), + Type::Record(i) => api.records[*i].fields.iter().any(|f| self.fails(&f.ty)), + Type::Enum(i) => (api.enums[*i].variants.iter()).any(|v| v.fields.iter().any(|f| self.fails(&f.ty))), + _ => false, + } + } + /// The arena argument, and its comma, a helper lowering `ty` takes, if it needs one. fn arena_arg(&self, ty: &Type) -> &'static str { match self.arena(ty) { @@ -968,22 +1007,20 @@ impl<'a> Gen<'a> { self.array(*n); format!("lower_{}({e})", abi.array(*n)) } - Type::Str | Type::String | Type::Path | Type::PathBuf => format!("a_.bufString({e})"), + Type::Str | Type::String => format!("a_.bufString({e})"), + Type::Path | Type::PathBuf => format!("a_.bufPath({e})"), Type::Slice | Type::Vec | Type::Bytes => format!("a_.bufView({e})"), Type::Unit => unreachable!("no value is unit"), Type::Bool => format!("C.bool({e})"), - Type::Int(int) | Type::NonZero(int) => format!("C.{}({e})", c_int(*int)), + Type::Int(int) => format!("C.{}({e})", c_int(*int)), + Type::NonZero(int) => format!("C.{}(nonZero_(a_, {e}))", c_int(*int)), Type::F32 => format!("C.float({e})"), Type::F64 => format!("C.double({e})"), - Type::Char => format!("C.uint32_t({e})"), + Type::Char => format!("a_.char({e})"), Type::U128 => format!("lowerU128_({e})"), Type::Duration => format!("lowerDuration_({e})"), - Type::SocketAddr(Family::Any) => format!("lowerAddr_({e})"), - Type::SocketAddr(Family::V4) => format!("lowerAddrV4_({e})"), - Type::SocketAddr(Family::V6) => format!("lowerAddrV6_({e})"), - Type::IpAddr(Family::Any) => format!("lowerIp_({e})"), - Type::IpAddr(Family::V4) => format!("lowerIpV4_({e})"), - Type::IpAddr(Family::V6) => format!("lowerIpV6_({e})"), + Type::SocketAddr(family) => format!("lower{}_({e})", pascal(abi::addr(true, *family))), + Type::IpAddr(family) => format!("lower{}_({e})", pascal(abi::addr(false, *family))), Type::Waker => format!("waker_({e})"), Type::Reader | Type::Future => unreachable!("{ty:?} crosses only as a parameter"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { @@ -1000,7 +1037,8 @@ impl<'a> Gen<'a> { self.convert(object); format!("holdOption_{}(a_, {e})", abi.object(*i)) } - Type::Str | Type::String | Type::Path | Type::PathBuf => format!("a_.bufStringOption({e})"), + Type::Str | Type::String => format!("a_.bufStringOption({e})"), + Type::Path | Type::PathBuf => format!("a_.bufPathOption({e})"), Type::Slice | Type::Vec | Type::Bytes => format!("a_.bufViewOption({e})"), Type::List(_, elem) => { let c = abi.list(elem); @@ -1019,11 +1057,7 @@ impl<'a> Gen<'a> { let arena = self.arena(inner); let inner = (**inner).clone(); self.helper(&format!("lower_{c}"), |g| { - // A Go pointer already, which nil is none of. - let (go, value) = match inner.base() { - Type::U128 => (g.go(&inner), g.lower(&inner, "v")), - _ => (format!("*{}", g.go(&inner)), g.lower(&inner, "*v")), - }; + let (go, value) = (format!("*{}", g.go(&inner)), g.lower(&inner, "*v")); let param = g.arena_param(&inner); format!( "func lower_{c}({param}v {go}) C.{c} {{\n\tif v == nil {{\n\t\treturn C.{c}{{}}\n\t}}\n\treturn C.{c}{{some: true, value: {value}}}\n}}\n\n" @@ -1124,8 +1158,8 @@ impl<'a> Gen<'a> { self.array(*n); format!("lift_{}({e})", abi.array(*n)) } - Type::SocketAddr(_) => format!("liftAddr_({e})"), - Type::IpAddr(_) => format!("liftIp_({e})"), + Type::SocketAddr(family) => format!("lift{}_({e})", pascal(abi::addr(true, *family))), + Type::IpAddr(family) => format!("lift{}_({e})", pascal(abi::addr(false, *family))), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { self.convert(ty); format!("lift_{}({e})", abi.range(ty)) @@ -1159,12 +1193,9 @@ impl<'a> Gen<'a> { let inner = (**inner).clone(); self.helper(&format!("lift_{c}"), |g| { let value = g.lift(&inner, "c.value"); - let (go, v) = match inner.base() { - Type::U128 => (g.go(&inner), "v"), - _ => (format!("*{}", g.go(&inner)), "&v"), - }; + let go = g.go(&inner); format!( - "func lift_{c}(c C.{c}) {go} {{\n\tif !c.some {{\n\t\treturn nil\n\t}}\n\tv := {value}\n\treturn {v}\n}}\n\n" + "func lift_{c}(c C.{c}) *{go} {{\n\tif !c.some {{\n\t\treturn nil\n\t}}\n\tv := {value}\n\treturn &v\n}}\n\n" ) }); format!("lift_{c}({e})") @@ -1508,8 +1539,9 @@ impl<'a> Gen<'a> { if !object || t.clone || t.default { self.convert(ty); } - // A value holding an object fails once that object is closed, as an object does. - let closable = object || self.holds(ty); + // A value holding an object fails once that object is closed, as an object does, and + // one holding text or a char Rust's types do not hold fails too. + let closable = object || self.fails(ty); // On a target the type is not on, only the binding makes a value of it, and each // trait fails as a function the target lacks does. let limited = !self.abi.api.everywhere(self.abi.on(ty)); @@ -2273,7 +2305,11 @@ impl<'a> Gen<'a> { /// The C value of a scalar `e` of type `ty` a callback returns, needing no arena. fn lower_scalar(&mut self, ty: &Type, e: &str) -> String { match ty { - Type::Bool | Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::F64 | Type::Char => self.lower(ty, e), + Type::Bool | Type::Int(_) | Type::F32 | Type::F64 => self.lower(ty, e), + // Rust, the caller, has no error to take, so a zero, or a char that is no Unicode + // scalar, aborts. + Type::NonZero(int) => self.lower(&Type::Int(*int), e), + Type::Char => format!("C.uint32_t({e})"), Type::Enum(i) if self.abi.api.enums[*i].plain() => self.lower(ty, e), other => unreachable!("a callback returns a scalar, not {other:?}"), } @@ -2500,11 +2536,12 @@ impl<'a> Gen<'a> { let err = function.err.as_ref().map(strip_refs); let mut results: Vec = oks.iter().map(|ty| self.go(ty)).collect(); // A call that takes an object, as its receiver or held in any value it takes, - // returns an error once that object is closed. + // returns an error once that object is closed, as one taking text or a char does + // once Rust's type does not hold it. let status = export.call.status().is_some(); let everywhere = abi.api.everywhere(function.on); - let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.holds(ty)) - || function.params.iter().any(|p| self.holds(&p.ty)) + let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.fails(ty)) + || function.params.iter().any(|p| self.fails(&p.ty)) || status || !everywhere; if function.err.is_some() || function.future || closable { @@ -2564,7 +2601,7 @@ impl<'a> Gen<'a> { }; prelude.push(format!("c_self_ := lower_{c}({a}{deref}self)")); args.push("&c_self_".to_string()); - checked |= self.holds(&ty); + checked |= self.fails(&ty); } } } @@ -2574,6 +2611,8 @@ impl<'a> Gen<'a> { // Go values, which may panic, such as on a nil enum. let mut bufs = false; let mut lowered = Vec::new(); + // The callbacks, which give Rust a handle only the call releases. + let mut callbacks = Vec::new(); let mut params = Vec::new(); if function.future { params.push(("ctx".to_string(), "context.Context".to_string())); @@ -2593,6 +2632,7 @@ impl<'a> Gen<'a> { let inner = param.ty.base(); params.push((name.clone(), format!("*{}", self.go(inner)))); arena |= self.arena(inner); + checked |= self.fails(inner); lowered.push(args.len()); args.push(self.lower(inner, &format!("(*{name})"))); continue; @@ -2627,7 +2667,7 @@ impl<'a> Gen<'a> { } (ty @ Type::Callback(i, _), _) => { self.convert(ty); - lowered.push(args.len()); + callbacks.push(args.len()); args.push(format!("lower_{}({name})", abi.callback(*i))) } (Type::Waker, _) => args.push(format!("waker_({name})")), @@ -2639,7 +2679,33 @@ impl<'a> Gen<'a> { }; args.push(format!("future_({ctx}, {name})")) } + // Checked before the call, as a char lowered in the arena is, but needing none. + (Type::Char, _) => { + prelude.push(format!( + "if err_ := checkRune_({name}); err_ != nil {{\n\t{}\n}}", + fail("err_") + )); + args.push(format!("C.uint32_t({name})")); + } + (Type::NonZero(int), _) => { + prelude.push(format!("if {name} == 0 {{\n\t{}\n}}", fail("errNonZero_"))); + args.push(format!("C.{}({name})", c_int(*int))); + } (ty, Ty::Buf) => { + // Text and paths are checked before the call. + let check = match ty.buffer() { + Some(Type::String) => Some("checkString_"), + Some(Type::PathBuf) => Some("checkPath_"), + Some(Type::Option(inner)) if *inner == Type::String => Some("checkStringOption_"), + Some(Type::Option(inner)) if *inner == Type::PathBuf => Some("checkPathOption_"), + _ => None, + }; + if let Some(check) = check { + prelude.push(format!( + "if err_ := {check}({name}); err_ != nil {{\n\t{}\n}}", + fail("err_") + )); + } let call = match ty.buffer() { Some(Type::String | Type::PathBuf) => format!("bufString_({name})"), Some(Type::Bytes) => format!("bufTake_({name})"), @@ -2681,6 +2747,7 @@ impl<'a> Gen<'a> { } (ty, _) => { arena |= self.arena(ty); + checked |= self.fails(ty); lowered.push(args.len()); args.push(self.lower(ty, &name)); } @@ -2711,13 +2778,21 @@ impl<'a> Gen<'a> { prelude.insert(take_at, "a_.take = true".to_string()); } body.extend(prelude); + // Converted first, as a conversion may panic or fail the call: then the arena frees + // Rust's buffers it lowered, and no parameter's own buffer is shared yet. + if arena || bufs { + for &i in &lowered { + body.push(format!("l{i}_ := {}", args[i])); + args[i] = format!("l{i}_"); + } + } if checked { body.push(format!("if a_.err != nil {{\n\t{}\n}}", fail("a_.err"))); } - // Converted first, as a conversion may panic: then the arena frees Rust's buffers it - // lowered, and no parameter's own buffer is shared yet. + // Given once no check can fail the call, which then releases them, but before the + // arena hands Rust its buffers, as a nil one panics. if arena || bufs { - for &i in &lowered { + for &i in &callbacks { body.push(format!("l{i}_ := {}", args[i])); args[i] = format!("l{i}_"); } diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index 468b75d77..629e3e6cb 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -384,6 +384,10 @@ pub struct Callback { pub struct Traits { pub clone: bool, pub eq: bool, + /// `Hash`, which a language hashes a value by where its equality is Rust's `PartialEq`. + /// A baseline written before it has none. + #[serde(default)] + pub hash: bool, pub display: bool, pub debug: bool, pub default: bool, diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 08e9ee6ed..225405ca7 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -32,6 +32,8 @@ use crate::abi::{self, Abi, Ty, pascal}; use crate::ir::{Access, Family, Function, Int, Jni, Literal, Mutability, On, Opaque, Param, Receiver, Type}; const RUNTIME: &str = include_str!("../runtime/rt.kt"); +/// The runtime's public types, at the generated file's top level. +const TYPES: &str = include_str!("../runtime/types.kt"); pub const GLUE_RUNTIME: &str = include_str!("../runtime/jni.rs"); /// What the generator writes: the Kotlin file, the Rust module of JNI functions, and the @@ -82,7 +84,16 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result(var value: T)\n\n", ); @@ -115,7 +128,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result jlong {{\n\ + \tusize::MAX as u64 as jlong\n\ + }}\n\n\ + #[unsafe(no_mangle)]\n\ pub extern \"system\" fn Java_{pkg}__1Native__1_1waker_1new(env: *mut JNIEnv, _: jclass, waker: jobject) -> jlong {{\n\ \trt::node(env, waker) as usize as jlong\n\ }}\n\n\ @@ -394,15 +411,23 @@ const TAKEN: &[&str] = &[ "ByteArray", "Double", "DoubleArray", + "Duration", "Exception", "Float", "FloatArray", "Int", "IntArray", + "IpAddr", + "Ipv4Addr", + "Ipv6Addr", "Long", "LongArray", + "PathBuf", "Short", "ShortArray", + "SocketAddr", + "SocketAddrV4", + "SocketAddrV6", "StartSeekable", "String", "UByte", @@ -463,6 +488,9 @@ struct Arg { inside: Vec, /// Kotlin statements once the call returns. after: Vec, + /// The glue's statements that may refuse the call, throwing, which run before any + /// other, so nothing they leave behind needs freeing. + rust_check: Vec, /// The glue's statements before the call, and the C arguments it passes. rust_pre: Vec, rust_args: Vec, @@ -513,13 +541,14 @@ struct Gen<'a> { impl<'a> Gen<'a> { /// The Kotlin call `raw`, then `after`, which closes what it moved out, however it ended - /// unless a failed check refused it, which moved nothing. It binds no name a parameter + /// unless a failed check refused it, or an argument held a value its Rust type cannot, + /// which throws `_Native.__Invalid` first: either moved nothing. It binds no name a parameter /// `after` closes may take: its exception's is fresh, and its result is `_`. fn moving(&mut self, raw: &str, after: &[String]) -> String { let after = after.join("; "); let thrown = self.fresh("e"); format!( - "try {{\n{}\n}} catch ({thrown}: Throwable) {{\n\tif ({thrown} !is _Native.__Refused) {{ {after} }}\n\tthrow {thrown}\n}}.also {{ _ -> {after} }}", + "try {{\n{}\n}} catch ({thrown}: Throwable) {{\n\tif ({thrown} !is _Native.__Refused && {thrown} !is _Native.__Invalid) {{ {after} }}\n\tthrow {thrown}\n}}.also {{ _ -> {after} }}", indent_by(raw, 1) ) } @@ -681,11 +710,11 @@ impl<'a> Gen<'a> { Type::Char => "kotlin.Int".to_string(), Type::Str | Type::String => "kotlin.String".to_string(), Type::Slice | Type::Vec | Type::Bytes => self.own("Bytes"), - Type::Path | Type::PathBuf => "kotlin.String".to_string(), + Type::Path | Type::PathBuf => self.own("PathBuf"), Type::U128 => "java.math.BigInteger".to_string(), - Type::Duration => "kotlin.time.Duration".to_string(), - Type::SocketAddr(_) => "java.net.InetSocketAddress".to_string(), - Type::IpAddr(_) => "java.net.InetAddress".to_string(), + Type::Duration => self.own("Duration"), + Type::SocketAddr(family) => self.own(&format!("SocketAddr{}", family_suffix(*family))), + Type::IpAddr(family) => self.own(&format!("Ip{}Addr", family_suffix(*family).to_lowercase())), Type::List(..) if let Some(inner) = ty.block() => format!("kotlin.{}", block(inner).kt), Type::List(_, inner) => format!("kotlin.collections.List<{}>", self.kt(inner)), Type::Waker => self.own("Waker"), @@ -734,24 +763,25 @@ impl<'a> Gen<'a> { let abi = self.abi; match ty { Type::Bool => format!("{w}.bool({e})"), - Type::Int(int) | Type::NonZero(int) => { + Type::Int(int) => { let (method, _) = wire_int(*int); format!("{w}.{method}({})", to_signed(*int, e)) } + Type::NonZero(int) => { + let (method, _) = wire_int(*int); + format!("{w}.{method}({})", non_zero(&to_signed(*int, e))) + } Type::F32 => format!("{w}.f32({e})"), Type::F64 => format!("{w}.f64({e})"), - Type::Char => format!("{w}.i32({e})"), - Type::Str | Type::String | Type::Path | Type::PathBuf => format!("{w}.string({e})"), + Type::Char => format!("{w}.i32({})", scalar_value(e)), + Type::Str | Type::String => format!("{w}.string({e})"), + Type::Path | Type::PathBuf => format!("{w}.path({e})"), Type::Slice | Type::Vec | Type::Bytes => format!("{w}.bytes({e})"), Type::U128 => format!("{w}.u128({e})"), Type::Array(_) => format!("{w}.raw({e}.bytes)"), Type::Duration => format!("{w}.duration({e})"), - Type::SocketAddr(Family::Any) => format!("{w}.socketAddr({e})"), - Type::SocketAddr(Family::V4) => format!("{w}.socketAddrV4({e})"), - Type::SocketAddr(Family::V6) => format!("{w}.socketAddrV6({e})"), - Type::IpAddr(Family::Any) => format!("{w}.ipAddr({e})"), - Type::IpAddr(Family::V4) => format!("{w}.ipAddrV4({e})"), - Type::IpAddr(Family::V6) => format!("{w}.ipAddrV6({e})"), + Type::SocketAddr(family) => format!("{w}.socketAddr{}({e})", family_suffix(*family)), + Type::IpAddr(family) => format!("{w}.ip{}Addr({e})", family_suffix(*family).to_lowercase()), Type::List(..) if let Some(inner) = ty.block() => format!("{w}.block({})", block(inner).signed(e)), Type::List(_, inner) => { let (l, v) = (self.fresh("l"), self.fresh("v")); @@ -818,8 +848,8 @@ impl<'a> Gen<'a> { Type::U128 => format!("{r}.u128()"), Type::Array(n) => format!("{}({r}.raw({n}))", Abi::array_class(*n)), Type::Duration => format!("{r}.duration()"), - Type::SocketAddr(_) => format!("{r}.socketAddr()"), - Type::IpAddr(_) => format!("{r}.ipAddr()"), + Type::SocketAddr(family) => format!("{r}.socketAddr{}()", family_suffix(*family)), + Type::IpAddr(family) => format!("{r}.ip{}Addr()", family_suffix(*family).to_lowercase()), Type::List(..) if let Some(inner) = ty.block() => block(inner).read(r, &format!("{r}.i32()")), Type::List(_, inner) => { let item = self.read(inner, r, owned); @@ -893,9 +923,11 @@ impl<'a> Gen<'a> { let class = self.qualified(name); self.made(&class, &opaque, &[e.to_string()]) } - Ty::Buf => match (decode(ty), matches!(ty.base(), Type::Option(_)), lent) { - (Some(decode), true, _) => format!("{e}?.{decode}"), - (Some(decode), false, _) => format!("{e}!!.{decode}"), + Ty::Buf => match (copied(ty), matches!(ty.base(), Type::Option(_)), lent) { + (Some(true), true, _) => format!("{e}?.decodeToString()"), + (Some(true), false, _) => format!("{e}!!.decodeToString()"), + (Some(false), true, _) => format!("{e}?.let {{ {}.of(it) }}", self.own("PathBuf")), + (Some(false), false, _) => format!("{}.of({e}!!)", self.own("PathBuf")), (None, true, true) => format!("{e}?.let {{ {}(it) }}", self.own("Bytes")), (None, false, true) => format!("{}({e}!!)", self.own("Bytes")), (None, true, false) => format!("_Native.owned({e})"), @@ -915,7 +947,9 @@ impl<'a> Gen<'a> { 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::Char => scalar_value(e), + Ty::Buf if copied(ty) == Some(false) => format!("{e}.bytes"), + Ty::Buf => format!("_utf8({e})"), Ty::Struct(_) if let Some(opaque) = opaque_of(self.abi, c) && one_word(&opaque) => @@ -959,7 +993,11 @@ impl<'a> Gen<'a> { arg.rust_args.push(p); } Ty::Int(int) => { - arg.exprs.push(to_signed(*int, e)); + let signed = to_signed(*int, e); + arg.exprs.push(match ty.base() { + Type::NonZero(_) => non_zero(&signed), + _ => signed, + }); arg.rust_args.push(from_jni(*int, &p)); } Ty::F32 | Ty::F64 => { @@ -967,7 +1005,7 @@ impl<'a> Gen<'a> { arg.rust_args.push(p); } Ty::Char => { - arg.exprs.push(e.to_string()); + arg.exprs.push(scalar_value(e)); arg.rust_args.push(format!("{p} as u32")); } Ty::Enum(_) => { @@ -996,13 +1034,28 @@ impl<'a> Gen<'a> { arg.exprs.push(h); arg.rust_args.push(format!("{p} as usize as _")); } - // Text crosses as the `String`, encoded once into a buffer Rust takes. - Ty::Buf if string(ty) => { - *arg.jni.last_mut().expect("pushed above") = "jstring"; - *arg.native.last_mut().expect("pushed above") = "String?".to_string(); - arg.exprs.push(e.to_string()); - arg.rust_pre.push(format!("let b{p} = rt::text(env, {p});")); - arg.rust_args.push(format!("b{p}")); + // Text crosses as the `String`, encoded once into a buffer Rust takes, and a path + // as its bytes, copied once. + Ty::Buf if let Some(text) = copied(ty) => { + let option = matches!(ty.base(), Type::Option(_)); + match text { + true => { + *arg.jni.last_mut().expect("pushed above") = "jstring"; + *arg.native.last_mut().expect("pushed above") = "String?".to_string(); + arg.exprs.push(e.to_string()); + arg.rust_check.push(format!( + "let Some(b{p}) = rt::text(env, {p}) else {{\n\t\t\treturn rt::raised();\n\t\t}};" + )); + } + false => { + arg.exprs.push(match option { + true => format!("{e}?.bytes"), + false => format!("{e}.bytes"), + }); + arg.rust_pre.push(format!("let b{p} = rt::bytes(env, {p});")); + } + } + arg.rust_args.push(format!("rt::buf(b{p})")); } // Bytes cross as what holds them, Rust's owner or 0, where they start, and how // many, or -1 for none: Rust takes a copy of Kotlin's, or shares its own. @@ -1108,8 +1161,8 @@ impl<'a> Gen<'a> { arg.inside.extend(writes); arg.exprs.push(format!("{w}.finish()")); arg.exprs.push(format!("{w}.refs()")); - arg.rust_pre.push(format!( - "let b{p} = rt::bytes(env, {b}).unwrap_or_default();\n\t\tlet mut {r} = rt::Reader::{}(env, &b{p}, {q});", + arg.rust_check.push(format!( + "let b{p} = rt::bytes(env, {b}).unwrap_or_default();\n\t\tlet Some(mut {r}) = rt::Reader::{}(env, &b{p}, {q}) else {{\n\t\t\treturn rt::raised();\n\t\t}};", // Read through a pointer, Rust keeps nothing of it. match c { Ty::In(_) => "new", @@ -1123,7 +1176,7 @@ impl<'a> Gen<'a> { _ => format!("a{p}"), }); } - Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::IpAddr | Ty::Struct(_) | Ty::In(_) => { + Ty::U128 | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) | Ty::In(_) => { let w = self.fresh("w"); // Taken by value, each object in a list moves out, as one passed alone does. if moved(abi, ty) { @@ -1140,14 +1193,15 @@ impl<'a> Gen<'a> { *n += 1; arg.exprs.push(format!("{w}.finish()")); arg.exprs.push(format!("{w}.refs()")); - arg.rust_pre.push(format!( - "let b{p} = rt::bytes(env, {p}).unwrap_or_default();\n\t\tlet mut r{p} = rt::Reader::{}(env, &b{p}, {q});\n\t\tlet a{p} = r{p}.get();", + arg.rust_check.push(format!( + "let b{p} = rt::bytes(env, {p}).unwrap_or_default();\n\t\tlet Some(mut r{p}) = rt::Reader::{}(env, &b{p}, {q}) else {{\n\t\t\treturn rt::raised();\n\t\t}};", // Read through a pointer, Rust keeps nothing of it. match c { Ty::In(_) => "new", _ => "taking", } )); + arg.rust_pre.push(format!("let a{p} = r{p}.get();")); arg.rust_args.push(match c { Ty::In(_) => format!("&a{p}"), _ => format!("a{p}"), @@ -1364,7 +1418,8 @@ impl<'a> Gen<'a> { // takes, or else encoded. let fits = |n: u64| opaque.is_some_and(|o| 2 * n * o.words() <= 255); // The receiver as the glue takes it: a handle, held for the call, an opaque value's - // words, or encoded. + // words, or encoded, which the glue reads first. + let mut pre = String::new(); let (params, open, arg, rust_self): (Vec<(&str, &str)>, _, _, _) = match (object, opaque) { (true, _) => ( vec![("jlong", "Long")], @@ -1383,11 +1438,12 @@ impl<'a> Gen<'a> { } (false, _) => { let write = self.write_value(ty, "w", "this"); + pre = "let b = rt::bytes(env, p0).unwrap_or_default();\n\t\tlet Some(mut r) = rt::Reader::new(env, &b, p1) else {\n\t\t\treturn rt::raised();\n\t\t};\n\t\t".to_string(); ( 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(), + "&r.get()".to_string(), ) } }; @@ -1395,10 +1451,10 @@ impl<'a> Gen<'a> { let status = self.abi.traits_status_of(ty); let checked = |call: &str, name: &str, then: &str| match status { true => format!( - "let mut status = 0;\n\t\tlet ret = {}, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::refuse(env, status, \"{name}\");\n\t\t}}\n\t\t{then}", + "{pre}let mut status = 0;\n\t\tlet ret = {}, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::refuse(env, status, \"{name}\");\n\t\t}}\n\t\t{then}", call.strip_suffix(')').expect("a call") ), - false => format!("let ret = {call};\n\t\t{then}"), + false => format!("{pre}let ret = {call};\n\t\t{then}"), }; let unary = |g: &mut Self, suffix: &str, sig: &str| { g.native( @@ -1413,6 +1469,19 @@ impl<'a> Gen<'a> { ); format!("\t{sig} = {open}\n\t\t_Native.{bare}_{suffix}({arg})!!.decodeToString()\n\t}}\n") }; + // The hash agreeing with an `equals` through Rust's `PartialEq`: Rust's own `Hash`, + // folded as `Long.hashCode` folds it, or else one for every value. + let hash_code = |g: &mut Self| { + if !t.hash { + return "\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): kotlin.Int = 0\n".to_string(); + } + let call = format!("crate::{c}_hash({rust_self})"); + let body = checked(&call, &format!("{c}_hash"), "ret as jlong"); + g.native(&format!("{bare}_hash"), ¶ms, ("jlong", "Long"), &body); + format!( + "\t/** Rust's `Hash`, as `Long.hashCode` folds it. */\n\toverride fun hashCode(): kotlin.Int = {open}\n\t\t_Native.{bare}_hash({arg}).hashCode()\n\t}}\n" + ) + }; if t.display { // Final, so a data class variant of a sealed class keeps it rather than making // its own. @@ -1463,14 +1532,15 @@ impl<'a> Gen<'a> { let (this, other) = (self.write_value(ty, "w", "this"), self.write_value(ty, "w", "other")); ( vec![("jbyteArray", "ByteArray"), ("jobjectArray", "kotlin.Array?")], - "{ let b = rt::bytes(env, p0).unwrap_or_default(); let mut r = rt::Reader::new(env, &b, p1); crate::{c}_eq(&r.get(), &r.get()) }".replace("{c}", c), + "{ let b = rt::bytes(env, p0).unwrap_or_default(); let Some(mut r) = rt::Reader::new(env, &b, p1) else { return rt::raised() }; crate::{c}_eq(&r.get(), &r.get()) }".replace("{c}", c), format!("_Native.__Writer().use {{ w -> {this}; {other}; _Native.{bare}_eq(w.finish(), w.refs()) }}"), ) } }; self.native(&format!("{bare}_eq"), ¶ms, ("jboolean", "Boolean"), &rust); + let hash = hash_code(self); body.push(format!( - "\toverride fun equals(other: kotlin.Any?): kotlin.Boolean = other is {class} && {kt}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): kotlin.Int = 0\n" + "\toverride fun equals(other: kotlin.Any?): kotlin.Boolean = other is {class} && {kt}\n\n{hash}" )); } if object && t.eq { @@ -1484,8 +1554,9 @@ impl<'a> Gen<'a> { "ret", ), ); + let hash = hash_code(self); body.push(format!( - "\toverride fun equals(other: kotlin.Any?): kotlin.Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> _Native.{bare}_eq(a, b) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): kotlin.Int = 0\n" + "\toverride fun equals(other: kotlin.Any?): kotlin.Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> _Native.{bare}_eq(a, b) }} }}\n\n{hash}" )); } if t.default { @@ -2086,10 +2157,7 @@ impl<'a> Gen<'a> { } // Encoded, as an option of an opaque value or one past a word, 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() - } + Ty::Struct(_) => "rt::Reader::plain(&rt::bytes(env, ret).unwrap_or_default()).get()".to_string(), _ => unreachable!("a callback or closure returns no {c_ok:?}"), }; let back = match thrown { @@ -2422,7 +2490,10 @@ impl<'a> Gen<'a> { // A future's call also takes the signal its waker parks, if its first poll pends. let signal = export.future.is_some().then(|| self.fresh("s")); exprs.extend(signal.clone()); - let mut rust_pre: Vec = args.iter().flat_map(|a| a.rust_pre.clone()).collect(); + let mut rust_pre: Vec = (args.iter().flat_map(|a| &a.rust_check)) + .chain(args.iter().flat_map(|a| &a.rust_pre)) + .cloned() + .collect(); if status { rust_pre.push("let mut status = 0;".to_string()); } @@ -2891,7 +2962,7 @@ impl<'a> Gen<'a> { let (jni, native) = match c_ret { _ if let Some(b) = &returned => (b.jni, b.signed), // Bytes cross copied or as their owner. - Ty::Buf if !string(&ok) => ("jobject", "Any?"), + Ty::Buf if copied(&ok).is_none() => ("jobject", "Any?"), Ty::Waker => ("jlong", "Long"), _ => jni_type(abi, &c_ret, &ok), }; @@ -3139,13 +3210,17 @@ impl<'a> Gen<'a> { *n += 1; let (jni, native, kt, rust) = match ty { Type::Bool => ("jboolean", "Boolean", e, p), - Type::Int(int) | Type::NonZero(int) => { + Type::Int(int) => { let (jni, native) = jni_type(abi, &Ty::Int(*int), ty); (jni, native, to_signed(*int, &e), from_jni(*int, &p)) } + Type::NonZero(int) => { + let (jni, native) = jni_type(abi, &Ty::Int(*int), ty); + (jni, native, non_zero(&to_signed(*int, &e)), from_jni(*int, &p)) + } Type::F32 => ("jfloat", "Float", e, p), Type::F64 => ("jdouble", "Double", e, p), - Type::Char => ("jint", "Int", e, format!("{p} as u32")), + Type::Char => ("jint", "Int", scalar_value(&e), format!("{p} as u32")), _ => ("jint", "Int", format!("{e}.value"), p), }; arg.jni.push(jni); @@ -3285,7 +3360,7 @@ fn c_slots(c: &Ty) -> usize { Ty::Int(int) if jni_int(*int) == "jlong" => 2, Ty::F64 | Ty::Handle { .. } | Ty::Future(_) => 2, Ty::Buf => 5, - Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::IpAddr | Ty::Struct(_) | Ty::In(_) => 2, + Ty::U128 | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) | Ty::In(_) => 2, _ => 1, } } @@ -3406,22 +3481,37 @@ fn moved(abi: &Abi<'_>, ty: &Type) -> bool { } } -/// Whether `ty` crosses as a buffer of UTF-8 text, or none. -fn string(ty: &Type) -> bool { - decode(ty).is_some() -} - -/// How Kotlin decodes the buffer of text `ty` crosses as, if it is text: a path, which on -/// Unix may hold any bytes, throws on those no `String` holds. -fn decode(ty: &Type) -> Option<&'static str> { +/// Whether `ty` crosses as text, `Some(true)`, or a path, `Some(false)`, each a buffer +/// copied whole, or as neither. +fn copied(ty: &Type) -> Option { match ty.base() { - Type::Option(inner) => decode(inner), - Type::Str | Type::String => Some("decodeToString()"), - Type::Path | Type::PathBuf => Some("decodeToString(throwOnInvalidSequence = true)"), + Type::Option(inner) => copied(inner), + Type::Str | Type::String => Some(true), + Type::Path | Type::PathBuf => Some(false), _ => None, } } +/// The suffix of the runtime's type, and of its codec's method, for an address of `family`. +fn family_suffix(family: Family) -> &'static str { + match family { + Family::Any => "", + Family::V4 => "V4", + Family::V6 => "V6", + } +} + +/// The Kotlin `Int` `e` as a `char` crosses, throwing `IllegalArgumentException` unless it is +/// a Unicode scalar value, as no Rust `char` holds any other. +fn scalar_value(e: &str) -> String { + format!("_Native.__char({e})") +} + +/// The signed JNI integer `e` of a `NonZero`, throwing if it is zero, which no `NonZero` is. +fn non_zero(e: &str) -> String { + format!("_Native.__nonZero({e})") +} + /// The bound struct of `int`'s pairs of bounds. fn abi_bound(abi: &Abi<'_>, int: Int) -> String { abi.range(&Type::Bound(int)) @@ -3445,7 +3535,7 @@ fn jni_type(abi: &Abi<'_>, c: &Ty, ty: &Type) -> (&'static str, &'static str) { Ty::Handle { .. } | Ty::Future(_) => ("jlong", "Long"), Ty::Buf => ("jbyteArray", "ByteArray?"), Ty::Struct(_) if matches!(ty.base(), Type::Callback(..)) => ("jobject", "Any"), - Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::IpAddr | Ty::Struct(_) | Ty::In(_) => { + Ty::U128 | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) | Ty::In(_) => { ("jbyteArray", "ByteArray") } Ty::Fn(..) => ("jobject", "Any"), @@ -3519,7 +3609,7 @@ fn ret_expr(abi: &Abi<'_>, c: &Ty, ok: &Type) -> String { Ty::Char => "ret as jint".to_string(), Ty::Handle { .. } => "ret as usize as jlong".to_string(), Ty::Waker => "rt::returned(ret)".to_string(), - Ty::Buf if string(ok) => "rt::owned(env, ret)".to_string(), + Ty::Buf if copied(ok).is_some() => "rt::owned(env, ret)".to_string(), Ty::Buf => "rt::hand(env, ret)".to_string(), _ => format!( "{{ let mut w = rt::Writer::default(); {}; w.array(env) }}", @@ -3619,8 +3709,7 @@ pub(crate) fn rust_c(abi: &Abi<'_>, c: &Ty) -> String { Ty::Buf => "crate::rt::Buf".to_string(), Ty::U128 => "crate::rt::U128".to_string(), Ty::Duration => "crate::rt::Duration".to_string(), - Ty::SocketAddr => "crate::rt::SocketAddr".to_string(), - Ty::IpAddr => "crate::rt::IpAddr".to_string(), + Ty::SocketAddr(_) | Ty::IpAddr(_) => format!("crate::{}", crate::glue::rs(c)), Ty::Struct(name) => format!("crate::{name}"), Ty::Handle { .. } => "*const std::ffi::c_void".to_string(), Ty::Out(inner) => format!("*mut {}", rust_c(abi, inner)), @@ -3672,7 +3761,10 @@ fn to_signed(int: Int, e: &str) -> String { Int::U8 => format!("{e}.toByte()"), Int::U16 => format!("{e}.toShort()"), Int::U32 => format!("{e}.toInt()"), - Int::U64 | Int::Usize => format!("{e}.toLong()"), + Int::U64 => format!("{e}.toLong()"), + // Checked against the library's target, which may be 32-bit. + Int::Usize => format!("_Native.__usize({e})"), + Int::Isize => format!("_Native.__isize({e})"), _ => e.to_string(), } } diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index af40e1a13..86a2a1eb4 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -566,6 +566,7 @@ fn changed(base: &Index<'_>, now: &Index<'_>, maps: &Maps, ours: &Item<'_>, thei let lost: Vec<&str> = [ ("Clone", ours.clone && !theirs.clone), ("PartialEq", ours.eq && !theirs.eq), + ("Hash", ours.hash && !theirs.hash), ("Display", ours.display && !theirs.display), ("Debug", ours.debug && !theirs.debug), ("Default", ours.default && !theirs.default), diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 6fe006f19..658ef8281 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -98,6 +98,7 @@ const TAKEN: &[&str] = &[ "_object", "_range", "ForeignReader", + "Duration", ]; /// Any buffer, as Python before 3.12's `collections.abc.Buffer` spells it. @@ -107,11 +108,11 @@ const BUFFER: &str = "_bytes | _bytearray | _memoryview"; const V4: &str = "_tuple[_str, _int]"; const V6: &str = "_tuple[_str, _int, _int, _int]"; -/// The registry's first entries, the runtime's: the glue's `T_FUTURE`, `T_TIMEDELTA`, +/// The registry's first entries, the runtime's: the glue's `T_FUTURE`, `T_DURATION`, /// `T_RESUME`, `T_WAKER`, `T_POLLABLE`, `T_IPV4`, and `T_IPV6`. const RUNTIME_TYPES: &[&str] = &[ "_Future", - "_datetime.timedelta", + "Duration", "_resume", "Waker", "_Pollable", @@ -495,7 +496,7 @@ impl<'a> Gen<'a> { Type::F32 | Type::F64 => "_float".to_string(), Type::Char | Type::Str | Type::String | Type::Path | Type::PathBuf => "_str".to_string(), Type::Slice | Type::Vec | Type::Bytes => "_memoryview".to_string(), - Type::Duration => "_datetime.timedelta".to_string(), + Type::Duration => "Duration".to_string(), Type::SocketAddr(family) => match family { Family::V4 => V4.to_string(), Family::V6 => V6.to_string(), @@ -578,7 +579,8 @@ impl<'a> Gen<'a> { abi.array(*n) ), Type::Bool => format!("rt::bool_in({o})"), - Type::Int(int) | Type::NonZero(int) => format!("rt::int_in::<{}>({o})", int.rust()), + Type::Int(int) => format!("rt::int_in::<{}>({o})", int.rust()), + Type::NonZero(int) => format!("rt::nonzero_in::<{}>({o})", int.rust()), Type::F32 => format!("rt::float_in({o}).map(|f| f as f32)"), Type::F64 => format!("rt::float_in({o})"), Type::Char => format!("rt::char_in({o})"), @@ -587,8 +589,7 @@ impl<'a> Gen<'a> { Type::Path | Type::PathBuf => format!("rt::path_in({o}, s)"), Type::U128 => format!("rt::u128_in({o})"), Type::Duration => format!("rt::duration_in({o})"), - Type::SocketAddr(family) => format!("rt::addr_in({o}, {})", family_of(*family)), - Type::IpAddr(family) => format!("rt::ip_in({o}, {})", family_of(*family)), + Type::SocketAddr(family) | Type::IpAddr(family) => format!("rt::{}_in({o})", addr_fn(ty, *family)), Type::List(_, inner) if ty.block().is_some() => format!("rt::block_in::<{}>({o}, s)", number(inner)), Type::List(..) => format!("r_{}({o}, s, {t})", self.converter(ty)), Type::Option(inner) => match inner.base() { @@ -655,8 +656,7 @@ impl<'a> Gen<'a> { Type::Path | Type::PathBuf => format!("rt::path_out({v})"), Type::U128 => format!("rt::u128_out({v})"), Type::Duration => format!("rt::duration_out({v})"), - Type::SocketAddr(_) => format!("rt::addr_out({v})"), - Type::IpAddr(_) => format!("rt::ip_out({v})"), + Type::SocketAddr(family) | Type::IpAddr(family) => format!("rt::{}_out({v})", addr_fn(ty, *family)), Type::List(_, inner) if ty.block().is_some() => format!("rt::block_out::<{}>({v})", number(inner)), Type::List(..) => format!("w_{}({v})", self.converter(ty)), Type::Option(inner) => match inner.base() { @@ -923,7 +923,7 @@ impl<'a> Gen<'a> { /// The Python members a type's std traits export beyond Python's own, with their glue: /// `__str__` from `Display`, `__repr__` from `Debug`, a static `default` from - /// `Default`, and `clone` and `__eq__` for an object. + /// `Default`, and `clone`, `__eq__`, and `__hash__` from `Hash` for an object. fn traits(&mut self, ty: &Type, c: &str, t: Traits, body: &mut Body) { let bare = self.bare(c).to_string(); let class = self.class(c); @@ -1017,6 +1017,11 @@ impl<'a> Gen<'a> { "@_staticmethod\ndef {bare}_eq(this: {class}, other: {class}, /) -> _bool: ...\n" )); } + // Rust's `Hash` makes it hashable again, agreeing with its `__eq__`. + if (object || opaque) && t.hash { + unary(self, "hash", "__hash__", "", "rt::uint_out(ret)".to_string()); + body.native.push("\tdef __hash__(self) -> _int: ...\n".to_string()); + } if t.default { let lower = self.lower(ty, "ret"); let docs = "Rust's `Default`."; @@ -1979,11 +1984,16 @@ impl<'a> Gen<'a> { } /// The glue's name for a socket address family. -fn family_of(family: Family) -> &'static str { - match family { - Family::Any => "rt::Family::Any", - Family::V4 => "rt::Family::V4", - Family::V6 => "rt::Family::V6", +/// The runtime's name for the converters of a socket or IP address of `family`, before +/// `_in` or `_out`. +fn addr_fn(ty: &Type, family: Family) -> &'static str { + match (ty, family) { + (Type::SocketAddr(_), Family::Any) => "addr", + (Type::SocketAddr(_), Family::V4) => "addr_v4", + (Type::SocketAddr(_), Family::V6) => "addr_v6", + (_, Family::Any) => "ip", + (_, Family::V4) => "ipv4", + (_, Family::V6) => "ipv6", } } diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 7309bc5ec..f2ca92669 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -26,7 +26,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::fmt::Write; use crate::abi::{self, Abi, Ty, pascal}; -use crate::ir::{Access, Closure, Function, Int, Literal, Mutability, On, Param, Receiver, Type}; +use crate::ir::{Access, Closure, Family, Function, Int, Literal, Mutability, On, Param, Receiver, Type}; const RUNTIME: &str = include_str!("../runtime/rt.swift"); @@ -87,8 +87,12 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result Swift.Bool {{\n\t\t{p}_contract() == 0x{contract:016x} && {upper}_CONTRACT == 0x{contract:016x}\n\t}}\n\n\ \t/// The index of the library's target among those this file serves.\n\ \tstatic let target = Swift.UInt64({p}_target())\n\n\ @@ -152,8 +156,8 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result_native`, `_bytes`, `_socket_addr`, and `_ip_addr`. +/// runtime's own are the C ABI's: `Native`, `Bytes`, and each address type are claimed as +/// `_native`, `_bytes`, `_socket_addr`, and so on. const TAKEN: &[&str] = &[ "Array", "Bool", @@ -169,14 +173,21 @@ const TAKEN: &[&str] = &[ "Int", "Int64", "IpAddr", + "Ipv4Addr", + "Ipv6Addr", "Never", + "NonZero", "OpaquePointer", "Optional", + "PathBuf", "RandomAccessCollection", + "RustDuration", "Sendable", "SeekOrigin", "SeekableReader", "SocketAddr", + "SocketAddrV4", + "SocketAddrV6", "StaticString", "Swift", "Task", @@ -351,16 +362,22 @@ impl<'a> Gen<'a> { Type::Array(n) => Abi::array_class(*n), Type::Unit => "Void".to_string(), Type::Bool => "Swift.Bool".to_string(), - Type::Int(int) | Type::NonZero(int) => sw_int(*int).to_string(), + Type::Int(int) => sw_int(*int).to_string(), + Type::NonZero(int) => format!("NonZero<{}>", sw_int(*int)), Type::F32 => "Swift.Float".to_string(), Type::F64 => "Swift.Double".to_string(), Type::Char => "Swift.Unicode.Scalar".to_string(), - Type::Str | Type::String | Type::Path | Type::PathBuf => "Swift.String".to_string(), + Type::Str | Type::String => "Swift.String".to_string(), + Type::Path | Type::PathBuf => "PathBuf".to_string(), Type::Slice | Type::Vec | Type::Bytes => "Bytes".to_string(), Type::U128 => "U128".to_string(), - Type::Duration => "Swift.Duration".to_string(), - Type::SocketAddr(_) => "SocketAddr".to_string(), - Type::IpAddr(_) => "IpAddr".to_string(), + Type::Duration => "RustDuration".to_string(), + Type::SocketAddr(Family::Any) => "SocketAddr".to_string(), + Type::SocketAddr(Family::V4) => "SocketAddrV4".to_string(), + Type::SocketAddr(Family::V6) => "SocketAddrV6".to_string(), + Type::IpAddr(Family::Any) => "IpAddr".to_string(), + Type::IpAddr(Family::V4) => "Ipv4Addr".to_string(), + Type::IpAddr(Family::V6) => "Ipv6Addr".to_string(), Type::List(_, inner) if self.unique(inner) => format!("UniqueArray<{}>", self.sw(inner)), Type::List(_, inner) => format!("[{}]", self.sw(inner)), Type::Waker => "@Sendable () -> Void".to_string(), @@ -408,8 +425,8 @@ impl<'a> Gen<'a> { Ty::Buf => abi.rt("buf"), Ty::U128 => abi.rt("u128"), Ty::Duration => abi.rt("duration"), - Ty::SocketAddr => abi.rt("socket_addr"), - Ty::IpAddr => abi.rt("ip_addr"), + Ty::SocketAddr(family) => abi.rt(abi::addr(true, *family)), + Ty::IpAddr(family) => abi.rt(abi::addr(false, *family)), Ty::Struct(n) | Ty::Enum(n) => n.clone(), Ty::Handle { .. } | Ty::Future(_) => "OpaquePointer?".to_string(), Ty::Waker => abi.rt("waker"), @@ -483,13 +500,13 @@ impl<'a> Gen<'a> { match ty { Type::Unit => unreachable!("no value is unit"), Type::Bool | Type::F32 | Type::F64 => e.to_string(), - Type::Int(int) | Type::NonZero(int) => lower_int(*int, e), + Type::Int(int) => lower_int(*int, e), + Type::NonZero(int) => lower_int(*int, &format!("{e}.value")), Type::Char => format!("{e}.value"), Type::U128 => format!("Native.u128({e})"), Type::Array(_) => format!("{e}._c"), Type::Duration => format!("Native.duration({e})"), - Type::SocketAddr(_) => format!("Native.socketAddr({e})"), - Type::IpAddr(_) => format!("Native.ipAddr({e})"), + Type::SocketAddr(_) | Type::IpAddr(_) => format!("Native.addr({e})"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => format!("{e}._lower()"), Type::Waker => format!("Native.waker({e})"), Type::Future => format!("Native.future({e})"), @@ -588,13 +605,13 @@ impl<'a> Gen<'a> { Type::Path | Type::PathBuf => format!("Native.path({e})"), Type::Slice | Type::Vec | Type::Bytes => format!("Native.bytes({e})"), Type::Bool | Type::F32 | Type::F64 => e.to_string(), - Type::Int(int) | Type::NonZero(int) => lift_int(*int, e), + Type::Int(int) => lift_int(*int, e), + Type::NonZero(int) => format!("NonZero(_unchecked: {})", lift_int(*int, e)), Type::Char => format!("Native.scalar({e})"), Type::U128 => format!("Native.u128({e})"), Type::Array(n) => format!("{}(_c: {e})", Abi::array_class(*n)), Type::Duration => format!("Native.duration({e})"), - Type::SocketAddr(_) => format!("Native.socketAddr({e})"), - Type::IpAddr(_) => format!("Native.ipAddr({e})"), + Type::SocketAddr(_) | Type::IpAddr(_) => format!("Native.addr({e})"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => format!("{}._lift({e})", self.sw(ty)), // A block is copied out whole, then freed. Type::List(_, inner) if ty.block().is_some() => { @@ -826,8 +843,8 @@ impl<'a> Gen<'a> { } /// The members a type's std traits export, with the protocols they conform it to: - /// `Display` is `description`, `Debug` `debugDescription`, `PartialEq` `==`, `Default` - /// a static `default()`, and `Clone` an object's `clone()`. + /// `Display` is `description`, `Debug` `debugDescription`, `PartialEq` `==`, `Hash` + /// `hash(into:)`, `Default` a static `default()`, and `Clone` an object's `clone()`. fn traits( &mut self, ty: &Type, @@ -929,8 +946,12 @@ impl<'a> Gen<'a> { )); } if t.eq { + // `Hash` agrees with `PartialEq`, so a type with both hashes as Rust does. if !status { - protocols.push("Equatable"); + protocols.push(match t.hash { + true => "Hashable", + false => "Equatable", + }); } let what = format!("{c}_eq"); let same = |r: &str| r.to_string(); @@ -964,6 +985,14 @@ impl<'a> Gen<'a> { "\tpublic static func == (a: {borrowing}{name}, b: {borrowing}{name}){throws} -> Swift.Bool {{\n\t\t{compare}\n\t}}\n" )); } + if t.hash { + members.push("hash(into:)".to_string()); + let combine = |r: &str| format!("hasher.combine({r})"); + body.push(format!( + "\t/// Rust's `Hash`, agreeing with `==`.\n\tpublic func hash(into hasher: inout Swift.Hasher){throws} {{\n\t\t{}\n\t}}\n", + unary("hash", &combine) + )); + } if t.default { members.push("static default".to_string()); let lift = match ty { @@ -1258,12 +1287,12 @@ impl<'a> Gen<'a> { self.types, "{docs}public struct {name}: ~Copyable, @unchecked Sendable {{\n{body}}}\n\n" ); - // Swift 6.4 takes a noncopyable type's `Equatable`, but not yet its + // Swift 6.4 takes a noncopyable type's `Equatable` and `Hashable`, but not yet its // `CustomStringConvertible` or `CustomDebugStringConvertible`, whose members it keeps. - if protocols.contains(&"Equatable") { + if let Some(equal) = protocols.iter().find(|p| ["Equatable", "Hashable"].contains(p)) { let _ = write!( self.types, - "#if compiler(>=6.4)\nextension {name}: Equatable {{}}\n#endif\n\n" + "#if compiler(>=6.4)\nextension {name}: {equal} {{}}\n#endif\n\n" ); } } @@ -1538,8 +1567,8 @@ impl<'a> Gen<'a> { /// as it lowers, and text in a buffer `buf_copy` made. fn returned(&mut self, ty: &Type, e: &str) -> String { match ty { - Type::Str | Type::String => format!("Native.owned({e})"), - Type::Option(inner) if matches!(**inner, Type::Str | Type::String) => { + Type::Str | Type::String | Type::PathBuf => format!("Native.owned({e})"), + Type::Option(inner) if matches!(**inner, Type::Str | Type::String | Type::PathBuf) => { format!("({e}.map {{ Native.owned($0) }} ?? Native.none)") } ty => Self::trapping(self.lower(ty, e)), @@ -2440,7 +2469,13 @@ mod tests { continue; }; let decl = decl.trim_end_matches('{').trim(); - let (name, conforms) = decl.split_once(':').unwrap_or((decl, "")); + // A generic's parameters, such as `NonZero`, name it + // no further; the type is its name before them. + let decl = match decl.split_once('<') { + Some((name, rest)) => name.to_string() + rest.split_once('>').expect("a generic closes").1, + None => decl.to_string(), + }; + let (name, conforms) = decl.split_once(':').unwrap_or((&decl, "")); let names = std::iter::once(name).chain(conforms.split(',')); // A qualified name, such as `Swift.Error`, cannot clash. for name in (names.map(str::trim)).filter(|n| !n.is_empty() && !n.starts_with('@') && !n.contains('.')) { diff --git a/ffi/tests/api.c b/ffi/tests/api.c index d0eb75c1d..0809e3b62 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -5,6 +5,7 @@ #include #include +#include #include #ifdef __linux__ @@ -484,9 +485,9 @@ static bool closures(void) { print("sought error ", landed_err, "\n"); } print("non zeros ", api_describe_non_zeros(api_non_zeros()), "\n"); - api_option_u64 hops[] = {{0}, {.some = true, .value = 1}, {.some = true, .value = UINT64_MAX}}; + api_option_non_zero_u64 hops[] = {{0}, {.some = true, .value = 1}, {.some = true, .value = UINT64_MAX}}; for (size_t i = 0; i < sizeof hops / sizeof hops[0]; i++) { - api_option_u64 hop = api_next_hop(hops[i]); + api_option_non_zero_u64 hop = api_next_hop(hops[i]); if (hop.some) { printf("hop Some(%llu)\n", (unsigned long long)hop.value); } else { @@ -597,11 +598,39 @@ static void print_escaped(const char *before, api_buf buf, const char *after) { /* A socket address, as Rust's Display prints the ones the script makes. */ static void print_addr(api_socket_addr addr) { if (!addr.v6) { - printf("%u.%u.%u.%u:%u", addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3], addr.port); + const uint8_t *ip = addr.addr.v4.ip.octets; + printf("%u.%u.%u.%u:%u", ip[0], ip[1], ip[2], ip[3], addr.addr.v4.port); return; } /* Only the loopback, `::1`. */ - printf("[::%u%%%u]:%u", addr.ip[15], addr.scope_id, addr.port); + printf("[::%u%%%u]:%u", addr.addr.v6.ip.octets[15], addr.addr.v6.scope_id, addr.addr.v6.port); +} + +/* Bytes in hex. */ +static void print_hex(const uint8_t *bytes, size_t len) { + for (size_t i = 0; i < len; i++) { + printf("%02x", bytes[i]); + } +} + +/* A socket address as the script's socket_hex prints it. */ +static void print_socket_hex(api_socket_addr addr) { + if (!addr.v6) { + print_hex(addr.addr.v4.ip.octets, 4); + printf(":%u", addr.addr.v4.port); + return; + } + print_hex(addr.addr.v6.ip.octets, 16); + printf(":%u:%u:%u", addr.addr.v6.port, addr.addr.v6.flowinfo, addr.addr.v6.scope_id); +} + +/* An IP address's bytes in hex, so its length shows its family. */ +static void print_ip_hex(api_ip_addr ip) { + if (ip.v6) { + print_hex(ip.ip.v6.octets, 16); + } else { + print_hex(ip.ip.v4.octets, 4); + } } /* A bound, as Rust's Debug prints it. */ @@ -1447,8 +1476,8 @@ static int script(void) { } api_socket_addr addrs[2] = { - {.ip = {127, 0, 0, 1}, .port = 8080}, - {.ip = {[15] = 1}, .port = 443, .v6 = true, .scope_id = 3}, + {.addr.v4 = {.ip = {{127, 0, 0, 1}}, .port = 8080}}, + {.v6 = true, .addr.v6 = {.ip = {{[15] = 1}}, .port = 443, .scope_id = 3}}, }; api_vec_u16 ports = api_ports((api_vec_socket_addr){.ptr = addrs, .len = 2}); printf("ports [%u, %u]\n", ports.ptr[0], ports.ptr[1]); @@ -1929,25 +1958,94 @@ static int script(void) { } printf("\n"); api_vec_Counter_free(numbered); - api_socket_addr at4 = {.ip = {127, 0, 0, 1}, .port = 8080}; - api_socket_addr at6 = {.ip = {[15] = 1}, .port = 443, .v6 = true, .scope_id = 3}; - api_socket_addr next4, next6; + api_socket_addr_v4 at4 = {.ip = {{127, 0, 0, 1}}, .port = 8080}; + api_socket_addr_v6 at6 = {.ip = {{[15] = 1}}, .port = 443, .scope_id = 3}; + api_socket_addr_v4 next4; + api_socket_addr_v6 next6; api_nexts(at4, at6, &next4, &next6); - printf("nexts %u.%u.%u.%u %u %u %u\n", next4.ip[0], next4.ip[1], next4.ip[2], next4.ip[3], next4.port, next6.port, - next6.scope_id); - api_ip_addr ip4 = {.ip = {10, 0, 0, 255}}, ip6 = {.ip = {[14] = 0xff, [15] = 0xff}, .v6 = true}; - api_ip_addr nextip4, nextip6; + const uint8_t *n4 = next4.ip.octets; + printf("nexts %u.%u.%u.%u %u %u %u\n", n4[0], n4[1], n4[2], n4[3], next4.port, next6.port, next6.scope_id); + api_ipv4_addr ip4 = {{10, 0, 0, 255}}, nextip4; + api_ipv6_addr ip6 = {{[14] = 0xff, [15] = 0xff}}, nextip6; api_next_ips(ip4, ip6, &nextip4, &nextip6); - api_ip_addr mapped = api_flip((api_ip_addr){.ip = {127, 0, 0, 1}}); - api_ip_addr ips[] = {nextip4, nextip6, mapped, api_flip(mapped), api_flip((api_ip_addr){.ip = {[15] = 1}, .v6 = true})}; + api_ip_addr mapped = api_flip((api_ip_addr){.ip.v4 = {{127, 0, 0, 1}}}); + api_ip_addr ips[] = { + {.ip.v4 = nextip4}, + {.v6 = true, .ip.v6 = nextip6}, + mapped, + api_flip(mapped), + api_flip((api_ip_addr){.v6 = true, .ip.v6 = {{[15] = 1}}}), + }; printf("ips"); for (size_t i = 0; i < sizeof ips / sizeof ips[0]; i++) { printf(" "); - for (size_t b = 0; b < (ips[i].v6 ? 16u : 4u); b++) { - printf("%02x", ips[i].ip[b]); - } + print_ip_hex(ips[i]); + } + printf("\n"); + + /* Lossless round trips: each type's edge values, passed through Rust and back. */ + api_duration durations[] = {{0, 0}, {0, 1}, {9223372036u, 854775808u}, {UINT64_MAX, 999999999u}}; + printf("lossless duration"); + for (size_t i = 0; i < sizeof durations / sizeof durations[0]; i++) { + api_duration d = api_same_duration(durations[i]); + printf(" %llu.%09u", (unsigned long long)d.secs, d.nanos); } printf("\n"); + printf("lossless u64 %llu %llu %llu\n", (unsigned long long)api_same_u64(0), + (unsigned long long)api_same_u64(1ull << 63), (unsigned long long)api_same_u64(UINT64_MAX)); + printf("lossless usize %zu %zu\n", api_same_usize(0), api_same_usize(SIZE_MAX)); + printf("lossless isize %td %td\n", api_same_isize(PTRDIFF_MIN), api_same_isize(PTRDIFF_MAX)); + api_u128 u128s[] = {{0, 0}, {0, 1}, {UINT64_MAX, UINT64_MAX}}; + printf("lossless u128"); + for (size_t i = 0; i < sizeof u128s / sizeof u128s[0]; i++) { + api_u128 v = api_same_u128(u128s[i]); + printf(" %016llx%016llx", (unsigned long long)v.hi, (unsigned long long)v.lo); + } + printf("\n"); + uint32_t chars[] = {0, 0xd7ff, 0xe000, 0x10ffff}; + printf("lossless char"); + for (size_t i = 0; i < sizeof chars / sizeof chars[0]; i++) { + printf(" %x", api_same_char(chars[i])); + } + printf("\n"); + api_buf same_paths[] = {str("a/\xc3\xa9"), {.ptr = (const uint8_t *)"\xff", .len = 1}}; + printf("lossless path"); + for (size_t i = 0; i < sizeof same_paths / sizeof same_paths[0]; i++) { + api_buf path = api_same_path(same_paths[i]); + printf(" "); + print_hex(path.ptr, path.len); + api_buf_free(path); + } + printf("\n"); + api_socket_addr_v4 sv4 = api_same_socket_addr_v4((api_socket_addr_v4){.ip = {{255, 255, 255, 255}}, .port = UINT16_MAX}); + api_socket_addr_v6 sv6 = api_same_socket_addr_v6( + (api_socket_addr_v6){.ip = {{0, 1, [15] = 2}}, .port = 3, .flowinfo = UINT32_MAX, .scope_id = UINT32_MAX}); + api_socket_addr sockets[] = {{.addr.v4 = sv4}, {.v6 = true, .addr.v6 = sv6}}; + printf("lossless socket"); + for (size_t i = 0; i < 2; i++) { + printf(" "); + print_socket_hex(sockets[i]); + } + for (size_t i = 0; i < 2; i++) { + printf(" "); + print_socket_hex(api_same_socket_addr(sockets[i])); + } + printf("\n"); + api_Key *key_a = api_Key_new(str("a")), *key_a2 = api_Key_new(str("a")), *key_b = api_Key_new(str("b")); + if (api_Key_hash(key_a) != api_Key_hash_of(key_a)) { + fprintf(stderr, "api_Key_hash differs from Rust's own hash\n"); + abort(); + } + printf("key %s %s\n", api_Key_hash(key_a) == api_Key_hash(key_a2) ? "true" : "false", + api_Key_hash(key_a) == api_Key_hash(key_b) ? "true" : "false"); + api_Key_free(key_a); + api_Key_free(key_a2); + api_Key_free(key_b); + printf("lossless ip "); + print_ip_hex(api_same_ip_addr((api_ip_addr){.ip.v4 = {{255, 255, 255, 255}}})); + printf(" "); + print_ip_hex(api_same_ip_addr((api_ip_addr){.v6 = true, .ip.v6 = {{0xff, 0xff, [15] = 1}}})); + printf("\n"); /* serde_json's map iterator, collected as pairs of a key and its document. */ api_tuple_string_Json section_pairs[2] = {{._0 = str("b")}, {._0 = str("a")}}; @@ -2234,10 +2332,6 @@ static int broken(const char *which) { api_Channel_recv_poll(recv, waker, &payload); api_buf_free(payload); api_Channel_recv_poll(recv, waker, &payload); - } else if (strcmp(which, "family") == 0) { - api_socket_addr v6 = {.ip = {[15] = 1}, .port = 443, .v6 = true}; - api_socket_addr next4, next6; - api_nexts(v6, v6, &next4, &next6); } else if (strcmp(which, "enum") == 0) { api_greet(str("x"), (api_Shape)2); } else if (strcmp(which, "lent") == 0 || strcmp(which, "lent moved") == 0) { @@ -2263,15 +2357,6 @@ static int broken(const char *which) { api_tail((api_foreign_reader){0}, 1, &bytes, &at, &err); } else if (strcmp(which, "bound") == 0) { api_count((api_bounds_u64){.start = {.kind = (api_bound)3}}); - } else if (strcmp(which, "ip family") == 0) { - api_ip_addr v6 = {.ip = {[15] = 1}, .v6 = true}; - api_ip_addr next4, next6; - api_next_ips(v6, v6, &next4, &next6); - } else if (strcmp(which, "ip") == 0) { - api_flip((api_ip_addr){.ip = {127, 0, 0, 1, 1}}); - } else if (strcmp(which, "socket") == 0) { - api_socket_addr addr = {.ip = {127, 0, 0, 1, 1}}; - api_ports((api_vec_socket_addr){.ptr = &addr, .len = 1}); } else if (strcmp(which, "future") == 0) { /* A future with no poll. */ api_after((api_foreign_future){0}, 1); @@ -2296,7 +2381,7 @@ static int broken(const char *which) { } else if (strcmp(which, "plain variant") == 0) { api_is_family(API_FAMILY_WASM); } else if (strcmp(which, "zero") == 0) { - api_next_hop((api_option_u64){.some = true, .value = 0}); + api_next_hop((api_option_non_zero_u64){.some = true, .value = 0}); } return 0; } diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 147b79365..97e956fc0 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -905,21 +905,22 @@ int script() { } api::socket_addr v4{}; - v4.ip[0] = 127; - v4.ip[3] = 1; - v4.port = 8080; + v4.addr.v4.ip.octets[0] = 127; + v4.addr.v4.ip.octets[3] = 1; + v4.addr.v4.port = 8080; api::socket_addr v6{}; - v6.ip[15] = 1; - v6.port = 443; v6.v6 = true; - v6.scope_id = 3; + v6.addr.v6.ip.octets[15] = 1; + v6.addr.v6.port = 443; + v6.addr.v6.scope_id = 3; auto ports = api::ports({v4, v6}); std::printf("ports [%u, %u]\n", ports[0], ports[1]); std::vector names; names.emplace_back(api::view("a")); names.emplace_back(api::view("b")); api::Peer peer = api::Peer{v6, names}.next("c"); - std::printf("peer Peer { addr: [::%u%%%u]:%u, names: ", peer.addr.ip[15], peer.addr.scope_id, peer.addr.port); + std::printf("peer Peer { addr: [::%u%%%u]:%u, names: ", peer.addr.addr.v6.ip.octets[15], peer.addr.addr.v6.scope_id, + peer.addr.addr.v6.port); print_strings(peer.names); std::printf(" } "); print_strings(peer.aliases()); @@ -1246,37 +1247,138 @@ int script() { std::printf("%llu", static_cast(numbered[i].get())); } std::printf("\n"); - api::socket_addr at4{}; - at4.ip[0] = 127; - at4.ip[3] = 1; + api::socket_addr_v4 at4{}; + at4.ip.octets[0] = 127; + at4.ip.octets[3] = 1; at4.port = 8080; - api::socket_addr at6{}; - at6.ip[15] = 1; + api::socket_addr_v6 at6{}; + at6.ip.octets[15] = 1; at6.port = 443; - at6.v6 = true; at6.scope_id = 3; auto [next4, next6] = api::nexts(at4, at6); - std::printf("nexts %u.%u.%u.%u %u %u %u\n", next4.ip[0], next4.ip[1], next4.ip[2], next4.ip[3], next4.port, next6.port, - next6.scope_id); + const auto &n4 = next4.ip.octets; + std::printf("nexts %u.%u.%u.%u %u %u %u\n", n4[0], n4[1], n4[2], n4[3], next4.port, next6.port, next6.scope_id); + auto print_hex = [](const std::uint8_t *bytes, std::size_t len) { + for (std::size_t b = 0; b < len; b++) { + std::printf("%02x", bytes[b]); + } + }; + auto print_ip = [&](const api::ip_addr &ip) { + if (ip.v6) { + print_hex(ip.ip.v6.octets, 16); + } else { + print_hex(ip.ip.v4.octets, 4); + } + }; + auto print_socket = [&](const api::socket_addr &addr) { + if (addr.v6) { + print_hex(addr.addr.v6.ip.octets, 16); + std::printf(":%u:%u:%u", addr.addr.v6.port, addr.addr.v6.flowinfo, addr.addr.v6.scope_id); + } else { + print_hex(addr.addr.v4.ip.octets, 4); + std::printf(":%u", addr.addr.v4.port); + } + }; { - api::ip_addr ip4{}, ip6{}, local4{}, local6{}; - ip4.ip[0] = 10; - ip4.ip[3] = 255; - ip6.ip[14] = ip6.ip[15] = 0xff; - ip6.v6 = true; - local4.ip[0] = 127; - local4.ip[3] = 1; - local6.ip[15] = 1; + api::ipv4_addr ip4{}; + ip4.octets[0] = 10; + ip4.octets[3] = 255; + api::ipv6_addr ip6{}; + ip6.octets[14] = ip6.octets[15] = 0xff; + api::ip_addr local4{}, local6{}, nextip4_{}, nextip6_{}; + local4.ip.v4.octets[0] = 127; + local4.ip.v4.octets[3] = 1; local6.v6 = true; + local6.ip.v6.octets[15] = 1; auto [nextip4, nextip6] = api::next_ips(ip4, ip6); + nextip4_.ip.v4 = nextip4; + nextip6_.v6 = true; + nextip6_.ip.v6 = nextip6; auto mapped = api::flip(local4); std::printf("ips"); - for (auto ip : {nextip4, nextip6, mapped, api::flip(mapped), api::flip(local6)}) { + for (auto ip : {nextip4_, nextip6_, mapped, api::flip(mapped), api::flip(local6)}) { std::printf(" "); - for (std::size_t b = 0; b < (ip.v6 ? 16u : 4u); b++) { - std::printf("%02x", ip.ip[b]); - } + print_ip(ip); + } + std::printf("\n"); + } + // Lossless round trips: each type's edge values, passed through Rust and back. + { + std::printf("lossless duration"); + for (api::duration ll_dur : {api::duration{0, 0}, api::duration{0, 1}, api::duration{9223372036u, 854775808u}, + api::duration{UINT64_MAX, 999999999u}}) { + ll_dur = api::same_duration(ll_dur); + std::printf(" %llu.%09u", static_cast(ll_dur.secs), ll_dur.nanos); + } + std::printf("\n"); + std::printf("lossless u64 %llu %llu %llu\n", static_cast(api::same_u64(0)), + static_cast(api::same_u64(1ull << 63)), + static_cast(api::same_u64(UINT64_MAX))); + std::printf("lossless usize %zu %zu\n", api::same_usize(0), api::same_usize(SIZE_MAX)); + std::printf("lossless isize %td %td\n", api::same_isize(PTRDIFF_MIN), api::same_isize(PTRDIFF_MAX)); + std::printf("lossless u128"); + for (api::u128 ll_wide : {api::u128{0, 0}, api::u128{0, 1}, api::u128{UINT64_MAX, UINT64_MAX}}) { + ll_wide = api::same_u128(ll_wide); + std::printf(" %016llx%016llx", static_cast(ll_wide.hi), static_cast(ll_wide.lo)); + } + std::printf("\n"); + std::printf("lossless char"); + for (char32_t ll_code : {U'\0', static_cast(0xd7ff), static_cast(0xe000), static_cast(0x10ffff)}) { + std::printf(" %x", static_cast(api::same_char(ll_code))); + } + std::printf("\n"); + // A path no Rust string spells: bytes that are not UTF-8, or on Windows, WTF-8's lone + // surrogate. +#ifdef _WIN32 + static const std::uint8_t ll_stray[] = {0xed, 0xa0, 0x80}; +#else + static const std::uint8_t ll_stray[] = {0xff}; +#endif + std::printf("lossless path"); + for (auto ll_same : {api::same_path("a/\xc3\xa9"), api::same_path(api::view(ll_stray, sizeof ll_stray))}) { + std::printf(" "); + print_hex(ll_same.data(), ll_same.size()); + } + std::printf("\n"); + api::socket_addr_v4 ll_sv4{}; + std::memset(ll_sv4.ip.octets, 0xff, 4); + ll_sv4.port = UINT16_MAX; + api::socket_addr_v6 ll_sv6{}; + ll_sv6.ip.octets[1] = 1; + ll_sv6.ip.octets[15] = 2; + ll_sv6.port = 3; + ll_sv6.flowinfo = ll_sv6.scope_id = UINT32_MAX; + api::socket_addr ll_sockets[2] = {}; + ll_sockets[0].addr.v4 = api::same_socket_addr_v4(ll_sv4); + ll_sockets[1].v6 = true; + ll_sockets[1].addr.v6 = api::same_socket_addr_v6(ll_sv6); + std::printf("lossless socket"); + for (const auto &ll_socket : ll_sockets) { + std::printf(" "); + print_socket(ll_socket); + } + for (const auto &ll_socket : ll_sockets) { + std::printf(" "); + print_socket(api::same_socket_addr(ll_socket)); + } + std::printf("\n"); + api::Key ll_key_a = api::Key::new_("a"), ll_key_b = api::Key::new_("b"); + if (ll_key_a.hash() != ll_key_a.hash_of()) { + std::fprintf(stderr, "Key::hash differs from Rust's own hash\n"); + std::exit(1); } + std::hash ll_hasher; + std::printf("key %s %s\n", ll_hasher(ll_key_a) == ll_hasher(api::Key::new_("a")) ? "true" : "false", + ll_hasher(ll_key_a) == ll_hasher(ll_key_b) ? "true" : "false"); + api::ip_addr ll_edge4{}, ll_edge6{}; + std::memset(ll_edge4.ip.v4.octets, 0xff, 4); + ll_edge6.v6 = true; + ll_edge6.ip.v6.octets[0] = ll_edge6.ip.v6.octets[1] = 0xff; + ll_edge6.ip.v6.octets[15] = 1; + std::printf("lossless ip "); + print_ip(api::same_ip_addr(ll_edge4)); + std::printf(" "); + print_ip(api::same_ip_addr(ll_edge6)); std::printf("\n"); } diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index dfc0f5e53..5d013028b 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -28,7 +28,7 @@ String bits64(Float64List v) => '[${v.buffer.asUint64List(v.offsetInBytes, v.len void numbers(String what, api.Numbers n) { print('$what u16 ${n.u16s}'); print('$what u32 ${n.u32s}'); - print('$what u64 [${n.u64s.map(u64).join(', ')}]'); + print('$what u64 [${n.u64s.map((v) => v.toRadixString(10)).join(', ')}]'); print('$what i8 ${n.i8s}'); print('$what i16 ${n.i16s}'); print('$what i32 ${n.i32s}'); @@ -60,14 +60,25 @@ String bound(api.BoundU64 b) => switch (b) { Uint8List bytes(List values) => Uint8List.fromList(values); +/// Bytes in hex, as transcript.rs prints them. +String hex(List bytes) => bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join(); + +/// A socket address as its IP's bytes in hex, its port, flow label, and scope, each +/// IPv6's alone, apart by colons, as transcript.rs prints it. +String socketHex(api.SocketAddr addr) => switch (addr) { + api.SocketAddrV4(:final ip, :final port) => '${hex(ip.octets)}:$port', + api.SocketAddrV6(:final ip, :final port, :final flowinfo, :final scopeId) => + '${hex(ip.octets)}:$port:$flowinfo:$scopeId', +}; + String text(Uint8List data) => utf8.decode(data); String quoted(List values) => '[${values.map((v) => '"$v"').join(', ')}]'; -/// A path's UTF-8 bytes as Rust's `escape_ascii` prints them. -String escaped(String path) { +/// A path's bytes as Rust's `escape_ascii` prints them. +String escaped(api.PathBuf path) { final out = StringBuffer(); - for (final b in utf8.encode(path)) { + for (final b in path.bytes) { switch (b) { case 0x09: out.write(r'\t'); @@ -149,17 +160,17 @@ Future script() async { final d = api.Point.default_(); print('default ${d.x} ${d.y}'); - final c = api.Counter('c', 5); + final c = api.Counter('c', api.U64(5)); print('get ${c.get()}'); print('name ${c.name()}'); - print('add ok ${c.add(10)}'); + print('add ok ${c.add(api.U64(10))}'); try { - c.add(2000); + c.add(api.U64(2000)); } on api.Error catch (e) { print('add err $e ${e.debug()}'); } try { - c.sub(20); + c.sub(api.U64(20)); } on api.Error catch (e) { print('sub err $e ${e.debug()}'); } @@ -167,7 +178,7 @@ Future script() async { print('name ${c.name()}'); print('is d ${c.is_('d')}'); final copy = c.clone(); - copy.add(1); + copy.add(api.U64(1)); print('clone ${copy.get()} original ${c.get()}'); copy.close(); print('debug ${c.debug()}'); @@ -175,22 +186,22 @@ Future script() async { print('label ${label.debug()}'); print('note ${api.hasText(const api.Note(text: ''))} ${api.hasText(const api.Note(text: null))}'); - final (below, above) = api.Counter('s', 7).split(5); + final (below, above) = api.Counter('s', api.U64(7)).split(api.U64(5)); print('split ${below.name()} ${below.get()} ${above.name()} ${above.get()}'); - final (only,) = api.only(api.Counter('o', 3)); + final (only,) = api.only(api.Counter('o', api.U64(3))); print('only ${only.name()} ${only.get()}'); - final (w8, w16, w32, w64) = api.widths(98305); + final (w8, w16, w32, w64) = api.widths(api.U64(98305)); print('widths $w8 $w16 $w32 $w64'); for (final b in [2, 0]) { try { - final (q, r) = api.divide(7, b); + final (q, r) = api.divide(api.U64(7), api.U64(b)); print('divide $q $r'); } on api.Error catch (e) { print('divide err $e'); } } for (final n in [98305, 0]) { - final spread = api.spread(n); + final spread = api.spread(api.U64(n)); print(spread == null ? 'spread none' : 'spread ${spread.$1} ${spread.$2} ${spread.$3} ${spread.$4}'); } for (final needle in ['lo', 'x']) { @@ -200,7 +211,7 @@ Future script() async { final statRows = api.rows([const api.Point(x: 1, y: 2), const api.Point(x: 3, y: 3)]); print('rows ${statRows.map((r) => '${r.$1} ${r.$2.debug()} ${r.$3.x} ${r.$3.y}').join(', ')}'); // Each handle in a tuple moves in, as one taken by value does. - final board = api.Board([('low', api.Counter('l', 1)), ('high', api.Counter('h', 9))]); + final board = api.Board([('low', api.Counter('l', api.U64(1))), ('high', api.Counter('h', api.U64(9)))]); final ranked = board.ranked(); print('ranked ${ranked.map((r) => '${r.$1} ${r.$2.name()} ${r.$2.get()}').join(', ')}'); final best = board.best(); @@ -247,26 +258,26 @@ Future script() async { ); // Each lent counter is a new handle to a clone. - final registry = api.Registry(api.Counter('first', 1)); + final registry = api.Registry(api.Counter('first', api.U64(1))); print('last ${registry.last() != null}'); - registry.push(api.Counter('last', 2)); + registry.push(api.Counter('last', api.U64(2))); final first = registry.firstGet; - first.add(10); + first.add(api.U64(10)); print('first ${registry.firstGet.get()} lent ${first.get()}'); final last = registry.last()!; - last.add(10); + last.add(api.U64(10)); print('last ${registry.last()!.get()} lent ${last.get()}'); // A closure lent for the call edits the first counter through a handle. const by = 5; final edited = registry.edit((counter) { - counter.add(by); + counter.add(api.U64(by)); return counter.edited() && counter.get() == 6; }); print('edit $edited ${registry.firstGet.get()}'); // A setter moves an object in, as assignment does, closing it. - registry.firstSet(api.Counter('second', 3)); + registry.firstSet(api.Counter('second', api.U64(3))); print('set first ${registry.firstGet.get()} ${registry.firstGet.name()}'); - print('walk ${api.walk(3, (cursor) => cursor.at() * 2)}'); + print('walk ${api.walk(api.U64(3), (cursor) => api.U64(cursor.at().toInt() * 2))}'); print('each ${api.each(2, (at) => print('visit $at'))}'); print('offset ${api.offset(() => 7, 3)}'); print('shift ${api.shift(() => 7, 4)}'); @@ -282,12 +293,12 @@ Future script() async { final lit = api.Name.borrowed('lit'); print('borrowed ${lit.asStr()} ${lit.shares(api.Name.borrowed('lit'))} ${lit.shares(api.Name.borrowed('other'))}'); - print('total ${api.total(const api.RangeU64(2, 5))}'); + print('total ${api.total(api.RangeU64(api.U64(2), api.U64(5)))}'); print('reach ${api.reach(2, 5)}'); final scaled = [ - api.scaled((BigInt.one << 128) - BigInt.one, 1), - api.scaled((BigInt.one << 64) + BigInt.one, 1 << 63), - api.scaled(BigInt.one << 127, 2), + api.scaled((BigInt.one << 128) - BigInt.one, api.U64(1)), + api.scaled((BigInt.one << 64) + BigInt.one, api.U64.fromBits(1 << 63)), + api.scaled(BigInt.one << 127, api.U64(2)), ]; print('scaled ${scaled.map((v) => v?.toRadixString(16).padLeft(32, '0') ?? 'none').join(' ')}'); final digest = api.Digest(); @@ -308,28 +319,28 @@ Future script() async { print('capacities ${api.capacities42(batch, batch2)} ${api.capacities22(batch2, batch2)}'); batch.close(); batch2.close(); - final wide = api.widen(const api.RangeU64(2, 5), 1); + final wide = api.widen(api.RangeU64(api.U64(2), api.U64(5)), api.U64(1)); print('widen ${wide.start} ${wide.end}'); final counts = [ - const api.BoundsU64(api.BoundU64Included(2), api.BoundU64Excluded(5)), - const api.BoundsU64(api.BoundU64Included(2), api.BoundU64Included(5)), - const api.BoundsU64(api.BoundU64Included(3), api.BoundU64Unbounded()), - const api.BoundsU64(api.BoundU64Unbounded(), api.BoundU64Unbounded()), - const api.BoundsU64(api.BoundU64Excluded(2), api.BoundU64Included(4)), + api.BoundsU64(api.BoundU64Included(api.U64(2)), api.BoundU64Excluded(api.U64(5))), + api.BoundsU64(api.BoundU64Included(api.U64(2)), api.BoundU64Included(api.U64(5))), + api.BoundsU64(api.BoundU64Included(api.U64(3)), api.BoundU64Unbounded()), + api.BoundsU64(api.BoundU64Unbounded(), api.BoundU64Unbounded()), + api.BoundsU64(api.BoundU64Excluded(api.U64(2)), api.BoundU64Included(api.U64(4))), ].map(api.count); print('count ${counts.join(' ')}'); - print('end ${bound(api.end(3))} ${bound(api.end(0))}'); - final (endsStart, endsEnd) = api.ends(3); + print('end ${bound(api.end(api.U64(3)))} ${bound(api.end(api.U64(0)))}'); + final (endsStart, endsEnd) = api.ends(api.U64(3)); print('ends ${bound(endsStart)} ${bound(endsEnd)}'); for (final n in [2, 5]) { try { - print('below ${api.below(n, 3)}'); + print('below ${api.below(api.U64(n), api.U64(3))}'); } on api.BoundsU64Exception catch (e) { print('below err ${bound(e.error.start)} ${bound(e.error.end)}'); } } - final window = api.Window(range: const api.RangeU64(2, 8), end: api.end(5)); - print('window ${window.contains(4)} ${window.contains(6)} ${window.debug()}'); + final window = api.Window(range: api.RangeU64(api.U64(2), api.U64(8)), end: api.end(api.U64(5))); + print('window ${window.contains(api.U64(4))} ${window.contains(api.U64(6))} ${window.debug()}'); print(api.greet('x', api.Shape.square)); print('len ${api.len(utf8.encode('abc'))}'); @@ -347,9 +358,9 @@ Future script() async { } // A `Copy` handle passed by value is copied, so it stays usable. - final stamp = api.Stamp.fromMillis(2500); + final stamp = api.Stamp.fromMillis(api.U64(2500)); print('secs ${stamp.secs()}'); - print('later ${api.Stamp.fromMillis(3000).later(stamp)}'); + print('later ${api.Stamp.fromMillis(api.U64(3000)).later(stamp)}'); print('secs ${stamp.secs()}'); print('whole ${stamp.whole().debug()}'); final message = api.Message(stamp, utf8.encode('body')); @@ -357,7 +368,7 @@ Future script() async { print('stamp ${message.stampGet!.debug()}'); print('sent ${message.sent()}'); // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. - final reset = api.Stamp.fromMillis(4000); + final reset = api.Stamp.fromMillis(api.U64(4000)); message.stampSet(reset); message.payloadSet(utf8.encode('edited')); print('payload ${text(message.payloadGet)}'); @@ -367,22 +378,22 @@ Future script() async { 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}'); + final moment = api.Moment.new_(api.U64(1500), api.U64(1000))!; + print('moment ${moment.millis()} ${api.Moment.new_(api.U64(1), api.U64(0)) == null}'); + final shifted = api.shifted(moment, (m) => m.after(api.U64(500))); + print('shifted ${shifted.millis()} ${shifted == moment.after(api.U64(500))}'); + print('wrapped ${moment.after(api.U64.max).millis()} ${moment == shifted}'); // A value with padding, or bytes that vary, is a handle, as no table says where padding is. - final gap = api.Gap(7, 3); - print('gap ${gap.sum()} ${gap == api.Gap(7, 3)}'); + final gap = api.Gap(api.U64(7), 3); + print('gap ${gap.sum()} ${gap == api.Gap(api.U64(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 value, which a `&mut self` method returns as the // call left it, Rust's own return beside it: a change reaches only the copy it replaces. - var score = api.Score.of(2); - final increment = api.Score.of(3); + var score = api.Score.of(api.U64(2)); + final increment = api.Score.of(api.U64(3)); score = score.add(increment); score = score.add(increment); print('score ${score.count()} ${increment.doubled().count()} ${increment.count()}'); @@ -392,28 +403,28 @@ Future script() async { final (absorbed, before0, before1) = scoreCopy.absorb(otherScore); scoreCopy = absorbed; print('absorbed $before0 $before1 ${scoreCopy.count()} ${otherScore.value.count()} ${increment.count()}'); - var hits = api.Hits.of(1, 2); + var hits = api.Hits.of(api.U64(1), api.U64(2)); final keptHits = hits; - hits = hits.add(api.Hits.of(10, 20)); - hits = hits.hitsSet(7); + hits = hits.add(api.Hits.of(api.U64(10), api.U64(20))); + hits = hits.hitsSet(api.U64(7)); print('hits ${hits.hitsGet} ${hits.missesGet} ${keptHits.hitsGet} ${keptHits.missesGet} ${hits == keptHits}'); final (counted, countedSame) = keptHits.eq(keptHits); print('counted $countedSame ${counted.hitsGet}'); - var extent = api.Extent(5, 3); + var extent = api.Extent(api.U64(5), api.U64(3)); final extentStart = extent; - final (slid, old) = extent.slide(10); + final (slid, old) = extent.slide(api.U64(10)); extent = slid; - extent = extent.startSet(extent.startGet + 1); + extent = extent.startSet(api.U64(extent.startGet.toInt() + 1)); print('extent $old ${extent.startGet} ${extent.end()} ${extentStart.startGet} ${extentStart.end()}'); final weighted = api.Weighted(score: increment, weight: 4); - print('weighted ${weighted.total()} ${weighted.score.count() + increment.count()}'); + print('weighted ${weighted.total()} ${weighted.score.count().toInt() + increment.count().toInt()}'); // A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. - final mark = api.Mark.of(1); - mark.add(2); + final mark = api.Mark.of(api.U64(1)); + mark.add(api.U64(2)); final parcel = api.Parcel(mark: mark, payload: bytes([97, 98, 99])); - print('heft ${api.heft((mark, bytes([97, 98])), parcel, (api.Counter('w', 1), bytes([97, 98, 99, 100])))}'); + print('heft ${api.heft((mark, bytes([97, 98])), parcel, (api.Counter('w', api.U64(1)), bytes([97, 98, 99, 100])))}'); // A value's `&mut self` beside a checked handle. - final (noted, notedBefore) = api.Score.of(2).note(api.Mark.of(5)); + final (noted, notedBefore) = api.Score.of(api.U64(2)).note(api.Mark.of(api.U64(5))); print('noted $notedBefore ${noted.count()}'); print('kept ${api.emit(PrintLog(), bytes([1, 2, 3, 4]))}'); @@ -470,7 +481,7 @@ Future script() async { // A poll with no async twin is a future. print('take ${text(await channel.pollTake())}'); channel.send(utf8.encode('measured')); - print('measure ${await channel.pollMeasure(2)}'); + print('measure ${await channel.pollMeasure(api.U64(2))}'); // A context is made of the waker, which Rust keeps until the send wakes it. channel.listen(() => wakes++); channel.send(utf8.encode('heard')); @@ -490,8 +501,8 @@ Future script() async { await until(() => wakes == 5, 'a returned waker wakes the waiter'); print('woken $wakes'); - final nodes = api.Node(1, null); - nodes.push(2); + final nodes = api.Node(api.U64(1), null); + nodes.push(api.U64(2)); print('node ${nodes.sum()}'); final tally = api.Tally(); @@ -518,7 +529,7 @@ Future script() async { // At its default, named as the type itself. final stack = api.Stack(); for (final (i, n) in ['a', 'b'].indexed) { - stack.push(api.Counter(n, i + 1)); + stack.push(api.Counter(n, api.U64(i + 1))); } final top = stack.pop()!.get(); print('stack ${stack.depth()} Some($top) ${stack.pop() != null} ${stack.pop() == null}'); @@ -596,14 +607,14 @@ Future script() async { } on api.StringException catch (e) { print('parse err ${e.error}'); } - final v4 = api.SocketAddr(InternetAddress('127.0.0.1'), 8080); - final v6 = api.SocketAddr(InternetAddress('::1'), 443, scopeId: 3); + final v4 = api.SocketAddrV4(api.Ipv4Addr.from(InternetAddress('127.0.0.1')), 8080); + final v6 = api.SocketAddrV6(api.Ipv6Addr.from(InternetAddress('::1')), 443, scopeId: 3); print('ports ${api.ports([v4, v6])}'); final peer = api.Peer(addr: v6, names: ['a', 'b']).next('c'); print('peer ${peer.debug()} ${quoted(peer.aliases())}'); - final file = api.file('dir', 'f\\\'"\t'); + final file = api.file(api.PathBuf('dir'), 'f\\\'"\t'); print('file ${escaped(file)}'); - print('parents ${api.parents([file, '/']).map((p) => p == null ? 'none' : escaped(p)).join(' ')}'); + print('parents ${api.parents([file, api.PathBuf('/')]).map((p) => p == null ? 'none' : escaped(p)).join(' ')}'); final words = api.words(' one two three '); print('words ${words.length} ${quoted(words)}'); print('sentence ${api.sentence(words)} ${api.sentence(null)}'); @@ -612,8 +623,8 @@ Future script() async { print('prefixed ${utf8.decode(api.prefixed(utf8.encode('cd'), utf8.encode('ab')))} ' '${utf8.decode(api.prefixed(utf8.encode('cd'), null))}'); print('coordinates ${api.coordinates(const api.Point(x: 2, y: 3))} ${api.coordinates(null)}'); - final two = api.Counter('two', 2); - final three = api.Counter('three', 3); + final two = api.Counter('two', api.U64(2)); + final three = api.Counter('three', api.U64(3)); print('plus ${two.plus(three)} ${two.plus(null)}'); print('add_to ${two.addTo(three)} ${two.addTo(null)} ${three.get()}'); two.close(); @@ -655,7 +666,7 @@ Future script() async { print('failure ${api.failureMessage(failure)} ${api.failureKind(failure)}'); // Iterators, passed as the arrays of their items. print('sum_all ${api.sumAll([1, 2, 3])}'); - print('weigh_all ${api.weighAllCounter([api.Counter('w', 3)])}'); + print('weigh_all ${api.weighAllCounter([api.Counter('w', api.U64(3))])}'); print('offered_len ${api.offeredLen([utf8.encode('h3'), utf8.encode('moq-00')])}'); print('join_words ${api.joinWords(['a', 'b', 'c'])}'); // Bytes that must be `'static`, interned. @@ -674,28 +685,28 @@ Future script() async { } final meters = api.Meters(); final lentMeter = meters.first(); - lentMeter.add(2); - print('lent ${lentMeter.add(3)} ${meters.first().count()}'); + lentMeter.add(api.U64(2)); + print('lent ${lentMeter.add(api.U64(3))} ${meters.first().count()}'); lentMeter.close(); meters.clear(); print('cleared ${meters.first().count()}'); final lastMeter = meters.lastGet; - lastMeter.add(4); - print('field lent ${lastMeter.add(1)}'); + lastMeter.add(api.U64(4)); + print('field lent ${lastMeter.add(api.U64(1))}'); lastMeter.close(); // An optional field that is not Clone, lent as `Some` of a handle. print('spare ${meters.spareGet != null}'); meters.spareSet(api.Meter.default_()); final spare = meters.spareGet!; - spare.add(5); - print('spare lent ${spare.add(1)}'); + spare.add(api.U64(5)); + print('spare lent ${spare.add(api.U64(1))}'); spare.close(); meters.close(); final tallies = api.Tallies(); tallies.first().bump(); print('tallied ${tallies.first().bump()}'); tallies.close(); - print('chunks ${api.chunks(utf8.encode('abcde'), 2)}'); + print('chunks ${api.chunks(utf8.encode('abcde'), api.U64(2))}'); final counters = api.counters(['x', 'y']); print('counters ${counters.map((c) => '${c.name()} ${c.get()}').join(', ')}'); // Each counter moves in, so a later call throws. @@ -703,7 +714,7 @@ Future script() async { for (final counter in counters) { closed(counter.get, 'Counter', 'a counter moved in is closed'); } - final held = [api.heldCount((api.Counter('h', 4),)), api.heldCount(null)]; + final held = [api.heldCount((api.Counter('h', api.U64(4)),)), api.heldCount(null)]; print('held ${held.map((n) => n == null ? 'None' : 'Some($n)').join(' ')}'); print( 'boxed Some("${api.boxedName(api.boxedCounter('b'))}") ' @@ -733,8 +744,13 @@ Future script() async { print('longest ${api.longest(['a', 'abc'])} ${api.longest([])}'); print('first of Some("${api.firstOf(['a', 'b'])}") ${api.firstOf([]) ?? 'None'}'); final limits = api.byteLimits(utf8.encode('a'), utf8.encode('bc')); - final waits = api.breadth(const api.LimitsDuration(low: Duration(milliseconds: 5), high: Duration(seconds: 2))); - print('limits ${utf8.decode(limits.low)} ${utf8.decode(limits.high)} breadth ${waits.inMilliseconds}'); + final waits = api.breadth( + api.LimitsDuration( + low: api.RustDuration.from(const Duration(milliseconds: 5)), + high: api.RustDuration.from(const Duration(seconds: 2)), + ), + ); + print('limits ${utf8.decode(limits.low)} ${utf8.decode(limits.high)} breadth ${waits.toDuration().inMilliseconds}'); final parts = utf8.decode(api.joined([utf8.encode('ab'), Uint8List(0), utf8.encode('c')])); print('joined $parts ${api.joined([]).length} read ${api.readLen(utf8.encode('hello'))}'); final doc = api.Json.parse('{"b": [1, 2.5, null], "a": "é"}'); @@ -778,11 +794,53 @@ Future script() async { final numbered = api.numbered(['p', 'q']); print('numbered ${numbered.map((c) => '${c.name()} ${c.get()}').join(', ')}'); final (next4, next6) = api.nexts(v4, v6); - print('nexts ${next4.address.address} ${next4.port} ${next6.port} ${next6.scopeId}'); - final (nextIp4, nextIp6) = api.nextIps(InternetAddress('10.0.0.255'), InternetAddress('::ffff')); - final mapped = api.flip(InternetAddress('127.0.0.1')); - final ips = [nextIp4, nextIp6, mapped, api.flip(mapped), api.flip(InternetAddress('::1'))]; - print('ips ${ips.map((ip) => ip.rawAddress.map((b) => b.toRadixString(16).padLeft(2, '0')).join()).join(' ')}'); + print('nexts ${next4.ip} ${next4.port} ${next6.port} ${next6.scopeId}'); + final (nextIp4, nextIp6) = api.nextIps( + api.Ipv4Addr.from(InternetAddress('10.0.0.255')), + api.Ipv6Addr.from(InternetAddress('::ffff')), + ); + final mapped = api.flip(api.IpAddr.from(InternetAddress('127.0.0.1'))); + final ips = [nextIp4, nextIp6, mapped, api.flip(mapped), api.flip(api.IpAddr.from(InternetAddress('::1')))]; + print('ips ${ips.map((ip) => hex(ip.octets)).join(' ')}'); + + // Lossless round trips: each type's edge values, passed through Rust and back. + final durations = [ + api.RustDuration.zero, + api.RustDuration(api.U64(0), 1), + api.RustDuration(api.U64(9223372036), 854775808), + api.RustDuration(api.U64.max, 999999999), + ].map(api.sameDuration); + print('lossless duration ${durations.map((d) => '${d.secs.toRadixString(10)}.${d.nanos.toString().padLeft(9, '0')}').join(' ')}'); + final u64s = [api.U64(0), api.U64.fromBigInt(BigInt.one << 63), api.U64.max]; + print('lossless u64 ${u64s.map((v) => api.sameU64(v).toRadixString(10)).join(' ')}'); + print('lossless usize ${[api.U64(0), api.U64.max].map((v) => api.sameUsize(v).toRadixString(10)).join(' ')}'); + print('lossless isize ${api.sameIsize(-0x8000000000000000)} ${api.sameIsize(0x7fffffffffffffff)}'); + final u128s = [BigInt.zero, BigInt.one << 64, (BigInt.one << 128) - BigInt.one]; + print('lossless u128 ${u128s.map((v) => api.sameU128(v).toRadixString(16).padLeft(32, '0')).join(' ')}'); + final chars = ['\u{0}', '\u{d7ff}', '\u{e000}', '\u{10ffff}']; + print('lossless char ${chars.map((c) => api.sameChar(c).runes.single.toRadixString(16)).join(' ')}'); + // A path no Rust string spells: bytes that are not UTF-8, or on Windows, WTF-8's lone surrogate. + final unspelled = api.PathBuf.fromBytes(Platform.isWindows ? [0xed, 0xa0, 0x80] : [0xff]); + print('lossless path ${[api.PathBuf('a/é'), unspelled].map((p) => hex(api.samePath(p).bytes)).join(' ')}'); + final sockV4 = api.sameSocketAddrV4(api.SocketAddrV4(api.Ipv4Addr.fromOctets([255, 255, 255, 255]), 65535)); + final sockV6 = api.sameSocketAddrV6( + api.SocketAddrV6( + api.Ipv6Addr.fromOctets([0, 1, for (var i = 0; i < 13; i++) 0, 2]), + 3, + flowinfo: 0xffffffff, + scopeId: 0xffffffff, + ), + ); + final socks = [sockV4, sockV6].map(api.sameSocketAddr); + print('lossless socket ${socketHex(sockV4)} ${socketHex(sockV6)} ${socks.map(socketHex).join(' ')}'); + // A key hashes by Rust's `Hash`: equal keys alike, and these two apart. + final key = api.Key('a'); + print('key ${key.hashCode == api.Key('a').hashCode} ${key.hashCode == api.Key('b').hashCode}'); + final sameIps = [ + api.sameIpAddr(api.Ipv4Addr.fromOctets([255, 255, 255, 255])), + api.sameIpAddr(api.Ipv6Addr.fromOctets([0xff, 0xff, for (var i = 0; i < 13; i++) 0, 1])), + ]; + print('lossless ip ${sameIps.map((ip) => hex(ip.octets)).join(' ')}'); // serde_json's map iterator, collected as pairs of a key and its document. final sections = api.sections([('b', api.Json.parse('[1]')), ('a', api.Json.parse('2'))]); @@ -792,9 +850,12 @@ Future script() async { } // An instant only Rust makes, which each call copies out of its handle, 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}'); + final ended = api.later(began, api.RustDuration.from(const Duration(milliseconds: 1500))); + final same = api.later(began, api.RustDuration.zero); + print( + 'instants ${api.since(ended, began).toDuration().inMilliseconds} ' + '${api.since(began, ended).toDuration().inMilliseconds} ${began == same} ${began == ended}', + ); print('has_instant ${api.hasInstant(began)} ${api.hasInstant(null)}'); // tracing's level, and its filter as an optional level whose null is `OFF`. final verbosity = api.Verbosity(api.Level.warn); @@ -811,7 +872,7 @@ Future script() async { final copied = api.reissue(api.Ticket(7), (_) => nine).number(); print('reissued $renewed $copied, cloned ${api.Ticket.clones()}'); // A record's traits borrow the checked handle it holds. - final nines = api.Mark.of(9); + final nines = api.Mark.of(api.U64(9)); final front = api.seat(nines, 1); final back = api.seat(nines, 2); print('seat ${front.debug()} ${front == api.seat(nines, 1)} ${front == back}'); @@ -820,16 +881,16 @@ Future script() async { print('spawned ${spawned.join()}'); // A kept closure: called again at the instant it asks for, then once its waiter wakes, // failing with the exception the binding throws for Rust's error. - final start = api.Moment.new_(0, 1000)!; + final start = api.Moment.new_(api.U64(0), api.U64(1000))!; var calls = 0; final stopped = await within( api.drive(start, (now, waiter) { calls++; switch (calls) { case 1: - return now.after(5); + return now.after(api.U64(5)); case 2: - print('drive ${now.millis() - start.millis()}'); + print('drive ${now.millis().toInt() - start.millis().toInt()}'); final waker = waiter.waker(); waker(); waker.close(); @@ -846,7 +907,7 @@ Future script() async { final opened = (File('${dir.path}/tail')..writeAsStringSync('hello world')).openSync(); for (final reader in [api.SeekableReader.file(opened), Broken()]) { try { - final (tail, at) = api.tail(reader, 5); + final (tail, at) = api.tail(reader, api.U64(5)); print('tail ${text(tail)} $at'); } on api.StringException catch (e) { print('tail error $e'); @@ -856,15 +917,14 @@ Future script() async { dir.deleteSync(recursive: true); // A seek from the start reaches the reader whole, past `i64::MAX`, as `Cursor` takes one. try { - print('sought ${BigInt.from(api.seekTo(Far(), -1)).toUnsigned(64)}'); + print('sought ${api.seekTo(Far(), api.U64.max).toRadixString(10)}'); } on api.StringException catch (e) { print('sought error $e'); } print('non zeros ${api.describeNonZeros(api.nonZeros())}'); - // A `u64` is its bits, so -1 is `u64::MAX`. - for (final hop in [null, 1, -1]) { + for (final hop in [null, api.U64(1), api.U64.max]) { final next = api.nextHop(hop); - print(next == null ? 'hop None' : 'hop Some(${BigInt.from(next).toUnsigned(64)})'); + print(next == null ? 'hop None' : 'hop Some(${next.toRadixString(10)})'); } print('farther hop ${api.fartherHop(const api.PairNonZeroU16(left: 3, right: 0xFFFF))}'); } @@ -1201,7 +1261,7 @@ void targets() { lacks(() => api.Page.default_(), 'api_Page_default'); lacks(() => const api.Page(size: 1).debug(), 'api_Page_debug'); lacks(() => const api.Page(size: 1) == const api.Page(size: 2), 'api_Page_eq'); - final booth = api.Booth(mark: api.Mark.of(1)); + final booth = api.Booth(mark: api.Mark.of(api.U64(1))); lacks(() => booth.debug(), 'api_Booth_debug'); lacks(() => booth == api.Booth(mark: booth.mark), 'api_Booth_eq'); } @@ -1218,7 +1278,7 @@ void unclosedLogger(api.Log log) { /// Each call taking a closed object throws, however it was closed, and moves nothing. void closedObjects() { - final c = api.Counter('c', 1); + final c = api.Counter('c', api.U64(1)); c.close(); closed(c.get, 'Counter', 'a method of a closed object'); closed(c.clone, 'Counter', 'clone of a closed object'); @@ -1230,16 +1290,16 @@ void closedObjects() { tally.close(); closed(() => tally == api.Tally(), 'Tally', '== of a closed object'); closed(() => api.Tally() == tally, 'Tally', '== with a closed object'); - final registry = api.Registry(api.Counter('first', 1)); + final registry = api.Registry(api.Counter('first', api.U64(1))); registry.close(); closed(() => registry.firstGet, 'Registry', "a field's getter of a closed object"); - final second = api.Counter('second', 2); + final second = api.Counter('second', api.U64(2)); closed(() => registry.firstSet(second), 'Registry', "a field's setter of a closed object"); check(second.get() == 2, 'a call that throws moves nothing in'); // An argument, alone, or in a list, beside others the call then moves none of. closed(() => api.bumpBoth(api.Tally(), tally), 'Tally', 'a closed argument'); - final open = api.Counter('open', 3); + final open = api.Counter('open', api.U64(3)); closed(() => api.worth([open, c], []), 'Counter', 'a closed object in a list'); closed(() => api.worth([open], [c]), 'Counter', 'a closed object in a later list'); closed(() => api.Board([('open', open), ('closed', c)]), 'Counter', 'a closed object in a tuple'); @@ -1269,14 +1329,14 @@ void closedObjects() { // Closed by a call that moved it, plain or boxed, or once the closure it was lent to // returned. - final moved = api.Counter('m', 0); + final moved = api.Counter('m', api.U64(0)); moved.intoLabel(null); closed(moved.get, 'Counter', 'a method of a moved object'); - final tail = api.Node(1, null); - api.Node(2, tail); + final tail = api.Node(api.U64(1), null); + api.Node(api.U64(2), tail); closed(tail.sum, 'Node', 'a method of a boxed moved object'); late api.Counter kept; - api.Registry(api.Counter('c', 0)).edit((counter) { + api.Registry(api.Counter('c', api.U64(0))).edit((counter) { kept = counter; return false; }); @@ -1284,7 +1344,7 @@ void closedObjects() { // Closed from the closure a call lends while the call borrows it, an object throws, as // Rust refuses a drop under a borrow, and changes nothing; closed after, it closes. - final editing = api.Registry(api.Counter('e', 4)); + final editing = api.Registry(api.Counter('e', api.U64(4))); final edited = editing.edit((counter) { borrowed(editing.close, 'api_Registry_drop: a handle is already borrowed', 'a close in a closure the call lends'); return counter.get() == 4; @@ -1391,30 +1451,98 @@ Uint8List carry(Uint8List payload) => api.nextPacket(api.Packet(seq: 1, payload: @pragma('vm:never-inline') Uint8List subView(int id) => Uint8List.sublistView(api.tokenBytes(id, 65537), 65527); -/// Values at the edges of what Rust takes and returns. +/// Values at the edges of what Rust takes and returns, and each value Rust's type lacks, +/// which throws the language's own error rather than reaching Rust. void bounds() { // A lone surrogate is one rune but no Unicode scalar. check(api.nextChar('a') == 'b' && api.nextChar('\u{1f600}') == '\u{1f601}', 'a char'); throws(() => api.nextChar(String.fromCharCode(0xd800)), 'a low surrogate'); throws(() => api.nextChar(String.fromCharCode(0xdfff)), 'a high surrogate'); + throws(() => api.nextChar('ab'), 'two chars'); check(api.nextChar('\u{d7ff}') == null, 'the scalar before the surrogates'); - throws(() => api.scaled(BigInt.one << 128, 1), 'an int past u128'); - throws(() => api.scaled(-BigInt.one, 1), 'a negative int for a u128'); - - // Dart's longest Duration is 9223372036854 seconds and 775807 microseconds. - for (final us in [9223372036854000000, 9223372036854775806]) { - final d = Duration(microseconds: us); - check(api.extended(d, Duration.zero) == d, 'a Duration near the longest, $us'); + throws(() => api.scaled(BigInt.one << 128, api.U64(1)), 'an int past u128'); + throws(() => api.scaled(-BigInt.one, api.U64(1)), 'a negative int for a u128'); + + // Text is UTF-8 in Rust, which a lone surrogate has no form in: Dart's encoder would + // replace it. + check(api.sameString('\u{1f600}é\u{10ffff}') == '\u{1f600}é\u{10ffff}', 'a pair of surrogates'); + for (final lone in ['\u{d800}', 'a\u{dc00}', '\u{dbff}\u{d800}', '\u{dc00}\u{d800}', 'é\u{d800}']) { + throws(() => api.sameString(lone), 'a lone surrogate in ${lone.codeUnits}'); } + throws(() => api.PathBuf('\u{dc00}'), 'a path of a lone surrogate'); + throws(() => api.nextHop(api.U64(0)), 'a zero NonZero'); + throws(() => api.fartherHop(const api.PairNonZeroU16(left: 0, right: 1)), 'a zero NonZero field'); + + // A `u64` is its bits read unsigned, checked into and out of an `int` or a `BigInt`. + check(api.U64.max.toBigInt() == (BigInt.one << 64) - BigInt.one, 'u64::MAX as a BigInt'); + check(api.U64.max.toRadixString(10) == '18446744073709551615', 'u64::MAX in decimal'); + check(api.U64.max.toRadixString(16) == 'ffffffffffffffff', 'u64::MAX in hex'); + check(api.U64(0x7fffffffffffffff).toInt() == 0x7fffffffffffffff, 'i64::MAX as an int'); + check(api.U64(1) < api.U64.max && api.U64.max > api.U64(0x7fffffffffffffff), 'a u64 compares unsigned'); + check(api.U64.fromBigInt(BigInt.one << 63).bits == -0x8000000000000000, 'i64::MAX + 1 as its bits'); + throws(() => api.U64(-1), 'a negative int for a u64'); + throws(() => api.U64.fromBigInt(BigInt.one << 64), 'a BigInt past u64'); + throws(() => api.U64.fromBigInt(-BigInt.one), 'a negative BigInt for a u64'); + throws(() => api.U64.max.toInt(), 'a u64 past int'); + + // A Rust Duration is its seconds and nanoseconds, checked into and out of a Duration, + // whose longest is 9223372036854 seconds and 775807 microseconds. const longest = Duration(microseconds: 0x7fffffffffffffff); - check(api.extended(longest, Duration.zero) == longest, 'the longest Duration'); + for (final d in [Duration.zero, const Duration(microseconds: 9223372036854000000), longest]) { + final same = api.extended(api.RustDuration.from(d), api.RustDuration.zero)!; + check(same.toDuration() == d, 'a Duration crosses whole, $d'); + } + check(api.extended(api.RustDuration(api.U64.max, 999999999), api.RustDuration(api.U64(0), 1)) == null, 'past the longest'); + throws(() => api.RustDuration(api.U64(0), 1000000000), 'a second of nanoseconds'); + throws(() => api.RustDuration(api.U64(0), -1), 'negative nanoseconds'); + throws(() => api.RustDuration.from(const Duration(microseconds: -1)), 'a negative Duration'); + throws(() => api.RustDuration(api.U64(0), 1).toDuration(), 'a nanosecond as a Duration'); + throws(() => api.RustDuration(api.U64(9223372036854), 775808000).toDuration(), 'past the longest Duration'); + check(api.RustDuration(api.U64(1)).compareTo(api.RustDuration(api.U64.max)) < 0, 'a Duration compares unsigned'); + + // A path is its bytes, which a String may not hold. + final stray = api.stray(); + check(api.samePath(stray) == stray && stray.bytes.single == 0xff, 'a path that is not UTF-8'); + check('$stray' == '\u{fffd}', "a path's display replaces what is not UTF-8"); + throws(stray.toFilePath, 'a path that is not UTF-8 as a String'); + api.PathBuf? lent; + api.visitStray((path) => lent = path); + check(lent == stray, 'a lent path that is not UTF-8'); + check(api.PathBuf('a/é').toFilePath() == 'a/é', 'a UTF-8 path as a String'); + final given = [0xff]; + final made = api.PathBuf.fromBytes(given); + given[0] = 0; + check(made == stray && api.samePath(made) == stray, "a caller's bytes make a path, copied"); + throws(() => api.PathBuf.fromBytes([0x100]), '256 is no path byte'); + throws(() => api.PathBuf.fromBytes([-1]), '-1 is no path byte'); + + // An address is its family's, checked out of an InternetAddress. + final loopback = InternetAddress('::1'); + check(api.IpAddr.from(loopback).toInternetAddress() == loopback, 'an IPv6 address both ways'); + check(api.IpAddr.from(InternetAddress('10.0.0.1')) is api.Ipv4Addr, 'an IPv4 address'); + throws(() => api.Ipv4Addr.from(loopback), 'an IPv6 address for an Ipv4Addr'); + throws(() => api.Ipv6Addr.from(InternetAddress('10.0.0.1')), 'an IPv4 address for an Ipv6Addr'); + throws(() => api.Ipv6Addr.from(InternetAddress('fe80::1%1')), 'an IPv6 address with a scope'); + throws( + () => api.IpAddr.from(InternetAddress('/tmp/api', type: InternetAddressType.unix)), + 'a Unix address', + ); + throws(() => api.Ipv4Addr.fromOctets([1, 2, 3]), 'three octets'); + throws(() => api.Ipv4Addr.fromOctets([256, 0, 0, 0]), 'an octet past 255'); + final ip = api.Ipv6Addr.from(loopback); + check(api.ports([api.SocketAddrV6(ip, 65535)]).single == 65535, 'the last port'); + throws(() => api.SocketAddrV6(ip, 65536), 'a port past u16'); + throws(() => api.SocketAddrV6(ip, 1, scopeId: -1), 'a negative scope'); + throws(() => api.SocketAddrV6(ip, 1, flowinfo: 0x100000000), 'a flow label past u32'); + + // A key hashes by Rust's Hash, so a Set holds each once. + check({api.Key('a'), api.Key('a'), api.Key('b')}.length == 2, 'a set of keys'); - check(api.ports([api.SocketAddr(InternetAddress('::1'), 65535)]).single == 65535, 'the last port'); // A block crosses whole: an empty one stays apart from none, and a u64 keeps its bits. api.Numbers blocks(Int32List? some) => api.Numbers( u16s: Uint16List(0), u32s: Uint32List(0), - u64s: Uint64List.fromList([-1]), + u64s: [api.U64.max], i8s: Int8List(0), i16s: Int16List(0), i32s: Int32List(0), @@ -1425,15 +1553,9 @@ void bounds() { ); check(blocks(null).reversed().some == null, 'no block crosses as none'); check(blocks(Int32List(0)).reversed().some?.isEmpty == true, 'an empty block crosses as itself'); - check(blocks(null).reversed().u64s.single == -1, 'u64::MAX crosses as its bits'); + check(blocks(null).reversed().u64s.single == api.U64.max, 'u64::MAX crosses whole'); final big = Uint32List.fromList(List.generate(1000, (i) => i)); check(api.sumU32(big) == 499500, 'a thousand u32s cross whole'); - throws(() => api.ports([api.SocketAddr(InternetAddress('::1'), 65536)]), 'a port past u16'); - throws(() => api.ports([api.SocketAddr(InternetAddress('::1'), 1, scopeId: -1)]), 'a negative scope'); - throws( - () => api.ports([api.SocketAddr(InternetAddress('::1'), 1, flowinfo: 0x100000000)]), - 'a flow label past u32', - ); print('ok'); } @@ -1459,12 +1581,12 @@ void broken(String which) { } switch (which) { case 'moved': - final c = api.Counter('c', 0); + final c = api.Counter('c', api.U64(0)); c.intoLabel(null); c.get(); case 'boxed moved': - final tail = api.Node(1, null); - api.Node(2, tail); + final tail = api.Node(api.U64(1), null); + api.Node(api.U64(2), tail); tail.sum(); case 'outlived': final meters = api.Meters(); @@ -1472,13 +1594,13 @@ void broken(String which) { meters.clear(); first.count(); case 'lent moved': - api.Registry(api.Counter('c', 0)).edit((counter) { + api.Registry(api.Counter('c', api.U64(0))).edit((counter) { counter.intoLabel(null); return false; }); case 'lent kept': late api.Counter kept; - api.Registry(api.Counter('c', 0)).edit((counter) { + api.Registry(api.Counter('c', api.U64(0))).edit((counter) { kept = counter; return false; }); @@ -1487,26 +1609,10 @@ void broken(String which) { api.emit(CountLog(() => throw StateError('a log failed')), bytes([1])); // Only the exception of Rust's error fails a closure. case 'closure threw': - api.drive(api.Moment.new_(0, 1000)!, (_, _) => throw StateError('a poll failed')); + api.drive(api.Moment.new_(api.U64(0), api.U64(1000))!, (_, _) => throw StateError('a poll failed')); // Only an Exception fails a reader. case 'reader threw': - api.tail(Thrower(), 1); - // An IPv6 address where Rust takes `SocketAddrV4`. - case 'family': - final any = api.SocketAddr(InternetAddress('::'), 1); - api.nexts(any, any); - // An IPv6 address where Rust takes `Ipv4Addr`. - case 'ip family': - final any = InternetAddress('::'); - api.nextIps(any, any); - // A Unix socket's address, which is no IP address. - case 'ip unix': - api.flip(InternetAddress('/tmp/api', type: InternetAddressType.unix)); - // No String holds a path that is not UTF-8. - case 'stray': - api.stray(); - case 'lent stray': - api.visitStray((_) {}); + api.tail(Thrower(), api.U64(1)); default: check(false, 'no case $which'); } diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 26a7735e5..c8c53f703 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -22,6 +22,7 @@ import ( "sync" "sync/atomic" "time" + "unicode" "unsafe" "api/api" @@ -79,6 +80,25 @@ func numbers(what string, n api.Numbers) { } } +// socketHex is a socket address as its IP's octets in hex, its port, and, IPv6's alone, its +// flow label and scope, apart by colons. +func socketHex(a rt.SocketAddr) string { + if v4, ok := a.V4(); ok { + return fmt.Sprintf("%x:%d", v4.IP, v4.Port) + } + v6, _ := a.V6() + return fmt.Sprintf("%x:%d:%d:%d", v6.IP, v6.Port, v6.FlowInfo, v6.ScopeID) +} + +// ipHex is an IP address's octets in hex, so the length shows its family. +func ipHex(ip rt.IpAddr) string { + if v4, ok := ip.V4(); ok { + return fmt.Sprintf("%x", v4) + } + v6, _ := ip.V6() + return fmt.Sprintf("%x", v6) +} + func quoted(s string) string { return fmt.Sprintf("%q", s) } @@ -182,7 +202,7 @@ func script() { d := api.PointDefault() fmt.Printf("default %d %d\n", d.X, d.Y) - c := api.NewCounter("c", 5) + c := must(api.NewCounter("c", 5)) fmt.Printf("get %d\n", must(c.Get())) fmt.Printf("name %s\n", must(c.Name())) fmt.Printf("add ok %d\n", must(c.Add(10))) @@ -204,12 +224,12 @@ func script() { fmt.Printf("debug %s\n", c.GoString()) label := must(c.IntoLabel(&p)) fmt.Printf("label %s\n", label.GoString()) - fmt.Printf("note %t %t\n", api.HasText(api.Note{Text: ptr("")}), api.HasText(api.NoteDefault())) + fmt.Printf("note %t %t\n", must(api.HasText(api.Note{Text: ptr("")})), must(api.HasText(api.NoteDefault()))) - below, above, err := api.NewCounter("s", 7).Split(5) + below, above, err := must(api.NewCounter("s", 7)).Split(5) ok(err) fmt.Printf("split %s %d %s %d\n", must(below.Name()), must(below.Get()), must(above.Name()), must(above.Get())) - one := must(api.Only(api.NewCounter("o", 3))) + one := must(api.Only(must(api.NewCounter("o", 3)))) fmt.Printf("only %s %d\n", must(one.Name()), must(one.Get())) w8, w16, w32, w64 := api.Widths(98305) fmt.Printf("widths %d %d %d %d\n", w8, w16, w32, w64) @@ -241,8 +261,8 @@ func script() { }), "[]")) // Each handle in a tuple moves in, as one taken by value does. board := must(api.NewBoard([]rt.Tuple2[string, *api.Counter]{ - {V0: "low", V1: api.NewCounter("l", 1)}, - {V0: "high", V1: api.NewCounter("h", 9)}, + {V0: "low", V1: must(api.NewCounter("l", 1))}, + {V0: "high", V1: must(api.NewCounter("h", 9))}, })) fmt.Printf("ranked %s\n", strings.Trim(list(must(board.Ranked()), func(r rt.Tuple2[string, *api.Counter]) string { return fmt.Sprintf("%s %s %d", r.V0, must(r.V1.Name()), must(r.V1.Get())) @@ -253,14 +273,14 @@ func script() { board.Close() // A map crosses as its pairs and a set as its items, which Rust collects in order, each // key once, the last kept. - atlas := api.Atlas{ + atlas := must(api.Atlas{ Points: []rt.Tuple2[string, api.Point]{ {V0: "b", V1: api.Point{X: 1, Y: 1}}, {V0: "ab", V1: api.Point{X: 2, Y: 3}}, {V0: "b", V1: api.Point{X: 4, Y: 4}}, }, Ids: []uint32{3, 1, 3}, - }.With("c", api.Point{X: 5, Y: 6}) + }.With("c", api.Point{X: 5, Y: 6})) atlasPoints := strings.Trim(list(atlas.Points, func(r rt.Tuple2[string, api.Point]) string { return fmt.Sprintf("%s %d %d", r.V0, r.V1.X, r.V1.Y) }), "[]") @@ -276,9 +296,11 @@ func script() { } square := api.ShapeSquare span := api.Span{Bounds: rt.Tuple2[uint32, uint32]{V0: 2, V1: 10}, Named: &rt.Tuple2[string, api.Shape]{V0: "s", V1: square}} - lower, upper := api.Halves(span) + lower, upper, err := api.Halves(span) + ok(err) fmt.Printf("halves %d %d %d %d %s\n", lower.V0, lower.V1, upper.V0, upper.V1, span.GoString()) - n, text := api.Swapped(rt.Tuple2[string, uint32]{V0: "t", V1: 4}) + n, text, err := api.Swapped(rt.Tuple2[string, uint32]{V0: "t", V1: 4}) + ok(err) fmt.Printf("swapped %d %s\n", n, text) // A tuple of one is its element, so `Option<(Option,)>` nests two pointers. for _, value := range []**uint32{nil, ptr[*uint32](nil), ptr(ptr[uint32](3))} { @@ -294,12 +316,12 @@ func script() { fmt.Printf("lent back %s boxed %s\n", list(api.LentBack(rt.NewBytes([]byte{1, 2})).Copy(), number), list(api.BoxedTuple(rt.NewBytes([]byte{3})).Copy(), number)) // A borrowed string and an owned one share a tuple struct. check(api.Title(false) == nil, "an informal title is none") - fmt.Printf("title Some((%s,)) None named Some((%s,))\n", quoted(*api.Title(true)), quoted(*api.Named("x"))) + fmt.Printf("title Some((%s,)) None named Some((%s,))\n", quoted(*api.Title(true)), quoted(*must(api.Named("x")))) // Each lent counter is a new handle to a clone. - registry := must(api.NewRegistry(api.NewCounter("first", 1))) + registry := must(api.NewRegistry(must(api.NewCounter("first", 1)))) fmt.Printf("last %t\n", must(registry.Last()) != nil) - ok(registry.Push(api.NewCounter("last", 2))) + ok(registry.Push(must(api.NewCounter("last", 2)))) first := must(registry.FirstGet()) must(first.Add(10)) fmt.Printf("first %d lent %d\n", must(must(registry.FirstGet()).Get()), must(first.Get())) @@ -314,38 +336,36 @@ func script() { })) fmt.Printf("edit %t %d\n", edited, must(must(registry.FirstGet()).Get())) // A setter moves an object in, as assignment does, closing its handle. - ok(registry.FirstSet(api.NewCounter("second", 3))) + ok(registry.FirstSet(must(api.NewCounter("second", 3)))) fmt.Printf("set first %d %s\n", must(must(registry.FirstGet()).Get()), must(must(registry.FirstGet()).Name())) fmt.Printf("walk %d\n", api.Walk(3, func(cursor *api.Cursor) uint64 { return must(cursor.At()) * 2 })) fmt.Printf("each %d\n", api.Each(2, func(at uint32) { fmt.Printf("visit %d\n", at) })) fmt.Printf("offset %d\n", api.Offset(func() uint32 { return 7 }, 3)) fmt.Printf("shift %d\n", api.Shift(func() uint32 { return 7 }, 4)) - name := api.NameOwned("a") - joined := must(name.Join(api.NameOwned("b"))) + name := must(api.NameOwned("a")) + joined := must(name.Join(must(api.NameOwned("b")))) fmt.Printf("joined %s %s %t\n", must(joined.AsStr()), joined.GoString(), must(joined.Equal(name))) ok(joined.Rename("c")) fmt.Printf("renamed %s\n", must(joined.AsStr())) - long := must(api.Longer(joined, api.NameOwned("dd"))) + long := must(api.Longer(joined, must(api.NameOwned("dd")))) fmt.Printf("longer %s\n", must(long.AsStr())) fmt.Printf("unnamed [%s]\n", must(api.Unnamed().AsStr())) - lit := api.NameBorrowed("lit") - fmt.Printf("borrowed %s %t %t\n", must(lit.AsStr()), must(lit.Shares(api.NameBorrowed("lit"))), must(lit.Shares(api.NameBorrowed("other")))) + lit := must(api.NameBorrowed("lit")) + fmt.Printf("borrowed %s %t %t\n", must(lit.AsStr()), must(lit.Shares(must(api.NameBorrowed("lit")))), must(lit.Shares(must(api.NameBorrowed("other"))))) fmt.Printf("total %d\n", api.Total(rt.RangeU64{Start: 2, End: 5})) fmt.Printf("reach %d\n", api.Reach(2, 5)) - bigOne := big.NewInt(1) - pow := func(n uint) *big.Int { return new(big.Int).Lsh(bigOne, n) } fmt.Print("scaled") - for _, v := range []*big.Int{ - api.Scaled(new(big.Int).Sub(pow(128), bigOne), 1), - api.Scaled(new(big.Int).Add(pow(64), bigOne), 1<<63), - api.Scaled(pow(127), 2), + for _, v := range []*rt.U128{ + api.Scaled(rt.U128{Hi: math.MaxUint64, Lo: math.MaxUint64}, 1), + api.Scaled(rt.U128{Hi: 1, Lo: 1}, 1<<63), + api.Scaled(rt.U128{Hi: 1 << 63}, 2), } { if v == nil { fmt.Print(" none") } else { - fmt.Printf(" %032x", v) + fmt.Printf(" %016x%016x", v.Hi, v.Lo) } } fmt.Println() @@ -405,19 +425,19 @@ func script() { window := api.Window{Range: rt.RangeU64{Start: 2, End: 8}, End: api.End(5)} fmt.Printf("window %t %t %s\n", window.Contains(4), window.Contains(6), window.GoString()) - fmt.Println(api.Greet("x", api.ShapeSquare)) + fmt.Println(must(api.Greet("x", api.ShapeSquare))) fmt.Printf("len %d\n", api.Len(rt.NewBytes([]byte("abc")))) binary := []byte{0xff, 0, 0x61} fmt.Printf("hex %s\n", api.Hex(rt.NewBytes(binary))) fmt.Printf("timed hex %s\n", api.TimedHex(rt.NewBytes(binary))) fmt.Printf("sample hex %s\n", api.SampleHex(rt.NewBytes(binary))) fmt.Printf("queued hex %s\n", api.QueuedHex(rt.NewBytes(binary))) - fmt.Printf("tick %s\n", api.TickName(api.Label{Name: "tick"})) - fmt.Printf("rooted %s\n", api.RootedName(api.Label{Name: "rooted"})) - fmt.Printf("stored %s\n", api.StoredText("stored")) - fmt.Printf("layer %s\n", api.LayerName(api.Label{Name: "layer"})) + fmt.Printf("tick %s\n", must(api.TickName(api.Label{Name: "tick"}))) + fmt.Printf("rooted %s\n", must(api.RootedName(api.Label{Name: "rooted"}))) + fmt.Printf("stored %s\n", must(api.StoredText("stored"))) + fmt.Printf("layer %s\n", must(api.LayerName(api.Label{Name: "layer"}))) for _, event := range []api.Event{api.EventMoved{V0: p}, api.EventNamed{Name: "n"}, api.EventDone{}} { - fmt.Println(api.Describe(event)) + fmt.Println(must(api.Describe(event))) } // A `Copy` value passed by value is copied, so it stays usable. @@ -496,7 +516,7 @@ func script() { mark := api.MarkOf(1) ok(mark.Add(2)) parcel := api.Parcel{Mark: mark, Payload: rt.NewBytes([]byte("abc"))} - hefted := api.NewCounter("w", 1) + hefted := must(api.NewCounter("w", 1)) fmt.Printf("heft %d\n", must(api.Heft(rt.Tuple2[*api.Mark, rt.Bytes]{V0: mark, V1: rt.NewBytes([]byte("ab"))}, parcel, rt.Tuple2[*api.Counter, rt.Bytes]{V0: hefted, V1: rt.NewBytes([]byte("abcd"))}))) hefted.Close() // A value's `&mut self` beside a checked handle. @@ -614,16 +634,16 @@ func script() { // At its default, named as the type itself. stack := api.NewStack() for i, n := range []string{"a", "b"} { - ok(stack.Push(api.NewCounter(n, uint64(i)+1))) + ok(stack.Push(must(api.NewCounter(n, uint64(i)+1)))) } top := must(must(stack.Pop()).Get()) fmt.Printf("stack %d Some(%d) %t %t\n", must(stack.Depth()), top, must(stack.Pop()) != nil, must(stack.Pop()) == nil) // Generic functions and methods, at each implementation of the trait bounding them, by // reference, which leaves Go its handle. - fromPoint := api.CounterSeededPoint("p", api.Point{X: -3, Y: 4}) - fromSize := api.CounterSeededSize("s", dep.Size{Width: 5, Height: 6}) + fromPoint := must(api.CounterSeededPoint("p", api.Point{X: -3, Y: 4})) + fromSize := must(api.CounterSeededSize("s", dep.Size{Width: 5, Height: 6})) fromCounter := must(api.CounterSeededCounter("c", fromPoint)) - fromPolar := api.CounterSeededPolarPoint("q", api.PolarPoint{Turns: 1, Distance: 2}) + fromPolar := must(api.CounterSeededPolarPoint("q", api.PolarPoint{Turns: 1, Distance: 2})) fmt.Printf("seeded %d %d %d %d past %d seeds %d\n", must(fromPoint.Get()), must(fromSize.Get()), must(fromCounter.Get()), must(fromPolar.Get()), must(stack.DepthPastPoint(api.Point{X: 1, Y: 2})), api.SeedsPointSize(dep.Size{Width: 2, Height: 3})) @@ -692,14 +712,14 @@ func script() { _, err = url.UrlParse("no scheme") check(errors.As(err, &parse), "parse returns its message") fmt.Printf("parse err %s\n", parse.Value) - v4 := netip.MustParseAddrPort("127.0.0.1:8080") - v6 := netip.MustParseAddrPort("[::1%3]:443") - fmt.Printf("ports %s\n", list(api.Ports([]netip.AddrPort{v4, v6}), number)) - peer := api.Peer{Addr: v6, Names: []string{"a", "b"}}.Next("c") - fmt.Printf("peer %s %s\n", peer.GoString(), list(peer.Aliases(), quoted)) - file := api.File("dir", "f\\'\"\t") + v4 := must(rt.SocketAddrV4Of(netip.MustParseAddrPort("127.0.0.1:8080"))) + v6 := must(rt.SocketAddrV6Of(netip.MustParseAddrPort("[::1%3]:443"))) + fmt.Printf("ports %s\n", list(api.Ports([]rt.SocketAddr{rt.SocketAddrFromV4(v4), rt.SocketAddrFromV6(v6)}), number)) + peer := must(api.Peer{Addr: rt.SocketAddrFromV6(v6), Names: []string{"a", "b"}}.Next("c")) + fmt.Printf("peer %s %s\n", peer.GoString(), list(must(peer.Aliases()), quoted)) + file := must(api.File("dir", "f\\'\"\t")) fmt.Printf("file %s\n", escaped(file)) - fmt.Printf("parents %s\n", strings.Trim(strings.ReplaceAll(list(api.Parents([]string{file, "/"}), func(p *string) string { + fmt.Printf("parents %s\n", strings.Trim(strings.ReplaceAll(list(must(api.Parents([]string{file, "/"})), func(p *string) string { if p == nil { return "none" } @@ -714,16 +734,16 @@ func script() { _, err = api.Refuse("why") var refusal *api.Refusal check(errors.As(err, &refusal) && err.Error() == "refused: why", "a boxed error is its type") - words := api.Words(" one two three ") + words := must(api.Words(" one two three ")) fmt.Printf("words %d %s\n", len(words), list(words, quoted)) - fmt.Printf("sentence %s %s\n", api.Sentence(&words), api.Sentence(nil)) - fmt.Printf("sentence %s|\n", api.Sentence(&[]string{})) + fmt.Printf("sentence %s %s\n", must(api.Sentence(&words)), must(api.Sentence(nil))) + fmt.Printf("sentence %s|\n", must(api.Sentence(&[]string{}))) abc := "abc" - fmt.Printf("either %d %d\n", api.Either(&abc), api.Either(nil)) + fmt.Printf("either %d %d\n", must(api.Either(&abc)), must(api.Either(nil))) ab := rt.NewBytes([]byte("ab")) fmt.Printf("prefixed %s %s\n", api.Prefixed(rt.NewBytes([]byte("cd")), &ab).Copy(), api.Prefixed(rt.NewBytes([]byte("cd")), nil).Copy()) fmt.Printf("coordinates %d %d\n", api.Coordinates(&api.Point{X: 2, Y: 3}), api.Coordinates(nil)) - two, three := api.NewCounter("two", 2), api.NewCounter("three", 3) + two, three := must(api.NewCounter("two", 2)), must(api.NewCounter("three", 3)) fmt.Printf("plus %d %d\n", must(two.Plus(three)), must(two.Plus(nil))) fmt.Printf("add_to %t %t %d\n", must(two.AddTo(three)), must(two.AddTo(nil)), must(three.Get())) encoded := rt.NewBytes([]byte("0,")) @@ -741,13 +761,13 @@ func script() { nudged := api.Point{X: 1, Y: 2} api.Nudge(&nudged, 3) fmt.Printf("nudge %d %d\n", nudged.X, nudged.Y) - config := api.Secure("h3") + config := must(api.Secure("h3")) fmt.Printf("secure %s %s\n", must(api.AlpnLent(config)), must(api.Alpn(must(config.Clone())))) cert := api.Certify(rt.NewBytes([]byte("der"))) certLen := must(api.CertLen(cert)) fmt.Printf("cert %d %s %d\n", certLen, must(api.CertDer(cert)).Copy(), must(api.ChainLen([]*tls.Cert{api.Certify(rt.NewBytes([]byte("a"))), api.Certify(rt.NewBytes([]byte("bc")))}))) // Another crate's trait objects: an `Arc`, taken back, and `'static` ones, held by value. - fmt.Printf("resolver %s\n", must(api.Resolved(api.Resolver("r1")))) + fmt.Printf("resolver %s\n", must(api.Resolved(must(api.Resolver("r1"))))) codecs := api.NewCodecs() fmt.Printf("codecs %d\n", must(codecs.Ids())) listed := must(codecs.ListGet()) @@ -755,13 +775,13 @@ func script() { fmt.Printf("codecs reversed %d\n", must(codecs.Ids())) codecs.Close() // A standard library value no type maps, in a handle. - failure := api.Failure("boom") + failure := must(api.Failure("boom")) fmt.Printf("failure %s %s\n", must(api.FailureMessage(failure)), must(api.FailureKind(failure))) // Iterators, passed as the arrays of their items. fmt.Printf("sum_all %d\n", api.SumAll([]uint32{1, 2, 3})) - fmt.Printf("weigh_all %d\n", must(api.WeighAllCounter([]*api.Counter{api.NewCounter("w", 3)}))) + fmt.Printf("weigh_all %d\n", must(api.WeighAllCounter([]*api.Counter{must(api.NewCounter("w", 3))}))) fmt.Printf("offered_len %d\n", api.OfferedLen([]rt.Bytes{rt.NewBytes([]byte("h3")), rt.NewBytes([]byte("moq-00"))})) - fmt.Printf("join_words %s\n", api.JoinWords([]string{"a", "b", "c"})) + fmt.Printf("join_words %s\n", must(api.JoinWords([]string{"a", "b", "c"}))) // Bytes that must be `'static`, interned. for _, label := range []string{"der", "der again"} { der := api.DerFromSlice(rt.NewBytes([]byte("der"))) @@ -793,13 +813,13 @@ func script() { _ = must(must(tallies.First()).Bump()) fmt.Printf("tallied %d\n", must(must(tallies.First()).Bump())) fmt.Printf("chunks %s\n", list(api.Chunks(rt.NewBytes([]byte("abcde")), 2), func(b rt.Bytes) string { return list(b.Copy(), number) })) - counters := api.Counters([]string{"x", "y"}) + counters := must(api.Counters([]string{"x", "y"})) fmt.Printf("counters %s\n", strings.Trim(list(counters, func(c *api.Counter) string { return fmt.Sprintf("%s %d", must(c.Name()), must(c.Get())) }), "[]")) // Each counter moves in, so Go closes it. fmt.Printf("worth %d %d\n", must(api.Worth(&[]*api.Counter{counters[0]}, []*api.Counter{counters[1]})), must(api.Worth(nil, nil))) - held := api.NewCounter("h", 4) + held := must(api.NewCounter("h", 4)) fmt.Print("held") for _, n := range []*uint64{must(api.HeldCount(&held)), must(api.HeldCount(nil))} { if n == nil { @@ -814,7 +834,7 @@ func script() { if s := must(api.BoxedName(nil)); s != nil { unnamed = *s } - fmt.Printf("boxed Some(%q) %s %t\n", *must(api.BoxedName(api.BoxedCounter(&b))), unnamed, api.BoxedCounter(nil) == nil) + fmt.Printf("boxed Some(%q) %s %t\n", *must(api.BoxedName(must(api.BoxedCounter(&b)))), unnamed, must(api.BoxedCounter(nil)) == nil) fmt.Printf("less %d\n", api.Less(5, 2)) fmt.Printf("sums %s\n", list(api.Sums([][]uint32{{1, 2}, {}, {3}}), number)) var leastSets []string @@ -844,15 +864,15 @@ func script() { sum := api.Summed([]api.Point{{X: 1, Y: 2}, {X: -4, Y: 6}}) none := api.Summed(nil) fmt.Printf("summed %d %d %d %d\n", sum.X, sum.Y, none.X, none.Y) - fmt.Printf("longest %d %d\n", api.Longest([]string{"a", "abc"}), api.Longest(nil)) + fmt.Printf("longest %d %d\n", must(api.Longest([]string{"a", "abc"})), must(api.Longest(nil))) noFirst := "None" - if api.FirstOf(nil) != nil { + if must(api.FirstOf(nil)) != nil { noFirst = "Some" } - fmt.Printf("first of Some(%s) %s\n", quoted(*api.FirstOf([]string{"a", "b"})), noFirst) + fmt.Printf("first of Some(%s) %s\n", quoted(*must(api.FirstOf([]string{"a", "b"}))), noFirst) limits := api.ByteLimits(rt.NewBytes([]byte("a")), rt.NewBytes([]byte("bc"))) - breadth := api.Breadth(api.LimitsDuration{Low: 5 * time.Millisecond, High: 2 * time.Second}) - fmt.Printf("limits %s %s breadth %d\n", limits.Low.Copy(), limits.High.Copy(), breadth.Milliseconds()) + breadth := api.Breadth(api.LimitsDuration{Low: must(rt.DurationOf(5 * time.Millisecond)), High: must(rt.NewDuration(2, 0))}) + fmt.Printf("limits %s %s breadth %d\n", limits.Low.Copy(), limits.High.Copy(), must(breadth.Std()).Milliseconds()) parts := []rt.Bytes{rt.NewBytes([]byte("ab")), rt.NewBytes(nil), rt.NewBytes([]byte("c"))} fmt.Printf("joined %s %d read %d\n", api.Joined(parts).Copy(), len(api.Joined(nil).Copy()), api.ReadLen(rt.NewBytes([]byte("hello")))) doc := must(serdejson.JsonParse(`{"b": [1, 2.5, null], "a": "é"}`)) @@ -881,7 +901,7 @@ func script() { // Slices, passed in and returned as clones, and the socket address of each family. fmt.Printf("shapes %s greetings %s\n", list(api.ShapeALL(), func(s api.Shape) string { return shapes[s] }), list(api.GREETINGS(), quoted)) fmt.Printf("both %s primes %s\n", list(api.ShapeBOTH(), func(s api.Shape) string { return shapes[s] }), list(api.PRIMES(), func(n uint32) string { return fmt.Sprint(n) })) - trail := api.NewTrail([]api.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}, []string{"a", "b"}) + trail := must(api.NewTrail([]api.Point{{X: 1, Y: 2}, {X: 3, Y: 4}}, []string{"a", "b"})) point := func(p api.Point) string { return p.GoString() } labeled := func(l api.Label) string { return l.GoString() } fmt.Printf("trail %s %s\n", list(must(trail.Points()), point), list(must(trail.Labels()), labeled)) @@ -893,14 +913,57 @@ func script() { return fmt.Sprintf("%d %d", p.X, p.Y) }), "[]")) trail.Close() - fmt.Printf("numbered %s\n", strings.Trim(list(api.Numbered([]string{"p", "q"}), func(c *api.Counter) string { + fmt.Printf("numbered %s\n", strings.Trim(list(must(api.Numbered([]string{"p", "q"})), func(c *api.Counter) string { return fmt.Sprintf("%s %d", must(c.Name()), must(c.Get())) }), "[]")) next4, next6 := api.Nexts(v4, v6) - fmt.Printf("nexts %s %d %d %s\n", next4.Addr(), next4.Port(), next6.Port(), next6.Addr().Zone()) - nextIp4, nextIp6 := api.NextIps(netip.MustParseAddr("10.0.0.255"), netip.MustParseAddr("::ffff")) - mapped := api.Flip(netip.MustParseAddr("127.0.0.1")) - fmt.Printf("ips %x %x %x %x %x\n", nextIp4.AsSlice(), nextIp6.AsSlice(), mapped.AsSlice(), api.Flip(mapped).AsSlice(), api.Flip(netip.IPv6Loopback()).AsSlice()) + fmt.Printf("nexts %s %d %d %d\n", next4.IP.Netip(), next4.Port, next6.Port, next6.ScopeID) + nextIp4, nextIp6 := api.NextIps(rt.Ipv4Addr{10, 0, 0, 255}, must(rt.Ipv6AddrOf(netip.MustParseAddr("::ffff")))) + mapped := api.Flip(rt.IpAddrFromV4(rt.Ipv4Addr{127, 0, 0, 1})) + fmt.Printf("ips %x %x %s %s %s\n", nextIp4, nextIp6, ipHex(mapped), ipHex(api.Flip(mapped)), ipHex(api.Flip(must(rt.IpAddrOf(netip.IPv6Loopback()))))) + + // Lossless round trips: each type's edge values, passed through Rust and back. + fmt.Print("lossless duration") + for _, d := range []rt.Duration{ + {}, + must(rt.NewDuration(0, 1)), + must(rt.NewDuration(9_223_372_036, 854_775_808)), + must(rt.NewDuration(math.MaxUint64, 999_999_999)), + } { + d = api.SameDuration(d) + fmt.Printf(" %d.%09d", d.Secs(), d.Nanos()) + } + fmt.Println() + fmt.Printf("lossless u64 %d %d %d\n", api.SameU64(0), api.SameU64(1<<63), api.SameU64(math.MaxUint64)) + fmt.Printf("lossless usize %d %d\n", api.SameUsize(0), api.SameUsize(math.MaxUint)) + fmt.Printf("lossless isize %d %d\n", api.SameIsize(math.MinInt), api.SameIsize(math.MaxInt)) + fmt.Print("lossless u128") + for _, v := range []rt.U128{{}, {Hi: 1}, {Hi: math.MaxUint64, Lo: math.MaxUint64}} { + v = api.SameU128(v) + fmt.Printf(" %016x%016x", v.Hi, v.Lo) + } + fmt.Println() + fmt.Print("lossless char") + for _, c := range []rune{0, 0xd7ff, 0xe000, unicode.MaxRune} { + fmt.Printf(" %x", must(api.SameChar(c))) + } + fmt.Println() + fmt.Printf("lossless path %x %x\n", must(api.SamePath("a/é")), must(api.SamePath(must(api.Stray())))) + sameV4 := api.SameSocketAddrV4(rt.SocketAddrV4{IP: rt.Ipv4Addr{255, 255, 255, 255}, Port: math.MaxUint16}) + sameV6 := api.SameSocketAddrV6(rt.SocketAddrV6{ + IP: rt.Ipv6Addr{0, 1, 15: 2}, + Port: 3, + FlowInfo: math.MaxUint32, + ScopeID: math.MaxUint32, + }) + fmt.Printf("lossless socket %s %s %s %s\n", socketHex(rt.SocketAddrFromV4(sameV4)), socketHex(rt.SocketAddrFromV6(sameV6)), + socketHex(api.SameSocketAddr(rt.SocketAddrFromV4(sameV4))), socketHex(api.SameSocketAddr(rt.SocketAddrFromV6(sameV6)))) + // A key hashes by Rust's Hash: equal keys alike, and these two apart. + keyA, keyB := must(api.NewKey("a")), must(api.NewKey("b")) + fmt.Printf("key %t %t\n", must(keyA.HashOf()) == must(must(api.NewKey("a")).HashOf()), must(keyA.HashOf()) == must(keyB.HashOf())) + fmt.Printf("lossless ip %s %s\n", ipHex(api.SameIpAddr(rt.IpAddrFromV4(rt.Ipv4Addr{255, 255, 255, 255}))), + ipHex(api.SameIpAddr(rt.IpAddrFromV6(rt.Ipv6Addr{0xff, 0xff, 15: 1})))) + checked() // serde_json's map iterator, collected as pairs of a key and its document. sections := must(api.Sections([]rt.Tuple2[string, *serdejson.Json]{ @@ -917,9 +980,9 @@ func script() { // An instant only Rust makes, a handle, as its padding is wherever rustc puts it, which // each call copies out of, 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(), + ended := must(api.Later(began, must(rt.NewDuration(1, 500_000_000)))) + same := must(api.Later(began, rt.Duration{})) + fmt.Printf("instants %d %d %t %t\n", must(must(api.Since(ended, began)).Std()).Milliseconds(), must(must(api.Since(began, ended)).Std()).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() @@ -987,16 +1050,16 @@ func script() { } else { fmt.Printf("sought %d\n", at) } - fmt.Printf("non zeros %s\n", api.DescribeNonZeros(api.NewNonZeros())) + fmt.Printf("non zeros %s\n", must(api.DescribeNonZeros(api.NewNonZeros()))) hopFirst, hopLast := uint64(1), uint64(math.MaxUint64) for _, hop := range []*uint64{nil, &hopFirst, &hopLast} { - if next := api.NextHop(hop); next != nil { + if next := must(api.NextHop(hop)); next != nil { fmt.Printf("hop Some(%d)\n", *next) } else { fmt.Println("hop None") } } - fmt.Printf("farther hop %d\n", api.FartherHop(api.PairNonZeroU16{Left: 3, Right: math.MaxUint16})) + fmt.Printf("farther hop %d\n", must(api.FartherHop(api.PairNonZeroU16{Left: 3, Right: math.MaxUint16}))) } // farReader is a reader whose reads fail, which seeks anywhere from its start, as Rust's @@ -1123,6 +1186,16 @@ func closures() { released(freed, "a dropped future releases its closure") } + // A callback a failed check stops before the call never reaches Rust, so nothing keeps it. + { + marker, freed := marked() + gone := must(api.NewCounter("gone", 1)) + check(gone.Close() == nil, "Close passes") + _, err := api.LogCounts(markedLog{marker}, 0, must(api.NewCounter("c", 1)), []*api.Counter{gone}) + check(errors.Is(err, rt.ErrClosed), "a closed object a list holds fails the call") + released(freed, "a call a check failed releases its callback") + } + // What a closure returns crosses: the instant it asks for, then its error. { start := *api.NewMoment(0, 1000) @@ -1457,7 +1530,7 @@ func closed() { check(errors.Is(err, rt.ErrClosed) && err.Error() == want, fmt.Sprintf("%s: %v", what, err)) } // A method, a trait's, and a field's accessors. - c := api.NewCounter("c", 1) + c := must(api.NewCounter("c", 1)) check(c.Close() == nil && c.Close() == nil, "Close, then again, passes") _, err := c.Get() isClosed(err, "Counter is closed", "a method of a closed object") @@ -1468,12 +1541,12 @@ func closed() { tally.Close() _, err = api.NewTally().Equal(tally) isClosed(err, "Tally is closed", "Equal with a closed object") - registry := must(api.NewRegistry(api.NewCounter("first", 1))) + registry := must(api.NewRegistry(must(api.NewCounter("first", 1)))) registry.Close() _, err = registry.FirstGet() isClosed(err, "Registry is closed", "a field's getter of a closed object") // A call that fails moves nothing in. - second := api.NewCounter("second", 2) + second := must(api.NewCounter("second", 2)) isClosed(registry.FirstSet(second), "Registry is closed", "a field's setter of a closed object") check(must(second.Get()) == 2, "a failed call leaves what it would move") @@ -1486,7 +1559,7 @@ func closed() { // An argument, alone, or nested where the call holds it through its arena. _, err = api.BumpBoth(api.NewTally(), tally) isClosed(err, "Tally is closed", "a closed argument") - open := api.NewCounter("open", 3) + open := must(api.NewCounter("open", 3)) _, err = api.Worth(&[]*api.Counter{open, c}, nil) isClosed(err, "Counter is closed", "a closed object in a list") check(must(open.Get()) == 3, "a failed call leaves the rest of a list") @@ -1498,7 +1571,7 @@ func closed() { isClosed(err, "Channel is closed", "an async call of a closed object") // Moved by a call, plain or boxed, or closed once the callback it was lent to returned. - taken := api.NewCounter("m", 0) + taken := must(api.NewCounter("m", 0)) must(taken.IntoLabel(nil)) _, err = taken.Get() isClosed(err, "Counter is closed", "a method of a moved object") @@ -1507,7 +1580,7 @@ func closed() { _, err = tail.Sum() isClosed(err, "Node is closed", "a method of a boxed moved object") var kept *api.Counter - must(must(api.NewRegistry(api.NewCounter("c", 0))).Edit(func(counter *api.Counter) bool { + must(must(api.NewRegistry(must(api.NewCounter("c", 0)))).Edit(func(counter *api.Counter) bool { kept = counter return false })) @@ -1638,6 +1711,89 @@ func bytes() { fmt.Println("bytes ok") } +// invalid checks that err is a checked conversion's or a refused value's error, matching +// rt.ErrInvalid. +func invalid(err error, what string) { + check(errors.Is(err, rt.ErrInvalid), what) +} + +// checked checks that each value Go's type holds and Rust's does not fails the call taking +// it, before it is made, and that each conversion to or from the runtime's types fails on a +// value the other has no room for: each with an error matching rt.ErrInvalid, never a panic. +func checked() { + // Text that is not UTF-8, alone, optional, and inside a record, a list, and a receiver. + _, err := api.SameString("\xff") + invalid(err, "a string that is not UTF-8 fails the call") + _, err = api.Greet("\xff", api.ShapeSquare) + invalid(err, "a borrowed string that is not UTF-8 fails the call") + _, err = api.BoxedCounter(ptr("\xff")) + invalid(err, "an optional string that is not UTF-8 fails the call") + _, err = api.HasText(api.Note{Text: ptr("a\xffb")}) + invalid(err, "a record's string that is not UTF-8 fails the call") + _, err = api.Longest([]string{"a", "\xc3"}) + invalid(err, "a list's string that is not UTF-8 fails the call") + _, err = api.Peer{Names: []string{"\xff"}}.Aliases() + invalid(err, "a receiver's string that is not UTF-8 fails the call") + check(must(api.SameString("é")) == "é" && must(api.SameString("")) == "", "UTF-8 text crosses whole") + check(must(api.SamePath("\xff")) == "\xff", "a path is any bytes on Unix") + // A rune that is a surrogate, past Unicode, or negative. + for _, r := range []rune{0xd800, 0xdfff, unicode.MaxRune + 1, -1} { + _, err = api.SameChar(r) + invalid(err, fmt.Sprintf("the rune %d fails the call", r)) + } + // A zero for a NonZero, optional and inside a record. + noHop := uint64(0) + invalid(second(api.NextHop(&noHop)), "a zero NonZero fails the call") + invalid(second(api.FartherHop(api.PairNonZeroU16{Left: 0, Right: 1})), "a record's zero NonZero fails the call") + + // Durations: built exactly, and converted to and from time.Duration where it holds them. + invalid(second(rt.NewDuration(math.MaxUint64, 1e9)), "a duration past Rust's longest fails") + carried := must(rt.NewDuration(1, 2_500_000_000)) + check(carried.Secs() == 3 && carried.Nanos() == 500_000_000, "nanoseconds past a second carry") + invalid(second(rt.DurationOf(-time.Nanosecond)), "a negative duration fails") + check(must(must(rt.DurationOf(math.MaxInt64)).Std()) == math.MaxInt64, "time.Duration's longest crosses") + longest := api.SameDuration(must(rt.NewDuration(math.MaxUint64, 999_999_999))) + invalid(second(longest.Std()), "a duration past time.Duration's longest fails") + extended := api.Extended(must(rt.NewDuration(math.MaxInt64, 0)), must(rt.NewDuration(1, 0))) + check(extended != nil && extended.Secs() == 1<<63, "a duration past time.Duration's longest crosses") + check(api.SameDuration(rt.Duration{}) == rt.Duration{}, "the zero duration is zero") + + // u128s: converted to and from big.Int within u128's range. + invalid(second(rt.U128Of(big.NewInt(-1))), "a negative int is no u128") + past := new(big.Int).Lsh(big.NewInt(1), 128) + invalid(second(rt.U128Of(past)), "an int past u128 fails") + most := new(big.Int).Sub(past, big.NewInt(1)) + check(must(rt.U128Of(most)) == rt.U128{Hi: math.MaxUint64, Lo: math.MaxUint64}, "u128's most converts") + check(api.SameU128(must(rt.U128Of(most))).Big().Cmp(most) == 0, "a u128 converts back") + + // Addresses: each family converted from netip only where it fits, and back. + invalid(second(rt.SocketAddrV4Of(netip.MustParseAddrPort("[::]:1"))), "an IPv6 address is no SocketAddrV4") + invalid(second(rt.SocketAddrV6Of(netip.MustParseAddrPort("1.2.3.4:1"))), "an IPv4 address is no SocketAddrV6") + invalid(second(rt.SocketAddrV6Of(netip.MustParseAddrPort("[fe80::1%no-such-interface]:1"))), "a zone naming no interface fails") + invalid(second(rt.SocketAddrOf(netip.AddrPort{})), "an invalid address is no SocketAddr") + invalid(second(rt.Ipv4AddrOf(netip.IPv6Unspecified())), "an IPv6 address is no Ipv4Addr") + invalid(second(rt.Ipv6AddrOf(netip.MustParseAddr("1.2.3.4"))), "an IPv4 address is no Ipv6Addr") + invalid(second(rt.Ipv6AddrOf(netip.MustParseAddr("fe80::1%3"))), "an Ipv6Addr has no zone") + invalid(second(rt.IpAddrOf(netip.MustParseAddr("fe80::1%3"))), "an IpAddr has no zone") + invalid(second(rt.IpAddrOf(netip.Addr{})), "an invalid address is no IpAddr") + flowing := api.SameSocketAddrV6(rt.SocketAddrV6{FlowInfo: 1}) + invalid(second(flowing.Netip()), "a flow label has no netip form") + invalid(second(rt.SocketAddrFromV6(flowing).Netip()), "a socket address's flow label has no netip form") + scoped := api.SameSocketAddr(must(rt.SocketAddrOf(netip.MustParseAddrPort("[fe80::1%3]:443")))) + check(must(scoped.Netip()) == netip.MustParseAddrPort("[fe80::1%3]:443"), "a scope crosses as its zone") + mapped := netip.MustParseAddr("::ffff:1.2.3.4") + check(api.SameIpAddr(must(rt.IpAddrOf(mapped))).Netip() == mapped, "an IPv4-mapped IPv6 address stays IPv6") + zero, isV4 := api.SameSocketAddr(rt.SocketAddr{}).V4() + check(isV4 && zero == rt.SocketAddrV4{}, "the zero socket address is IPv4's unspecified, port 0") + zeroIp, isV4 := api.SameIpAddr(rt.IpAddr{}).V4() + check(isV4 && zeroIp == rt.Ipv4Addr{}, "the zero IP address is IPv4's unspecified") +} + +// second is a pair's second value, the error a conversion returns. +func second[T any](_ T, err error) error { + return err +} + // budgeted checks that at most 64 MiB of Rust's bytes are lent at once, freed or not, since // the GC sees none of them: past that, bytes are copied, Rust's freed at the call, until the // GC frees what was lent. First, so no bytes an earlier step lent count against it. @@ -1705,30 +1861,13 @@ func broken(which string) { check(errors.Is(err, rt.ErrClosed), "a handle that outlived its lender is closed") panic(err) case "lent moved": - must(api.NewRegistry(api.NewCounter("c", 0))).Edit(func(counter *api.Counter) bool { + must(api.NewRegistry(must(api.NewCounter("c", 0)))).Edit(func(counter *api.Counter) bool { _, err := counter.IntoLabel(nil) check(errors.Is(err, rt.ErrBorrowed), "a lent handle moved out is borrowed") panic(err) }) - // An IPv6 address where Rust takes `SocketAddrV4`. - case "family": - any := netip.MustParseAddrPort("[::]:1") - api.Nexts(any, any) - // An IPv6 address where Rust takes `Ipv4Addr`. - case "ip family": - any := netip.IPv6Unspecified() - api.NextIps(any, any) - // An IPv6 address with a zone, which Rust's `IpAddr` has no room for. - case "ip scope": - api.Flip(netip.MustParseAddr("fe80::1%3")) - // No Rust string holds text that is not UTF-8. - case "text": - api.Greet("\xff", api.ShapeSquare) case "callback panicked": api.Emit(panicking{}, rt.NewBytes([]byte{1})) - // A Rust Duration longer than Go's longest. - case "duration": - api.Extended(math.MaxInt64, 1) // The packages serve wasm32 too, whose items the host's library lacks. case "target": _, err := api.WasmOnly() @@ -1762,6 +1901,13 @@ func (l *cancelLog) Log(uint8, string) bool { return true } +// markedLog holds a marker, so its collection shows nothing keeps it. +type markedLog struct{ marker *[4]int } + +func (l markedLog) Log(uint8, string) bool { + return l.marker[0] == 0 +} + type panicking struct{} func (panicking) Log(uint8, string) bool { diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 8e82075dd..79fe7a52a 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -507,15 +507,15 @@ fun script() { } catch (e: StringException) { println("parse err ${e.error}") } - val v4 = java.net.InetSocketAddress(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1)), 8080) + val v4 = SocketAddrV4(Ipv4Addr(127u, 0u, 0u, 1u), 8080u) val loopback = ByteArray(16).also { it[15] = 1 } - val v6 = java.net.InetSocketAddress(java.net.Inet6Address.getByAddress(null, loopback, 3), 443) + val v6 = SocketAddrV6(Ipv6Addr(loopback), 443u, 0u, 3u) println("ports ${ports(listOf(v4, v6)).contentToString()}") val peer = Peer(v6, listOf("a", "b")).next("c") println("peer ${peer.debug()} ${peer.aliases().joinToString(", ", "[", "]") { "\"$it\"" }}") - val file = file("dir", "f\\'\"\t") + val file = file(PathBuf("dir"), "f\\'\"\t") println("file ${escaped(file)}") - println("parents ${parents(listOf(file, "/")).joinToString(" ") { it?.let(::escaped) ?: "none" }}") + println("parents ${parents(listOf(file, PathBuf("/"))).joinToString(" ") { it?.let(::escaped) ?: "none" }}") val words = words(" one two three ") println("words ${words.size} ${words.joinToString(", ", "[", "]") { "\"$it\"" }}") println("sentence ${sentence(words)} ${sentence(null)}") @@ -669,8 +669,8 @@ fun script() { println("longest ${longest(listOf("a", "abc"))} ${longest(emptyList())}") println("first of Some(\"${firstOf(listOf("a", "b"))}\") ${firstOf(emptyList()) ?: "None"}") val limits = byteLimits(Bytes("a".encodeToByteArray()), Bytes("bc".encodeToByteArray())) - val waits = breadth(LimitsDuration(5.milliseconds, 2.seconds)) - println("limits ${limits.low.toByteArray().decodeToString()} ${limits.high.toByteArray().decodeToString()} breadth ${waits.inWholeMilliseconds}") + val waits = breadth(LimitsDuration(Duration(5.milliseconds), Duration(2.seconds))) + println("limits ${limits.low.toByteArray().decodeToString()} ${limits.high.toByteArray().decodeToString()} breadth ${waits.toKotlinDuration().inWholeMilliseconds}") val parts = joined(listOf(Bytes("ab".encodeToByteArray()), Bytes(ByteArray(0)), Bytes("c".encodeToByteArray()))).toByteArray().decodeToString() println("joined $parts ${joined(emptyList()).toByteArray().size} read ${readLen(Bytes("hello".encodeToByteArray()))}") val doc = Json.parse("""{"b": [1, 2.5, null], "a": "\u00e9"}""") @@ -703,17 +703,13 @@ fun script() { val numbered = numbered(listOf("p", "q")) println("numbered ${numbered.joinToString(", ") { "${it.name()} ${it.get()}" }}") numbered.forEach { it.close() } - val at4 = java.net.InetSocketAddress(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1)), 8080) - val at6 = java.net.InetSocketAddress(java.net.Inet6Address.getByAddress(null, loopback, 3), 443) - val (next4, next6) = nexts(at4, at6) - println("nexts ${next4.address.hostAddress} ${next4.port} ${next6.port} ${(next6.address as java.net.Inet6Address).scopeId}") - val (nextIp4, nextIp6) = nextIps( - java.net.InetAddress.getByAddress(byteArrayOf(10, 0, 0, -1)), - java.net.Inet6Address.getByAddress(null, ByteArray(16).also { it[14] = -1; it[15] = -1 }, null as java.net.NetworkInterface?), - ) - val mapped = flip(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1))) - val ips = listOf(nextIp4, nextIp6, mapped, flip(mapped), flip(java.net.Inet6Address.getByAddress(null, loopback, null as java.net.NetworkInterface?))) - println("ips ${ips.joinToString(" ") { ip -> ip.address.joinToString("") { "%02x".format(it) } }}") + val (next4, next6) = nexts(v4, v6) + println("nexts ${next4.ip} ${next4.port} ${next6.port} ${next6.scopeId}") + val (nextIp4, nextIp6) = nextIps(Ipv4Addr(10u, 0u, 0u, 255u), Ipv6Addr(ByteArray(16).also { it[14] = -1; it[15] = -1 })) + val mapped = flip(Ipv4Addr(127u, 0u, 0u, 1u)) + val ips = listOf(nextIp4, nextIp6, mapped, flip(mapped), flip(Ipv6Addr(loopback))) + println("ips ${ips.joinToString(" ") { ipHex(it) }}") + lossless() // serde_json's map iterator, collected as pairs of a key and its document. val jsonSections = sections(listOf(Pair("b", Json.parse("[1]")), Pair("a", Json.parse("2")))) @@ -721,9 +717,9 @@ fun script() { jsonSections.forEach { it.second.close() } // An instant only Rust makes, which each call copies out of its handle, 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}") + val ended = later(began, Duration(1500.milliseconds)) + val same = later(began, Duration.ZERO) + println("instants ${since(ended, began).toKotlinDuration().inWholeMilliseconds} ${since(began, ended).toKotlinDuration().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 -> @@ -840,8 +836,8 @@ open class Broken : java.nio.channels.SeekableByteChannel { override fun close() {} } -/** A path's UTF-8 bytes as Rust's `escape_ascii` prints them. */ -fun escaped(path: String): String = path.encodeToByteArray().joinToString("") { +/** A path's bytes as Rust's `escape_ascii` prints them. */ +fun escaped(path: PathBuf): String = path.toByteArray().joinToString("") { when (val b = it.toInt() and 0xff) { '\t'.code -> "\\t" '\r'.code -> "\\r" @@ -862,6 +858,117 @@ fun check(ok: Boolean, what: String) { } } +/** Bytes in hex, as transcript.rs prints them. */ +fun hex(bytes: ByteArray): String = bytes.joinToString("") { "%02x".format(it) } + +/** An address's octets in hex, so their length shows its family. */ +fun ipHex(ip: IpAddr): String = hex( + when (ip) { + is Ipv4Addr -> ip.octets() + is Ipv6Addr -> ip.octets() + }, +) + +/** A socket address as its IP's octets in hex, its port, flow label, and scope, each IPv6's alone. */ +fun socketHex(addr: SocketAddr): String = when (addr) { + is SocketAddrV4 -> "${hex(addr.ip.octets())}:${addr.port}" + is SocketAddrV6 -> "${hex(addr.ip.octets())}:${addr.port}:${addr.flowinfo}:${addr.scopeId}" +} + +/** Whether [f] throws `IllegalArgumentException`, as a value its Rust type cannot hold does. */ +fun rejects(f: () -> Any?): Boolean = runCatching(f).exceptionOrNull() is IllegalArgumentException + +/** + * Each type's edge values, passed through Rust and back, as transcript.rs prints them; then + * each value the Kotlin type holds and Rust's cannot, which throws, and each conversion to + * and from the idiomatic type, which throws where the other cannot hold a value exactly. + */ +fun lossless() { + val durations = listOf(Duration.ZERO, Duration(0u, 1u), Duration(9223372036u, 854775808u), Duration.MAX) + .map { sameDuration(it) } + println("lossless duration ${durations.joinToString(" ") { "${it.secs}.${it.nanos.toString().padStart(9, '0')}" }}") + println("lossless u64 ${sameU64(0u)} ${sameU64(1uL shl 63)} ${sameU64(ULong.MAX_VALUE)}") + println("lossless usize ${sameUsize(0u)} ${sameUsize(ULong.MAX_VALUE)}") + println("lossless isize ${sameIsize(Long.MIN_VALUE)} ${sameIsize(Long.MAX_VALUE)}") + val two = java.math.BigInteger.valueOf(2) + val wides = listOf(java.math.BigInteger.ZERO, two.pow(64), two.pow(128) - java.math.BigInteger.ONE) + println("lossless u128 ${wides.joinToString(" ") { sameU128(it).toString(16).padStart(32, '0') }}") + println("lossless char ${listOf(0, 0xd7ff, 0xe000, 0x10ffff).joinToString(" ") { sameChar(it).toString(16) }}") + // A path no Rust string spells: bytes that are not UTF-8, or on Windows, WTF-8's lone surrogate. + val unspelled = when (java.io.File.separatorChar) { + '\\' -> byteArrayOf(0xed.toByte(), 0xa0.toByte(), 0x80.toByte()) + else -> byteArrayOf(-1) + } + println("lossless path ${listOf(PathBuf("a/é"), PathBuf(unspelled)).joinToString(" ") { hex(samePath(it).toByteArray()) }}") + val v4 = sameSocketAddrV4(SocketAddrV4(Ipv4Addr(UInt.MAX_VALUE), UShort.MAX_VALUE)) + val v6 = sameSocketAddrV6(SocketAddrV6(Ipv6Addr(ByteArray(16).also { it[1] = 1; it[15] = 2 }), 3u, UInt.MAX_VALUE, UInt.MAX_VALUE)) + println("lossless socket ${socketHex(v4)} ${socketHex(v6)} ${listOf(v4, v6).joinToString(" ") { socketHex(sameSocketAddr(it)) }}") + // Kotlin's own hash of a key is Rust's `Hash`, as its `equals` is Rust's `==`. + Key.new("a").use { a -> + Key.new("a").use { same -> + Key.new("b").use { b -> + println("key ${a.hashCode() == same.hashCode()} ${a.hashCode() == b.hashCode()}") + check(a == same && a != b, "keys compare as Rust's") + check(a.hashCode() == a.hashOf().toLong().hashCode(), "a key hashes as Rust's Hash") + check(setOf(a, b).contains(same), "a key finds its equal in a set") + } + } + } + val v6Max = Ipv6Addr(ByteArray(16).also { it[0] = -1; it[1] = -1; it[15] = 1 }) + println("lossless ip ${ipHex(sameIpAddr(Ipv4Addr(UInt.MAX_VALUE)))} ${ipHex(sameIpAddr(v6Max))}") + + // Durations: exact both ways, and checked against kotlin.time's. + check(rejects { Duration(0u, 1_000_000_000u) }, "a second of nanoseconds is no Duration's") + check(rejects { Duration((-1).milliseconds) } && rejects { Duration(kotlin.time.Duration.INFINITE) }, "negative and infinite durations are no Rust Duration") + check(Duration(1.5.seconds) == Duration(1u, 500_000_000u) && Duration(1.5.seconds).toKotlinDuration() == 1.5.seconds, "a kotlin.time Duration crosses exactly") + check(Duration(0u, 1u) < Duration(1u) && Duration.MAX > Duration(ULong.MAX_VALUE), "durations order as Rust's") + check(rejects { Duration.MAX.toKotlinDuration() }, "kotlin.time has no Duration::MAX") + // Past about 146 years, kotlin.time holds milliseconds alone. + check(rejects { Duration(5_000_000_000u, 1u).toKotlinDuration() }, "kotlin.time drops nanoseconds past 146 years") + check(Duration(Duration(5_000_000_000u, 1_000_000u).toKotlinDuration()) == Duration(5_000_000_000u, 1_000_000u), "whole milliseconds past 146 years convert") + check(sameDuration(Duration(ULong.MAX_VALUE, 1u)) == Duration(ULong.MAX_VALUE, 1u), "seconds past Long.MAX_VALUE cross") + + // Integers and chars Kotlin holds and Rust does not throw before they cross. + for (c in listOf(0xd800, 0xdfff, 0x110000, -1)) check(rejects { sameChar(c) }, "$c is no char") + check(rejects { nextHop(0UL) } && rejects { fartherHop(PairNonZeroU16(0u, 1u)) }, "a zero is no NonZero") + + // Text: a lone surrogate throws, passed alone or encoded, before anything moves. + check(rejects { sameString("a\ud800") } && rejects { sameString("\udc00b") }, "a lone surrogate is no Rust text") + check(sameString("\ud83d\ude00") == "\ud83d\ude00", "a surrogate pair is one char") + check(sameString("x".repeat(1023) + "\ud83d\ude00") == "x".repeat(1023) + "\ud83d\ude00", "a pair split across chunks is one char") + check(rejects { joinWords(listOf("a", "\ud800")) }, "a lone surrogate in a list throws") + val kept = Counter.new("kept", 1u) + check(rejects { Board.new(listOf(Pair("\ud800", kept))) }, "a lone surrogate beside a moved object throws") + check(kept.get() == 1uL, "an object passed beside text that threw stays open") + kept.close() + + // Paths: any bytes on Unix, and text only where it is UTF-8. + val stray = stray() + check(stray.toByteArray().contentEquals(byteArrayOf(-1)) && stray == PathBuf(byteArrayOf(-1)), "a path keeps bytes no String holds") + check(rejects { stray.toText() } && rejects { stray.toPath() } && stray.toString() == "\ufffd", "a path that is not UTF-8 has no text") + check(rejects { PathBuf("\ud800") }, "a lone surrogate has no path") + check(PathBuf("a/é").toText() == "a/é" && PathBuf("a/é").toPath() == java.nio.file.Paths.get("a/é"), "a UTF-8 path converts") + check(_wtf8(byteArrayOf(0xed.toByte(), 0xa0.toByte(), 0x80.toByte())) && !_wtf8(byteArrayOf(0xed.toByte(), 0xa0.toByte(), 0x80.toByte(), 0xed.toByte(), 0xb0.toByte(), 0x80.toByte())), "a lone surrogate is WTF-8, a pair encoded apart is not") + check(!_wtf8(byteArrayOf(-1)) && !_wtf8(byteArrayOf(0xc0.toByte(), 0x80.toByte())) && _wtf8("a/é😀".encodeToByteArray()), "WTF-8 is UTF-8 otherwise") + + // Addresses: exact by construction, and checked to and from java.net's. + val loopback = ByteArray(16).also { it[15] = 1 } + val scoped = java.net.Inet6Address.getByAddress(null, loopback, 3) + check(rejects { Ipv6Addr(scoped) } && rejects { IpAddr.of(scoped) } && rejects { Ipv4Addr(ByteArray(3)) }, "a scope or a wrong length is no Rust IP") + val unresolved = java.net.InetSocketAddress.createUnresolved("localhost", 1) + check(rejects { SocketAddr.of(unresolved) } && rejects { SocketAddrV6(java.net.InetSocketAddress(java.net.InetAddress.getByAddress(byteArrayOf(1, 2, 3, 4)), 1)) }, "an unresolved or other family's address is refused") + check(rejects { SocketAddrV6(Ipv6Addr(loopback), 1u, 1u).toInetSocketAddress() }, "Java keeps no flow label") + check(rejects { SocketAddrV6(Ipv6Addr(loopback), 1u, 0u, UInt.MAX_VALUE).toInetSocketAddress() }, "Java keeps no scope past Int.MAX_VALUE") + val at6 = SocketAddr.of(java.net.InetSocketAddress(scoped, 443)) + check(at6 == SocketAddrV6(Ipv6Addr(loopback), 443u, 0u, 3u) && at6.toString() == "[::1%3]:443", "an IPv6 address keeps its scope") + check(at6.toInetSocketAddress() == java.net.InetSocketAddress(scoped, 443), "an IPv6 address converts back") + val mapped = Ipv6Addr(ByteArray(16).also { it[10] = -1; it[11] = -1; it[12] = 127; it[15] = 1 }) + check(mapped.toString() == "::ffff:127.0.0.1" && (mapped as IpAddr).toInetAddress() is java.net.Inet6Address, "an IPv4-mapped address stays IPv6") + check(IpAddr.of(java.net.InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1))) == Ipv4Addr(127u, 0u, 0u, 1u), "an IPv4 address converts") + check(Ipv6Addr(ByteArray(16).also { it[1] = 1; it[15] = 2 }).toString() == "1::2" && Ipv6Addr(ByteArray(16)).toString() == "::", "IPv6 prints as Rust's") + check(SocketAddrV4(Ipv4Addr(127u, 0u, 0u, 1u), 80u).toString() == "127.0.0.1:80", "IPv4 prints as Rust's") +} + // A queue of jobs that a test runs when it chooses: the dispatcher coroutines resume on. class Queue : Executor { private val jobs = ConcurrentLinkedQueue() @@ -1133,20 +1240,6 @@ fun broken(which: String) { } kept!!.get() } - // An IPv6 address where Rust takes `SocketAddrV4`. - "family" -> nexts( - java.net.InetSocketAddress(java.net.Inet6Address.getByAddress(null, ByteArray(16), null as java.net.NetworkInterface?), 1), - java.net.InetSocketAddress(java.net.Inet6Address.getByAddress(null, ByteArray(16), null as java.net.NetworkInterface?), 1), - ) - // An IPv6 address where Rust takes `Ipv4Addr`. - "ip family" -> nextIps( - java.net.Inet6Address.getByAddress(null, ByteArray(16), null as java.net.NetworkInterface?), - java.net.Inet6Address.getByAddress(null, ByteArray(16), null as java.net.NetworkInterface?), - ) - // An IPv6 address with a scope, which Rust's `IpAddr` has no room for. - "ip scope" -> flip(java.net.Inet6Address.getByAddress(null, ByteArray(16), 3)) - // No String holds a path that is not UTF-8. - "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" -> Moment.new(0u, 1000u)!!.let { start -> @@ -1171,16 +1264,17 @@ fun broken(which: String) { // Rust's bytes only the GC, once no Bytes reaching them is reachable. Rust's bytes up to // 1 KiB cross copied, freed at the call, and longer ones lent. // Kotlin's text and bytes, which Rust copies or encodes once from where Kotlin keeps them: -// each reads back as Kotlin's own encoder has it, at a call's top level and in a record. +// each reads back as it was, at a call's top level and in a record, and text holding a lone +// surrogate, which UTF-8 has no form of, throws. fun encoded() { - val texts = listOf( - "", "a\u0000b", "é\u4e2d\uD83D\uDE00", "\uD800x", "x\uDC00", "end\uD83D", "\uDE00\uD83D", - "a".repeat(1023) + "\uD83D\uDE00", "a".repeat(1023) + "\uD83Dz", "\u4e2d\uD83D\uDE00".repeat(1500), - ) + val texts = listOf("", "a\u0000b", "é\u4e2d\uD83D\uDE00", "a".repeat(1023) + "\uD83D\uDE00", "\u4e2d\uD83D\uDE00".repeat(1500)) for (text in texts) { - val expected = text.encodeToByteArray().decodeToString() - check(greet(text, Shape.SQUARE) == "hello $expected, a Square", "text crosses as its UTF-8") - check(tickName(Label(text, null, null)) == expected, "a record's text crosses as its UTF-8") + check(greet(text, Shape.SQUARE) == "hello $text, a Square", "text crosses as its UTF-8") + check(tickName(Label(text, null, null)) == text, "a record's text crosses as its UTF-8") + } + for (text in listOf("\uD800x", "x\uDC00", "end\uD83D", "\uDE00\uD83D", "a".repeat(1023) + "\uD83Dz")) { + check(rejects { greet(text, Shape.SQUARE) }, "a lone surrogate throws") + check(rejects { tickName(Label(text, null, null)) }, "a record's lone surrogate throws") } val body = ByteArray(300) { it.toByte() } val sliced = nextPacket(Packet(1u, Bytes(body).slice(7, 290))).payload diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 663f2b1a7..87f3734a7 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -112,6 +112,17 @@ def numbers(what, n): print(f"{what} some {'None' if n.some is None else f'Some({n.some.tolist()})'}") +def millis(duration): + """A duration in whole milliseconds.""" + return duration.secs * 1000 + duration.nanos // 1_000_000 + + +def socket_hex(addr): + """A socket address as the native script's socket_hex prints it.""" + ip = ipaddress.ip_address(addr[0]).packed.hex() + return ":".join([ip] + [str(part) for part in addr[1:]]) + + def check(ok, what): if not ok: print(f"failed: {what}", file=sys.stderr) @@ -710,8 +721,8 @@ async def polls(): print(f"longest {api.longest(['a', 'abc'])} {api.longest([])}") print(f'first of Some("{api.first_of(["a", "b"])}") {api.first_of([]) or "None"}') limits = api.byte_limits(b'a', b'bc') - waits = api.LimitsDuration(datetime.timedelta(milliseconds=5), datetime.timedelta(seconds=2)) - breadth = api.breadth(waits) // datetime.timedelta(milliseconds=1) + waits = api.LimitsDuration(api.Duration(0, 5_000_000), api.Duration(2)) + breadth = millis(api.breadth(waits)) print(f"limits {bytes(limits.low).decode()} {bytes(limits.high).decode()} breadth {breadth}") joined = bytes(api.joined([b'ab', b'', b'c'])).decode() print(f"joined {joined} {len(api.joined([]))} read {api.read_len(b'hello')}") @@ -749,15 +760,36 @@ async def polls(): ips = [nextip4, nextip6, mapped, api.flip(mapped), api.flip(ipaddress.IPv6Address("::1"))] print("ips " + " ".join(ip.packed.hex() for ip in ips)) + # Lossless round trips: each type's edge values, passed through Rust and back. + durations = [api.Duration(0), api.Duration(0, 1), api.Duration(9223372036, 854775808), api.Duration(2**64 - 1, 999_999_999)] + print("lossless duration " + " ".join(f"{d.secs}.{d.nanos:09}" for d in map(api.same_duration, durations))) + print(f"lossless u64 {api.same_u64(0)} {api.same_u64(2**63)} {api.same_u64(2**64 - 1)}") + usize = 2 ** (struct.calcsize("P") * 8) + print(f"lossless usize {api.same_usize(0)} {api.same_usize(usize - 1)}") + print(f"lossless isize {api.same_isize(-usize // 2)} {api.same_isize(usize // 2 - 1)}") + print("lossless u128 " + " ".join(f"{api.same_u128(v):032x}" for v in [0, 2**64, 2**128 - 1])) + print("lossless char " + " ".join(f"{ord(api.same_char(c)):x}" for c in ["\0", "\ud7ff", "\ue000", "\U0010ffff"])) + # A path no Rust string spells: bytes that are not UTF-8, or on Windows, WTF-8's lone + # surrogate, which its filesystem encoding passes through. + paths = ["a/é", os.fsdecode(b"\xff" if os.name == "posix" else b"\xed\xa0\x80")] + print("lossless path " + " ".join(os.fsencode(api.same_path(p)).hex() for p in paths)) + sv4 = api.same_socket_addr_v4(("255.255.255.255", 65535)) + sv6 = api.same_socket_addr_v6(("1::2", 3, 2**32 - 1, 2**32 - 1)) + sockets = [sv4, sv6] + [api.same_socket_addr(a) for a in [sv4, sv6]] + print("lossless socket " + " ".join(socket_hex(a) for a in sockets)) + key = api.Key.new("a") + print(f"key {flag(hash(key) == hash(api.Key.new('a')))} {flag(hash(key) == hash(api.Key.new('b')))}") + ips = [api.same_ip_addr(ipaddress.IPv4Address("255.255.255.255")), api.same_ip_addr(ipaddress.IPv6Address("ffff::1"))] + print("lossless ip " + " ".join(ip.packed.hex() for ip in ips)) + # serde_json's map iterator, collected as pairs of a key and its document. sections = api.sections([("b", api.Json.parse("[1]")), ("a", api.Json.parse("2"))]) print("sections " + ", ".join(f"{key} {value.to_text()}" for key, value in sections)) # An instant only Rust makes, which each call copies out of its handle, and compares. - ms = datetime.timedelta(milliseconds=1) began = api.now() - ended = api.later(began, datetime.timedelta(milliseconds=1500)) - same = api.later(began, datetime.timedelta()) - print(f"instants {api.since(ended, began) // ms} {api.since(began, ended) // ms} {flag(began == same)} {flag(began == ended)}") + ended = api.later(began, api.Duration.from_timedelta(datetime.timedelta(milliseconds=1500))) + same = api.later(began, api.Duration(0)) + print(f"instants {millis(api.since(ended, began))} {millis(api.since(began, ended))} {flag(began == same)} {flag(began == ended)}") print(f"has_instant {flag(api.has_instant(began))} {flag(api.has_instant(None))}") # tracing's level, and its filter as an optional level whose None is `OFF`. verbosity = api.Verbosity.new(api.Level.WARN) @@ -1367,8 +1399,8 @@ def forwarding(): def values(): """The values Python's types bound: integers checked against their Rust type's range, - durations truncated to the microsecond up to `timedelta.max`, which stands for longer, - and on Unix, a path whose bytes are not UTF-8, which crosses as `os.fsdecode` decodes it, + durations and their conversions to and from `timedelta`, which raise rather than lose + anything, a key hashing by Rust's `Hash`, a zero passed for a `NonZero`, which raises, and on Unix, a path whose bytes are not UTF-8, which crosses as `os.fsdecode` decodes it, and back as it came. Also a borrowed tuple, returned whole.""" def raises(error, f, what): @@ -1422,10 +1454,26 @@ def raises(error, f, what): raises(api.ErrorException, lambda: api.corner(False), "a borrowed tuple's error raises") td = datetime.timedelta - check(api.doubled_ttl(td(seconds=1, microseconds=5)) == td(seconds=2, microseconds=10), "a duration crosses") - check(api.doubled_ttl(td(days=600_000_000)) == td.max, "a duration past timedelta's is its max") - check(api.doubled_ttl(td.max) == td.max, "timedelta's max is Duration::MAX") - raises(ValueError, lambda: api.doubled_ttl(td(seconds=-1)), "a negative duration raises") + longest = api.Duration(2**64 - 1, 999_999_999) + check(api.doubled_ttl(api.Duration(1, 5)) == api.Duration(2, 10), "a duration crosses") + check(api.doubled_ttl(api.Duration(1, 5)) in {api.Duration(2, 10)}, "a returned duration hashes as one Python makes") + check(api.doubled_ttl(longest) == longest, "the longest crosses") + check(api.Duration(1, 2) < api.Duration(1, 3) < api.Duration(2), "durations order as Rust's") + check(api.Duration.from_timedelta(td(days=1, microseconds=5)) == api.Duration(86_400, 5_000), "from a timedelta") + check(api.Duration(86_400, 5_000).to_timedelta() == td(days=1, microseconds=5), "to a timedelta") + check(api.Duration.from_timedelta(td.max).to_timedelta() == td.max, "timedelta's max round trips") + raises(ValueError, lambda: api.Duration.from_timedelta(td(seconds=-1)), "a negative timedelta raises") + raises(ValueError, lambda: api.Duration(0, 1).to_timedelta(), "a part of a microsecond raises") + raises(OverflowError, lambda: api.Duration(2**64 - 1).to_timedelta(), "past timedelta's max raises") + raises(ValueError, lambda: api.doubled_ttl(api.Duration(0, 10**9)), "a billion nanoseconds raises") + raises(OverflowError, lambda: api.doubled_ttl(api.Duration(2**64)), "seconds past u64 raise") + raises(OverflowError, lambda: api.doubled_ttl(api.Duration(-1)), "negative seconds raise") + raises(TypeError, lambda: api.doubled_ttl(td(seconds=1)), "a timedelta is no Duration") + raises(ValueError, lambda: api.next_hop(0), "a zero NonZero raises") + raises(ValueError, lambda: api.farther_hop(api.PairNonZeroU16(0, 1)), "a zero NonZero field raises") + key = api.Key.new("a") + check(len({key, api.Key.new("a"), api.Key.new("b")}) == 2, "equal keys are one set member") + check(hash(api.Url.parse("https://a/")) == hash(api.Url.parse("https://a/")), "a URL hashes by Rust's Hash") # The module serves wasm32 too: what only wasm32 has raises on the host's library. check(api.pointer_bits(api.platform()) == api.pointer_bits(api.Platform.Native(64)) == 64, "the host is native") diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index f427a272f..784cb0c56 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -129,10 +129,10 @@ func listed(_ items: borrowing UniqueArray, _ each: (borrowing return out.joined(separator: ", ") } -/// A path's UTF-8 bytes as Rust's `escape_ascii` prints them. -func escaped(_ path: String) -> String { +/// A path's bytes as Rust's `escape_ascii` prints them. +func escaped(_ path: PathBuf) -> String { var out = "" - for b in path.utf8 { + for b in path.bytes { switch b { case 0x09: out += "\\t" case 0x0d: out += "\\r" @@ -451,12 +451,15 @@ func script() { print(describe(event)) } check(advance(.init_(1)) == .init_(0) && advance(.init_(0)) == .done, "a variant named init crosses") - // Rust's seconds are a u64, past Int64's that Swift's components give. - let rustMax = Duration.seconds(Int64.max) * 2 + .seconds(1) + .nanoseconds(999_999_999) - let past = Duration.seconds(Int64.max) + .seconds(1) - check(extended(past, past - .nanoseconds(1)) == rustMax, "a duration past Int64's seconds crosses") - check(extended(rustMax - .nanoseconds(3), .nanoseconds(3)) == rustMax, "Rust's longest duration crosses") + // Rust's seconds are a u64, past Int64's. + let rustMax = try! RustDuration(secs: .max, nanos: 999_999_999) + let past = RustDuration.seconds(UInt64(Int64.max) + 1) + check( + extended(past, try! RustDuration(secs: UInt64(Int64.max), nanos: 999_999_999)) == rustMax, + "a duration past Int64's seconds crosses") + check(extended(try! RustDuration(secs: .max, nanos: 999_999_996), .nanoseconds(3)) == rustMax, "Rust's longest duration crosses") check(extended(rustMax, .nanoseconds(1)) == nil, "Rust refuses a duration past its longest") + durations(rustMax) // A `Copy` handle passed by value is copied, so it stays usable. let stamp = Stamp.fromMillis(2500) @@ -707,9 +710,9 @@ func script() { } catch { check(false, "parse throws its message") } - let v4 = SocketAddr(ip: [127, 0, 0, 1], port: 8080) - let loopback: [UInt8] = Array(repeating: 0, count: 15) + [1] - let v6 = SocketAddr(ip: loopback, port: 443, scopeId: 3) + let v4 = SocketAddr.v4(SocketAddrV4(ip: Ipv4Addr(127, 0, 0, 1), port: 8080)) + let loopback = Ipv6Addr(bits: U128(1)) + let v6 = SocketAddr.v6(SocketAddrV6(ip: loopback, port: 443, scopeId: 3)) print("ports \(ports([v4, v6]))") let peer = Peer(addr: v6, names: ["a", "b"]).next("c") print("peer \(debug(peer)) \(peer.aliases())") @@ -904,7 +907,7 @@ func script() { let waits = breadth(LimitsDuration(low: .milliseconds(5), high: .seconds(2))) let low = String(decoding: limits.low, as: UTF8.self) let high = String(decoding: limits.high, as: UTF8.self) - print("limits \(low) \(high) breadth \(waits.components.seconds * 1000 + waits.components.attoseconds / 1_000_000_000_000_000)") + print("limits \(low) \(high) breadth \(waits.secs * 1000 + UInt64(waits.nanos) / 1_000_000)") let parts = String(decoding: Api.joined([Bytes("ab".utf8), Bytes(), Bytes("c".utf8)]), as: UTF8.self) print("joined \(parts) \(Api.joined([]).count) read \(readLen(Bytes("hello".utf8)))") let doc = try! Json.parse(#"{"b": [1, 2.5, null], "a": "é"}"#) @@ -940,12 +943,13 @@ func script() { print("beyond \(try! trail.beyond(1).map { "\($0.x) \($0.y)" }.joined(separator: ", "))") let numbered = numbered(["p", "q"]) print("numbered \(listed(numbered) { "\(try! $0.name()) \(try! $0.get())" })") - let (next4, next6) = nexts(SocketAddr(ip: [127, 0, 0, 1], port: 8080), v6) - print("nexts \(next4.ip.map(String.init).joined(separator: ".")) \(next4.port) \(next6.port) \(next6.scopeId)") - let (nextIp4, nextIp6) = nextIps(IpAddr(ip: [10, 0, 0, 255]), IpAddr(ip: Array(repeating: 0, count: 14) + [255, 255])) - let mapped = flip(IpAddr(ip: [127, 0, 0, 1])) - let ips = [nextIp4, nextIp6, mapped, flip(mapped), flip(IpAddr(ip: Array(repeating: 0, count: 15) + [1]))] - print("ips \(ips.map { $0.ip.map { ($0 < 16 ? "0" : "") + String($0, radix: 16) }.joined() }.joined(separator: " "))") + let (next4, next6) = nexts(SocketAddrV4(ip: Ipv4Addr(127, 0, 0, 1), port: 8080), SocketAddrV6(ip: loopback, port: 443, scopeId: 3)) + print("nexts \(next4.ip.octets.map(String.init).joined(separator: ".")) \(next4.port) \(next6.port) \(next6.scopeId)") + let (nextIp4, nextIp6) = nextIps(Ipv4Addr(10, 0, 0, 255), Ipv6Addr(bits: U128(0xffff))) + let mapped = flip(.v4(Ipv4Addr(127, 0, 0, 1))) + let ips = [IpAddr.v4(nextIp4), .v6(nextIp6), mapped, flip(mapped), flip(.v6(loopback))] + print("ips \(ips.map(ipHex).joined(separator: " "))") + lossless() // serde_json's map iterator, collected as pairs of a key and its document. var sectionPairs = UniqueArray() @@ -954,10 +958,10 @@ func script() { let jsonSections = try! sections(sectionPairs) print("sections \(listed(jsonSections) { "\($0._0) \(try! $0._1.toText())" })") // An instant only Rust makes, which each call copies out of its handle, and compares. - let millis = { (d: Swift.Duration) in d.components.seconds * 1000 + d.components.attoseconds / 1_000_000_000_000_000 } + let millis = { (d: RustDuration) in d.secs * 1000 + UInt64(d.nanos) / 1_000_000 } let began = now() let ended = later(began, .milliseconds(1500)) - print("instants \(millis(since(ended, began))) \(millis(since(began, ended))) \(began == later(began, .zero)) \(began == ended)") + print("instants \(millis(since(ended, began))) \(millis(since(began, ended))) \(began == later(began, .seconds(0))) \(began == ended)") print("has_instant \(hasInstant(began)) \(hasInstant(nil))") // tracing's level, and its filter as an optional level whose nil is `OFF`. var verbosity = Verbosity.new(.warn) @@ -1011,9 +1015,16 @@ func script() { } print("non zeros \(describeNonZeros(nonZeros()))") for hop in [nil, 1, UInt64.max] as [UInt64?] { - print("hop \(nextHop(hop).map { "Some(\($0))" } ?? "None")") - } - print("farther hop \(fartherHop(PairNonZeroU16(left: 3, right: .max)))") + print("hop \(nextHop(hop.map { try! NonZero($0) }).map { "Some(\($0))" } ?? "None")") + } + print("farther hop \(fartherHop(PairNonZeroU16(left: try! NonZero(3), right: try! NonZero(.max))))") + // A zero, which Swift's integer holds and Rust's `NonZero` does not, makes none, and a + // record holding one keeps the protocols its traits give. + check(refused("NonZero(_:)") { _ = try NonZero(0) }, "a zero makes no NonZero") + check(Set([try! NonZero(-1), try! NonZero(-1), try! NonZero(.min)]).count == 2, "a set of NonZero") + check(try! NonZero(.min) < NonZero(1), "a NonZero compares as its integer") + let debug: any CustomDebugStringConvertible = nonZeros() + check(debug.debugDescription == describeNonZeros(nonZeros()), "a record of NonZero is Debug") targets() } @@ -1484,6 +1495,108 @@ func lentKept() { } #endif +/// `bytes` in hex, two digits each. +func hexBytes(_ bytes: some Sequence) -> String { + bytes.map { String($0, radix: 16).leftPadded(2) }.joined() +} + +/// An IP address's octets in hex, so their length shows its family. +func ipHex(_ ip: IpAddr) -> String { + switch ip { + case .v4(let ip): return hexBytes(ip.octets) + case .v6(let ip): return hexBytes(ip.octets) + } +} + +/// A socket address as its IP's octets in hex, its port, flow label, and scope, each +/// IPv6's alone, apart by colons. +func socketHex(_ addr: SocketAddr) -> String { + switch addr { + case .v4(let v4): return "\(hexBytes(v4.ip.octets)):\(v4.port)" + case .v6(let v6): return "\(hexBytes(v6.ip.octets)):\(v6.port):\(v6.flowinfo):\(v6.scopeId)" + } +} + +/// Whether `body` throws a `CheckError` from `function`. +func refused(_ function: String, _ body: () throws -> Void) -> Bool { + do { + try body() + return false + } catch let e as CheckError { + return e.function == function + } catch { + return false + } +} + +/// A `RustDuration`'s conversions to and from `Swift.Duration`, which holds every one, and +/// those it refuses, each throwing. +func durations(_ rustMax: RustDuration) { + let swiftMax = Duration.seconds(Int64.max) * 2 + .seconds(1) + .nanoseconds(999_999_999) + check(rustMax.duration == swiftMax, "Rust's longest duration is a Swift.Duration") + check(try! RustDuration(swiftMax) == rustMax, "Rust's longest Swift.Duration is a RustDuration") + check(try! RustDuration(.nanoseconds(1_500_000_001)) == RustDuration(secs: 1, nanos: 500_000_001), "a duration converts") + check(try! RustDuration(.zero) == .seconds(0) && RustDuration.seconds(0).duration == .zero, "zero converts") + check(RustDuration.milliseconds(.max) == (try! RustDuration(secs: UInt64.max / 1000, nanos: 615_000_000)), "milliseconds are total") + check(RustDuration.microseconds(1_000_001) == (try! RustDuration(secs: 1, nanos: 1000)), "microseconds are total") + check(RustDuration.nanoseconds(.max).duration == .nanoseconds(Int64.max) * 2 + .nanoseconds(1), "nanoseconds are total") + check(RustDuration.seconds(1) < (try! RustDuration(secs: 1, nanos: 1)), "durations order as Rust's") + check(refused("RustDuration(secs:nanos:)") { _ = try RustDuration(secs: 0, nanos: 1_000_000_000) }, "a second's nanoseconds throw") + check(refused("RustDuration(_:)") { _ = try RustDuration(.nanoseconds(-1)) }, "a negative duration throws") + check(refused("RustDuration(_:)") { _ = try RustDuration(swiftMax + .nanoseconds(1)) }, "a duration past Rust's longest throws") + check(refused("RustDuration(_:)") { _ = try RustDuration(.nanoseconds(1) / 2) }, "a part below a nanosecond throws") + check(refused("RustDuration(_:)") { _ = try RustDuration(swiftMax + .nanoseconds(1) / 2) }, "past the longest by less than a nanosecond throws") +} + +/// Each type's edge values, passed through Rust and back, as transcript.rs prints them, +/// and the conversions to and from each runtime type, which throw or return nil for a +/// value Rust's type cannot hold. +func lossless() { + let spans = [ + RustDuration.seconds(0), .nanoseconds(1), .nanoseconds(UInt64(Int64.max) + 1), + try! RustDuration(secs: .max, nanos: 999_999_999), + ].map { sameDuration($0) } + print("lossless duration \(spans.map { "\($0.secs).\(String($0.nanos).leftPadded(9))" }.joined(separator: " "))") + print("lossless u64 \(sameU64(0)) \(sameU64(1 << 63)) \(sameU64(.max))") + print("lossless usize \(sameUsize(0)) \(sameUsize(.max))") + print("lossless isize \(sameIsize(.min)) \(sameIsize(.max))") + let wides = [U128(0), U128(high: 1, low: 0), U128(high: .max, low: .max)].map { sameU128($0) } + print("lossless u128 \(wides.map { hexBytes(withUnsafeBytes(of: ($0.high.bigEndian, $0.low.bigEndian)) { Array($0) }) }.joined(separator: " "))") + let chars: [Unicode.Scalar] = ["\0", "\u{d7ff}", "\u{e000}", "\u{10ffff}"] + print("lossless char \(chars.map { String(sameChar($0).value, radix: 16) }.joined(separator: " "))") + check(sameString("a\u{0}é\u{10ffff}") == "a\u{0}é\u{10ffff}", "text crosses") + // A path that is not UTF-8, which no String holds, keeps its bytes. + let strayPath = try! stray() + check(strayPath.string == nil && PathBuf("a/é").string == "a/é", "a path's text is nil unless UTF-8") + print("lossless path \(hexBytes(samePath("a/é").bytes)) \(hexBytes(samePath(strayPath).bytes))") + check(samePath(strayPath) == strayPath, "a path that is not UTF-8 crosses back") + try! visitStray { path in check(path == strayPath, "a lent path that is not UTF-8 keeps its bytes") } + check(samePath(PathBuf(bytes: Bytes([0xff]))) == strayPath, "a caller's bytes make a path") + let v4 = sameSocketAddrV4(SocketAddrV4(ip: Ipv4Addr(bits: .max), port: .max)) + let ip6 = Ipv6Addr(bits: U128(high: 1 << 48, low: 2)) + let v6 = sameSocketAddrV6(SocketAddrV6(ip: ip6, port: 3, flowinfo: .max, scopeId: .max)) + let addrs = [SocketAddr.v4(v4), .v6(v6)] + print("lossless socket \(addrs.map(socketHex).joined(separator: " ")) \(addrs.map { socketHex(sameSocketAddr($0)) }.joined(separator: " "))") + // A key hashes by Rust's `Hash`, which agrees with its `==`. + let a = Key.new("a") + let b = Key.new("b") + print("key \(hashed(a) == hashed(Key.new("a"))) \(hashed(a) == hashed(b))") + let edge6 = Ipv6Addr(octets: [0xff, 0xff] + Array(repeating: 0, count: 13) + [1])! + print("lossless ip \(ipHex(sameIpAddr(.v4(Ipv4Addr(255, 255, 255, 255))))) \(ipHex(sameIpAddr(.v6(edge6))))") + // Octets convert both ways, and only exactly as many as the family holds. + check(Ipv4Addr(octets: [1, 2, 3, 4]) == Ipv4Addr(1, 2, 3, 4) && Ipv4Addr(1, 2, 3, 4).bits == 0x0102_0304, "IPv4 octets convert") + check(Ipv6Addr(octets: edge6.octets) == edge6 && edge6.bits == U128(high: 0xffff << 48, low: 1), "IPv6 octets convert") + check(Ipv4Addr(octets: [1, 2, 3]) == nil && Ipv6Addr(octets: [UInt8](repeating: 0, count: 4)) == nil, "octets of another length convert to nil") + check(SocketAddr(ip: .v6(ip6), port: 3) == .v6(SocketAddrV6(ip: ip6, port: 3)) && addrs[1].ip == .v6(ip6) && addrs[0].port == .max, "a socket address is its IP and port") +} + +/// `key`'s hash by Swift's own hasher, which `Hashable` uses. +func hashed(_ key: borrowing Key) -> Int { + var hasher = Swift.Hasher() + key.hash(into: &hasher) + return hasher.finalize() +} + // Each breaks a contract, which must fail with a message naming it. func broken(_ which: String) { #if os(Linux) @@ -1508,19 +1621,6 @@ func broken(_ which: String) { } catch { check(false, "a failed check throws a CheckError") } - // An IPv6 address where Rust takes `SocketAddrV4`. - case "family": - let any = SocketAddr(ip: Array(repeating: 0, count: 16), port: 1) - _ = nexts(any, any) - // An IPv6 address where Rust takes `Ipv4Addr`. - case "ip family": - let any = IpAddr(ip: Array(repeating: 0, count: 16)) - _ = nextIps(any, any) - // No String holds a path that is not UTF-8. - case "stray": - _ = try! stray() - case "lent stray": - try! visitStray { _ in } // The file serves wasm32 too, whose items the host's library lacks, where Swift cannot // throw. case "variant": diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index f76c3a7f6..54ff6fa79 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -231,10 +231,6 @@ fn api_c() { ), ("enum", "api_greet: an enum value is invalid"), ("bound", "api_count: a bound's kind is invalid"), - ("socket", "api_ports: an IPv4 socket address has IPv6 bytes or fields"), - ("family", "api_nexts: an IPv6 socket address where IPv4 is taken"), - ("ip", "api_flip: an IPv4 address has IPv6 bytes"), - ("ip family", "api_next_ips: an IPv6 address where IPv4 is taken"), ("array", "api_vec_u16_free: an array is not as Rust returned it"), ("future", "api_after: a future's poll is null"), ("started", "api_after_start: a future's poll is null"), @@ -865,10 +861,6 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { "outlived", "Native$__Refused: api_Meter_count: a handle lent from another outlived it", ), - ("stray", "MalformedInputException"), - ("family", "an IPv6 address has no Rust SocketAddrV4"), - ("ip family", "an IPv6 address has no Rust Ipv4Addr"), - ("ip scope", "an IPv6 address with a scope has no Rust IpAddr"), ] { // Under a piped core pattern the handler still runs at a limit of 0, so the // status reports a core though none is written. @@ -1169,11 +1161,7 @@ fn api_swift() { assert!(stderr.contains(message), "Swift {case} printed {stderr}"); } for (case, message) in [ - ("family", "api_nexts: an IPv6 socket address where IPv4 is taken"), - ("ip family", "api_next_ips: an IPv6 address where IPv4 is taken"), ("outlived", OUTLIVED), - ("stray", "a path from Rust is not UTF-8"), - ("lent stray", "a path from Rust is not UTF-8"), ("variant", "api_Platform.Wasm: it is not on this library's target"), ("plain variant", "api_Family.Wasm: it is not on this library's target"), ] { @@ -1279,11 +1267,6 @@ fn api_dart() { "outlived", "Bad state: api_Meter_count: a handle lent from another outlived it", ), - ("family", "an IPv6 address has no Rust SocketAddrV4"), - ("ip family", "an IPv6 address has no Rust Ipv4Addr"), - ("ip unix", "a Unix address has no Rust IpAddr"), - ("stray", "a path from Rust is not UTF-8"), - ("lent stray", "a path from Rust is not UTF-8"), ] { let output = dart_run(&exe, &[case]).output().unwrap(); failed(&output, &format!("Dart {case}")); @@ -1485,13 +1468,8 @@ fn api_go() { ), ("nil reader", "a nil io.ReadSeeker"), ("returned zero", "a zero Moment, which only Rust makes"), - ("family", "an address that is not IPv4 has no Rust SocketAddrV4"), - ("ip family", "an address that is not IPv4 has no Rust Ipv4Addr"), - ("ip scope", "an IPv6 address with a zone has no Rust IpAddr"), ("outlived", OUTLIVED), - ("text", "api_greet: a string is not UTF-8"), ("callback panicked", "a log failed"), - ("duration", "a Rust Duration past time.Duration's longest"), ("target", "api_wasm_only: it is not on this library's target"), ("variant", "api_Platform.Wasm: it is not on this library's target"), ("plain variant", "api_Family.Wasm: it is not on this library's target"), diff --git a/ffi/tests/fixtures/shapes.rs b/ffi/tests/fixtures/shapes.rs index 32adfa287..cac7d568e 100644 --- a/ffi/tests/fixtures/shapes.rs +++ b/ffi/tests/fixtures/shapes.rs @@ -279,10 +279,7 @@ impl Emit { Shape::Buf => ("bytes::Bytes".into(), "bytes::Bytes::from(r.bytes())".into()), Shape::Duration => ("std::time::Duration".into(), "r.duration()".into()), Shape::Array(n) => (format!("[u8; {n}]"), "std::array::from_fn(|_| r.next() as u8)".into()), - Shape::PathBuf => ( - "std::path::PathBuf".into(), - "std::path::PathBuf::from(r.string())".into(), - ), + Shape::PathBuf => ("std::path::PathBuf".into(), "r.path()".into()), Shape::SocketAddr(family) | Shape::IpAddr(family) => { (format!("std::net::{family}"), format!("r.{}()", family.to_lowercase())) } @@ -447,9 +444,27 @@ impl Rng { (0..self.below(6)).map(|_| self.next() as u8).collect() } - /// Up to 2^33 seconds, past 136 years: Go's `time.Duration` holds 292. + /// Any, the longest and those past what a nanosecond or millisecond `i64` holds + /// among them. fn duration(&mut self) -> std::time::Duration { - std::time::Duration::new(self.next() >> 31, (self.next() % 1_000_000_000) as u32) + let nanos = match self.below(4) { + 0 => 0, + 1 => 999_999_999, + _ => (self.next() % 1_000_000_000) as u32, + }; + std::time::Duration::new(self.int(), nanos) + } + + /// Text, or on Unix bytes that are not UTF-8, as a path there may hold. + fn path(&mut self) -> std::path::PathBuf { + #[cfg(unix)] + if self.below(3) == 0 { + use std::os::unix::ffi::OsStringExt; + let mut bytes = self.string().into_bytes(); + bytes.push(0x80 | self.next() as u8 & 0x3f); + return std::ffi::OsString::from_vec(bytes).into(); + } + self.string().into() } fn socketaddrv4(&mut self) -> std::net::SocketAddrV4 { @@ -869,32 +884,6 @@ impl Built { } } -/// Whether `shape` or a shape within it meets `f`. -fn within(shape: &Shape, f: &dyn Fn(&Shape) -> bool) -> bool { - f(shape) - || match shape { - Shape::Option(inner) - | Shape::Vec(inner) - | Shape::Set(inner) - | Shape::Box(inner) - | Shape::Enum(inner) - | Shape::Object(inner) => within(inner, f), - Shape::Map(key, value) => within(key, f) || within(value, f), - Shape::Tuple(elements) | Shape::Record(elements) => elements.iter().any(|e| within(e, f)), - _ => false, - } -} - -fn duration(shape: &Shape) -> bool { - within(shape, &|s| matches!(s, Shape::Duration)) -} - -fn ipv6(shape: &Shape) -> bool { - within(shape, &|s| { - matches!(s, Shape::SocketAddr("SocketAddr" | "SocketAddrV6")) - }) -} - /// A round trip a binding gets wrong today. struct Known { language: &'static str, @@ -909,38 +898,7 @@ struct Known { /// it gets wrong, the shapes it skips for it, and the quest. The nightly fails on an entry /// matching no exported shape, but its shapes stay skipped after a fix, so the quest that /// fixes one deletes its entry. -const KNOWN: &[Known] = &[ - Known { - language: "python", - what: "a Duration's nanoseconds", - skips: duration, - quest: "/quest/m1/ffi-export/lossless.md", - }, - Known { - language: "dart", - what: "a Duration's nanoseconds", - skips: duration, - quest: "/quest/m1/ffi-export/lossless.md", - }, - Known { - language: "kotlin", - what: "a Duration's nanoseconds past 146 years", - skips: duration, - quest: "/quest/m1/ffi-export/lossless.md", - }, - Known { - language: "kotlin", - what: "an IPv6 flow label", - skips: ipv6, - quest: "/quest/m1/ffi-export/lossless.md", - }, - Known { - language: "go", - what: "an IPv6 flow label", - skips: ipv6, - quest: "/quest/m1/ffi-export/lossless.md", - }, -]; +const KNOWN: &[Known] = &[]; /// A function the C header declares: each parameter's type and name. struct Proto { @@ -1436,24 +1394,23 @@ fn go(built: &Built, calls: &[Call], source: &str, rt: &str) -> String { ) } -/// The Dart driver; Dart's `int` is Rust's `u64` in two's complement, printed unsigned. +/// The Dart driver, which prints each digest, a `U64`, unsigned. fn dart(built: &Built, calls: &[Call]) -> String { let name = built.name(); let mut body = String::new(); for call in calls { + // A seed as the `int` of its bits. + let u = |seed: u64| format!("s.U64.fromBits({})", seed as i64); let value = match *call { - Call::Echo(n, seed) => format!("s.digest{n}(s.echo{n}(s.make{n}({seed})))"), - Call::Lent(n, seed) => format!("s.lent{n}({seed}, s.digest{n})"), - Call::Peek(n, seed) => format!("s.peek{n}({seed}, s.digest{n})"), - Call::Made(n, seed) => format!("s.made{n}({seed}, s.make{n})"), - Call::Later(n, seed) => format!("s.digest{n}(await s.later{n}(s.make{n}({seed})))"), + Call::Echo(n, seed) => format!("s.digest{n}(s.echo{n}(s.make{n}({})))", u(seed)), + Call::Lent(n, seed) => format!("s.lent{n}({}, s.digest{n})", u(seed)), + Call::Peek(n, seed) => format!("s.peek{n}({}, s.digest{n})", u(seed)), + Call::Made(n, seed) => format!("s.made{n}({}, s.make{n})", u(seed)), + Call::Later(n, seed) => format!("s.digest{n}(await s.later{n}(s.make{n}({})))", u(seed)), }; - let _ = writeln!(body, " print('{} ${{u({value})}}');", call.label()); + let _ = writeln!(body, " print('{} ${{({value}).toRadixString(10)}}');", call.label()); } - format!( - "import 'package:{name}/{name}.dart' as s;\n\nString u(int v) => BigInt.from(v).toUnsigned(64).toString();\n\n\ - Future main() async {{\n{body}}}\n" - ) + format!("import 'package:{name}/{name}.dart' as s;\n\nFuture main() async {{\n{body}}}\n") } /// Build and run `language`'s driver of `built`, checking it against the native transcript. diff --git a/ffi/tests/miri/api.rs b/ffi/tests/miri/api.rs index 9449d5de6..1d9bfebc3 100644 --- a/ffi/tests/miri/api.rs +++ b/ffi/tests/miri/api.rs @@ -212,3 +212,19 @@ fn foreign_futures() { assert!(dropped.freed && dropped.waker.is_none()); } } + +/// WTF-8, as a Windows path crosses: a surrogate alone is its own unit, and a lead and +/// trail written apart, which WTF-8 writes as the scalar they make, are refused. +#[test] +fn wtf8() { + let units = |s: &str| s.encode_utf16().collect::>(); + assert_eq!(rt::wtf8("aé😀".as_bytes()), Some(units("aé😀"))); + assert_eq!(rt::wtf8(&[0xED, 0xA0, 0x80]), Some(vec![0xD800])); + assert_eq!(rt::wtf8(&[b'a', 0xED, 0xB0, 0x80, b'b']), Some(vec![0x61, 0xDC00, 0x62])); + assert_eq!(rt::wtf8(&[0xED, 0xB0, 0x80, 0xED, 0xA0, 0x80]), Some(vec![0xDC00, 0xD800])); + assert_eq!(rt::wtf8(&[0xED, 0xA0, 0x80, b'x', 0xED, 0xB0, 0x80]), Some(vec![0xD800, 0x78, 0xDC00])); + assert_eq!(rt::wtf8(&[0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80]), None); + assert_eq!(rt::wtf8(&[0xFF]), None); + assert_eq!(rt::wtf8(&[0xED, 0xA0]), None); + assert_eq!(rt::wtf8(&[b'a', 0xC3]), None); +} diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index ed2be7b06..0914d9c5f 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -29,7 +29,7 @@ Decided with the maintainer on 2026-11-09, in the quest audit: - ✅ [Sessions on sans-IO QUIC](/quest/m3/session-quic.md) left the epic for m3, since it waits on moq's m3 driver and would hold the epic open. Rejected: m2; staying in m1. - ✅ A binding's types are lossless, with checked conversions to and from the idiomatic - ones ([lossless](/quest/m1/ffi-export/lossless.md)). Rejected: idiomatic types whose + ones ([#832](https://github.com/kixel-ai/rust2/pull/832)). Rejected: idiomatic types whose truncations and aborts become errors; today's mappings. - ✅ A failed run-time check raises the language's own error, and a Rust panic aborts ([#730](https://github.com/kixel-ai/rust2/pull/730)). Rejected: catching panics at the diff --git a/quest/m1/ffi-export/lossless.md b/quest/m1/ffi-export/lossless.md deleted file mode 100644 index 16a6f4664..000000000 --- a/quest/m1/ffi-export/lossless.md +++ /dev/null @@ -1,54 +0,0 @@ -# [L] Lossless binding types - -## Goal - -Every type a [rust2-ffi](/ffi/README.md) binding takes or returns holds every value of its -Rust type exactly: a value passed in and returned is the same value, and no conversion -truncates, clamps, panics, traps, or aborts. A native type stands in only where it is -exact; otherwise the runtime's own type crosses, with checked conversions to and from the -idiomatic one that fail with the language's error. Proven in every language's `api` test -by the edge values of each type crossing both ways, and by deleting this quest's `KNOWN` -entries in [the shape tests](/ffi/tests/fixtures/shapes.rs). - -## Plan - -Decided with the maintainer on 2026-11-09, in the quest audit: ✅ lossless always, with -helpers that convert to and from the idiomatic type, checked. Rejected: idiomatic types -whose silent losses become errors, which still fail at run time on values that do not fit; -today's mappings. AGENTS.md carries the rule. - -What the audit found, each a place native Rust would have gone on: - -- `Duration`: Python's `timedelta` and Dart's `Duration` truncate to the microsecond and - clamp (`ffi/runtime/python.rs`, `rt.dart`); Go's `time.Duration` panics on a negative - one or past 292 years (`ffi_cgo.go`); Swift traps on a negative `Swift.Duration` or one - past `Duration::MAX` (`rt.swift`); Kotlin's maps past `i64` seconds to `INFINITE` - and back to `i64::MAX` seconds (`rt.kt`), and past about 146 years, where - `kotlin.time.Duration` holds milliseconds, drops what is below one. Seconds and nanoseconds, as std holds them, - cross exactly. moq's switch also asks that Go stop panicking on a negative one and that - a duration field default to zero - ([moq's quest](https://github.com/moq-dev/moq/blob/main/quest/m3/ffi-exporter.md)). -- Socket addresses: Kotlin throws and Go panics on an IPv6 flow label, and Go on a zone it - cannot look up; `SocketAddrV4` given an IPv6 address aborts in C and C++, panics in Go, - and throws in Kotlin and Dart. The runtime's own `SocketAddr`, as Swift and Dart have, - and a C struct per family, hold each exactly. -- Paths: one that is not UTF-8 throws in Kotlin, traps in Swift, and aborts in Dart; Go - and Python already keep its bytes. -- Text: a Go `string` or C++ view that is not UTF-8 aborts the process (`rt.rs`), and an - invalid `char` aborts from Kotlin's `Int`, Go's `rune`, and C++'s `char32_t`. A value - the language's own type allows and Rust's does not is checked in the binding, which - raises. -- Integers: a Dart `u64` past `i64` reads negative; a Kotlin `usize` past a 32-bit - target's aborts (`ffi/runtime/jni.rs`'s `narrow`) since - [#689](https://github.com/kixel-ai/rust2/pull/689); and a Go `*big.Int` outside `u128` - panics (`ffi_cgo.go`). -- `Hash`: Dart's `hashCode` throws on a `PartialEq` type and Kotlin's returns 0; the IR - carries no `Hash`. Export it where the type implements it. - -Each mapping reports its cost on [the binding benchmark](/bench/ffi/README.md); none -should add a crossing or an allocation, and several remove one. Where a language's range -is the limit and no exact type exists at a call's speed, such as a Python `int` past -`u64` raising `OverflowError`, the raise stays: it rejects a value Rust's type cannot hold. - -Public API: every generated API's types for durations, addresses, paths, and text -(breaking), and the runtimes' conversion helpers. diff --git a/quest/m1/ffi-export/ship.md b/quest/m1/ffi-export/ship.md index ac3abac36..fc7012032 100644 --- a/quest/m1/ffi-export/ship.md +++ b/quest/m1/ffi-export/ship.md @@ -20,5 +20,4 @@ then see change. A sibling planned later adds itself to `Required` here. ## Required - [Refusals](/quest/m1/ffi-refusals/README.md) - the census lists no refusals at the moq pin, and `ffi.toml` is gone -- [Lossless binding types](/quest/m1/ffi-export/lossless.md) - the types of durations, addresses, paths, and text - [Not Send](/quest/m1/ffi-export/not-send.md) - the types and futures that export checked by thread diff --git a/quest/m2/wasm/backend.md b/quest/m2/wasm/backend.md index a02e43158..7dd4236c7 100644 --- a/quest/m2/wasm/backend.md +++ b/quest/m2/wasm/backend.md @@ -102,7 +102,3 @@ Open, for the maintainer once the implementer has the facts: Recommended: refuse it at the import's span until a consumer needs both. Public API: the TypeScript binding's shape, and the name of the web target. - -## Required - -- [Lossless binding types](/quest/m1/ffi-export/lossless.md) - the types a new binding copies are the exact ones