From 016e0acfff7561dc945e3ad4a514c684164b560e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:38:11 -0800 Subject: [PATCH 01/28] chore(quest): claim quest/m1/ffi-refusals/ip-addr.md Quest-Claim: ec915abe9d447176 From d6cca4a86ce7610f915777a936592a6b2a0f2e42 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:35:16 -0800 Subject: [PATCH 02/28] feat(ffi): IpAddr, Ipv4Addr, and Ipv6Addr cross by family Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 31 +++++++++++-- ffi/fixtures/api/src/lib.rs | 17 +++++++ ffi/moq/ffi.toml | 1 - ffi/runtime/ffi_cgo.go | 45 +++++++++++++++++++ ffi/runtime/jni.rs | 14 ++++++ ffi/runtime/python.rs | 46 ++++++++++++++++++- ffi/runtime/rt.dart | 59 ++++++++++++++++++------- ffi/runtime/rt.kt | 43 +++++++++++++++--- ffi/runtime/rt.rs | 52 ++++++++++++++++++++++ ffi/runtime/rt.swift | 39 +++++++++++++--- ffi/src/abi.rs | 5 +++ ffi/src/c.rs | 4 ++ ffi/src/collect.rs | 18 ++++++-- ffi/src/cpp.rs | 22 ++++++--- ffi/src/dart.rs | 18 ++++++-- ffi/src/glue.rs | 11 +++-- ffi/src/go.rs | 9 +++- ffi/src/ir.rs | 6 ++- ffi/src/kotlin.rs | 14 ++++-- ffi/src/python.rs | 19 +++++++- ffi/src/swift.rs | 12 +++-- ffi/tests/api.c | 19 ++++++++ ffi/tests/api.cpp | 21 +++++++++ ffi/tests/api.dart | 11 +++++ ffi/tests/api.go | 10 +++++ ffi/tests/api.kt | 14 ++++++ ffi/tests/api.py | 11 +++++ ffi/tests/api.swift | 8 ++++ ffi/tests/fixtures.rs | 14 ++++++ ffi/tests/fixtures/shapes.rs | 32 +++++++++++--- 30 files changed, 557 insertions(+), 68 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 7d0a4dc1c..1760742f5 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -217,13 +217,12 @@ fn main() { scaled((1 << 64) + 1, 1 << 63), scaled(1 << 127, 2) ); - let hex = |sum: [u8; 4]| sum.iter().map(|b| format!("{b:02x}")).collect::(); let mut digest = api::Digest::new(); digest.sum = api::fingerprint("deadbeef").unwrap(); println!( "digest {} {} {}", - hex(digest.sum), - hex(digest.flipped([0xff, 0, 0xff, 0])), + hex(&digest.sum), + hex(&digest.flipped([0xff, 0, 0xff, 0])), api::fingerprint("zz").unwrap_err() ); let mut batch: api::Batch = api::Batch::new(); @@ -859,6 +858,21 @@ fn main() { println!("numbered {}", numbered.join(", ")); let (v4, v6) = api::nexts("127.0.0.1:8080".parse().unwrap(), "[::1%3]:443".parse().unwrap()); println!("nexts {} {} {} {}", v4.ip(), v4.port(), v6.port(), v6.scope_id()); + // IP addresses of each family, printed as their bytes in hex, so their length shows their + // family: an IPv4-mapped IPv6 stays IPv6 through each binding, and comes back as IPv4 + // only when Rust flips it. + let (v4, v6) = api::next_ips("10.0.0.255".parse().unwrap(), "::ffff".parse().unwrap()); + let mapped = api::flip(std::net::Ipv4Addr::new(127, 0, 0, 1).into()); + let back = api::flip(mapped); + let other = api::flip(std::net::Ipv6Addr::LOCALHOST.into()); + println!( + "ips {} {} {} {} {}", + hex(&v4.octets()), + hex(&v6.octets()), + ip_hex(mapped), + ip_hex(back), + ip_hex(other) + ); // Standard and common types: serde_json's map iterator, std's instant, and tracing's // level and its filter. @@ -961,6 +975,17 @@ fn main() { } /// Print each of `numbers`' lists, its floats as their bits. +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} + +fn ip_hex(ip: std::net::IpAddr) -> String { + match ip { + std::net::IpAddr::V4(v4) => hex(&v4.octets()), + std::net::IpAddr::V6(v6) => hex(&v6.octets()), + } +} + fn numbers(what: &str, numbers: &api::Numbers) { println!("{what} u16 {:?}", numbers.u16s); println!("{what} u32 {:?}", numbers.u32s); diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index b3e7f593a..7dcc8f464 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -3286,6 +3286,23 @@ pub fn nexts( (std::net::SocketAddrV4::new(*v4.ip(), v4.port() + 1), v6) } +/// Each address one past, wrapping, one IPv4 and one IPv6. +pub fn next_ips(v4: std::net::Ipv4Addr, v6: std::net::Ipv6Addr) -> (std::net::Ipv4Addr, std::net::Ipv6Addr) { + ( + u32::from(v4).wrapping_add(1).into(), + u128::from(v6).wrapping_add(1).into(), + ) +} + +/// The address in the other family: IPv4 as its IPv4-mapped IPv6, and an IPv4-mapped IPv6 +/// as its IPv4; any other IPv6 stays as it is. +pub fn flip(ip: std::net::IpAddr) -> std::net::IpAddr { + match ip { + std::net::IpAddr::V4(v4) => v4.to_ipv6_mapped().into(), + std::net::IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(ip, Into::into), + } +} + /// Why [`refuse`] refused, which Rust returns only boxed. #[derive(Debug)] pub struct Refusal { diff --git a/ffi/moq/ffi.toml b/ffi/moq/ffi.toml index 783c59cfb..e23b44fb7 100644 --- a/ffi/moq/ffi.toml +++ b/ffi/moq/ffi.toml @@ -13,7 +13,6 @@ exclude = [ "moq_quic::VarInt::from_u64_unchecked", "moq_tokio::accept::Failure::classify", "moq_tokio::accept::Health::failed", - "moq_tokio::quic::demux::Datagram::local", "moq_tokio::quic::driver::connection::Connection::congestion_state", "moq_tokio::quic::driver::connection::Connection::poll_closed", "moq_tokio::quic::driver::recv::Stream::read", diff --git a/ffi/runtime/ffi_cgo.go b/ffi/runtime/ffi_cgo.go index 44dc0022a..ab13cefa5 100644 --- a/ffi/runtime/ffi_cgo.go +++ b/ffi/runtime/ffi_cgo.go @@ -222,6 +222,51 @@ func liftAddr_(c cAddr) netip.AddrPort { return netip.AddrPortFrom(ip, uint16(c.port)) } +// 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") + } + if ip.Zone() != "" { + panic("an IPv6 address with a zone has no Rust IpAddr") + } + 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 +} + +// 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") + } + return lowerIp_(ip) +} + +// 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") + } + return lowerIp_(ip) +} + +// 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])) + } + return netip.AddrFrom16([16]byte(b)) +} + // liftList_ is the array Rust returned, each item lifted by lift, which takes what it // holds. func liftList_[CT, T any](ptr *CT, n C.size_t, lift func(CT) T) []T { diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index 9702cc561..4ca36b1ae 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -1347,6 +1347,20 @@ impl Wire for rt::SocketAddr { } } +impl Wire for rt::IpAddr { + fn read(r: &mut Reader<'_>) -> Self { + Self { + ip: std::array::from_fn(|_| r.get()), + v6: r.get(), + } + } + + fn write(self, w: &mut Writer) { + self.ip.into_iter().for_each(|b| w.put(b)); + w.put(self.v6); + } +} + impl Wire for rt::List { /// A view of the elements, as their length, or -1 for none, then each element, which /// the reader keeps. diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 02be9650e..149c8d405 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -457,12 +457,14 @@ 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, -/// `Waker`, and `_Pollable`, which runs an awaitable Rust polls. +/// `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_RESUME: usize = 2; const T_WAKER: usize = 3; const T_POLLABLE: usize = 4; +const T_IPV4: usize = 5; +const T_IPV6: usize = 6; /// The class registered at `index`. pub fn ty(index: usize) -> *mut PyObject { @@ -1211,6 +1213,48 @@ pub fn addr_out(addr: rt::SocketAddr) -> R { } } +static PACKED: Key = Key::new(c"packed"); +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 { + 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"), + (true, _) => false, + (_, true) => true, + _ if family == Family::V4 => return expected(obj, T_IPV4), + _ if family == Family::V6 => return expected(obj, T_IPV6), + _ => return expected_named(obj, "an IPv4Address or IPv6Address"), + }; + let mut s = Scope::default(); + if v6 && !is_none(attr(obj, &SCOPE_ID, &mut s)?) { + 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 }) +} + +/// An `ipaddress.IPv4Address` or `IPv6Address`. +pub fn ip_out(ip: rt::IpAddr) -> R { + let (class, packed) = match ip.lift("python") { + std::net::IpAddr::V4(v4) => (T_IPV4, v4.octets().to_vec()), + std::net::IpAddr::V6(v6) => (T_IPV6, v6.octets().to_vec()), + }; + // SAFETY: the GIL is held. + let packed = unsafe { Obj::new(PyBytes_FromStringAndSize(packed.as_ptr().cast(), packed.len() as Py_ssize_t)) }; + call_py(ty(class), vec![packed]) +} + static START: Key = Key::new(c"start"); static STOP: Key = Key::new(c"stop"); static STEP: Key = Key::new(c"step"); diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 19aee9769..56413c785 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -201,6 +201,13 @@ final class _Addr extends ffi.Struct { external int scope_id; } +final class _IpAddr extends ffi.Struct { + @ffi.Array(16) + external ffi.Array ip; + @ffi.Bool() + external bool v6; +} + final class _Waker extends ffi.Struct { external ffi.Pointer wake; external ffi.Pointer drop; @@ -920,33 +927,51 @@ final class _Native { return Duration(microseconds: secs * 1000000 + c.nanos ~/ 1000); } - void addr(_Addr dst, SocketAddr value, String family) { - final ip = value.address.rawAddress; - final v6 = value.address.type == io.InternetAddressType.IPv6; - if (family == 'V4' && v6 || family == 'V6' && !v6) { - throw ArgumentError.value( - value, - 'addr', - 'an IP${v6 ? 'v6' : 'v4'} address has no Rust SocketAddr$family', - ); + /// 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.ip[i] = i < ip.length ? ip[i] : 0; + 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.v6 = v6; dst.flowinfo = u32(value.flowinfo); dst.scope_id = u32(value.scopeId); } SocketAddr addrOf(_Addr c) { - final n = c.v6 ? 16 : 4; - final ip = typed.Uint8List(n); - for (var i = 0; i < n; i++) { - ip[i] = c.ip[i]; - } return SocketAddr( - io.InternetAddress.fromRawAddress(ip), + _ipOf(c.ip, c.v6), c.port, flowinfo: c.flowinfo, scopeId: c.scope_id, diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index 21b9751b3..4a878d028 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -169,6 +169,28 @@ class __Writer { socketAddr(v) } + /** 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) + } + + /** 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) + } + + /** 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) + } + /** 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" } @@ -307,13 +329,20 @@ class __Reader(private val b: java.nio.ByteBuffer) { val flowinfo = b.getInt() val scope = b.getInt() kotlin.check(flowinfo == 0) { "an IPv6 flow label has no Java form" } - // Built as Inet6Address directly, as InetAddress would read an IPv4-mapped one as IPv4. - val address = when { - !v6 -> java.net.InetAddress.getByAddress(ip.copyOf(4)) - scope == 0 -> java.net.Inet6Address.getByAddress(null, ip, null as java.net.NetworkInterface?) - else -> java.net.Inet6Address.getByAddress(null, ip, scope) - } - return java.net.InetSocketAddress(address, port) + return java.net.InetSocketAddress(address(ip, v6, scope), port) + } + + fun ipAddr(): java.net.InetAddress { + val ip = ByteArray(16) + b.get(ip) + return address(ip, bool(), 0) + } + + // 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 u128(): java.math.BigInteger { diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 61cf66588..d5688a9c5 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -560,6 +560,58 @@ impl SocketAddr { } } +/// `std::net::IpAddr`: an IPv4 address in the first 4 bytes of `ip`, with the rest zero, or +/// an IPv6 address, in network order. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct IpAddr { + pub ip: [u8; 16], + pub v6: bool, +} + +impl IpAddr { + pub fn lift(self, what: &str) -> std::net::IpAddr { + if self.v6 { + return std::net::Ipv6Addr::from(self.ip).into(); + } + let [a, b, c, d, rest @ ..] = self.ip; + if rest != [0; 12] { + fail(what, "an IPv4 address has IPv6 bytes"); + } + std::net::Ipv4Addr::new(a, b, c, d).into() + } + + /// 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"), + } + } + + /// 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"), + } + } + + 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, + }, + } + } +} + /// `Vec` across the ABI, as an array of its elements' C values. Passed in, a view Rust /// copies each element out of and never frees. Returned, an array the caller frees once, /// with what its first `len` elements hold. A null `ptr` is `None`. diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 02a8ae7d4..84b39b07e 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -21,6 +21,16 @@ 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] + + public init(ip: [UInt8]) { + self.ip = ip + } +} + /// Bytes as Rust holds them: Rust's stay in the buffer it returned, shared without a copy /// by every `Bytes` and slice reading them and passed back to Rust uncopied, and Swift's are /// a `Data`. Rust's are never lent as a `Data`, so nothing writes into the buffer Rust shares. @@ -456,17 +466,20 @@ extension Native { U128(high: value.hi, low: value.lo) } - static func socketAddr(_ addr: SocketAddr) -> CAddr { - let v6: Bool - switch addr.ip.count { - case 4: v6 = false - case 16: v6 = true - default: fatalError("an IP address is 4 bytes or 16, not \(addr.ip.count)") + /// 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 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.v6 = v6 c.flowinfo = addr.flowinfo c.scope_id = addr.scopeId return c @@ -478,6 +491,18 @@ extension Native { return SocketAddr(ip: ip, port: c.port, flowinfo: c.flowinfo, scopeId: c.scope_id) } + static func ipAddr(_ addr: IpAddr) -> CIpAddr { + var c = CIpAddr() + c.v6 = v6(addr.ip) + withUnsafeMutableBytes(of: &c.ip) { ip in ip.copyBytes(from: 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)) }) + } + /// The C array Rust returned, as its items each lifted by `lift`, which takes what /// each holds; `free` then frees the array alone. static func list(_ ptr: UnsafePointer?, _ len: Int, _ lift: (C) throws -> T) rethrows -> [T] { diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 0da323c3c..06d8b151c 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -69,6 +69,8 @@ pub enum Ty { Duration, /// `_socket_addr` SocketAddr, + /// `_ip_addr` + IpAddr, /// 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`. @@ -623,6 +625,7 @@ impl<'a> Abi<'a> { 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::Option(inner) => format!("option_{}", self.key(inner)), Type::List(_, inner) => format!("vec_{}", self.key(inner)), Type::Tuple(elements) => strip(self.tuple(elements)), @@ -684,6 +687,7 @@ impl<'a> Abi<'a> { Type::Duration => Ty::Duration, Type::Array(n) => Ty::Struct(self.array(*n)), Type::SocketAddr(_) => Ty::SocketAddr, + Type::IpAddr(_) => Ty::IpAddr, Type::Waker => Ty::Waker, Type::Future => Ty::ForeignFuture, Type::Reader => Ty::ForeignReader, @@ -1000,6 +1004,7 @@ impl<'a> Abi<'a> { "u128", "duration", "socket_addr", + "ip_addr", "waker", "foreign_future", "foreign_reader", diff --git a/ffi/src/c.rs b/ffi/src/c.rs index ea74e0601..a24f60f2c 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -90,6 +90,9 @@ impl Header<'_> { 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" + )); 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" )); @@ -521,6 +524,7 @@ pub fn decl(abi: &Abi<'_>, ty: &Ty, name: &str) -> String { 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::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 f1a3dcd9e..da78b2d64 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1007,9 +1007,8 @@ impl<'tcx> Collector<'tcx> { } /// Whether `did` is one of the standard library's types that crosses by its role rather - /// than in a handle, such as `String` or `SocketAddr`, or that will: `IpAddr` and its - /// families, which stay refused until they cross as `SocketAddr` does. A collection's - /// allocator and hasher are part of the collection's role. + /// than in a handle, such as `String` or `SocketAddr`. A collection's allocator and + /// hasher are part of the collection's role. fn by_role(&self, did: DefId) -> bool { const ROLES: [&str; 14] = [ "String", @@ -5332,6 +5331,16 @@ impl<'tcx> Collector<'tcx> { .flatten() { Type::SocketAddr(family) + } else if let Some(family) = (tcx.lang_items().sized_trait().map(|d| d.krate) == Some(did.krate)) + .then(|| match tcx.item_name(did).as_str() { + "IpAddr" => Some(Family::Any), + "Ipv4Addr" => Some(Family::V4), + "Ipv6Addr" => Some(Family::V6), + _ => None, + }) + .flatten() + { + Type::IpAddr(family) } else if let Some(&(_, i)) = (self.foreign.iter()).find(|&&(ty, _)| ty == did && !self.opaque(did)) { // A value holds only values. match pos { @@ -5602,7 +5611,8 @@ fn foreign_arg(ty: &Type) -> bool { | Type::F64 | Type::Char | Type::Duration - | Type::SocketAddr(_) => true, + | Type::SocketAddr(_) + | Type::IpAddr(_) => true, Type::String | Type::Vec | Type::Bytes | Type::PathBuf => true, Type::Ref(Mutability::Not, inner) => matches!(**inner, Type::Str | Type::Slice | Type::Path), Type::Option(inner) => foreign_arg(inner), diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 76b63be10..5708d4239 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -173,6 +173,7 @@ 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::List(_, inner) => format!("::std::vector<{}>", self.owned(inner)), Type::Waker => format!("::{}::waker", self.ns), Type::Option(inner) => format!("::std::optional<{}>", self.owned(inner)), @@ -348,9 +349,13 @@ impl<'a> Header<'a> { Type::Unit => unreachable!("no value is unit"), Type::Bool | Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::F64 => e.to_string(), Type::Char => format!("static_cast({e})"), - Type::U128 | Type::Duration | Type::SocketAddr(_) | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { - e.to_string() - } + Type::U128 + | Type::Duration + | Type::SocketAddr(_) + | Type::IpAddr(_) + | Type::Range(_) + | Type::Bound(_) + | Type::Bounds(_) => e.to_string(), Type::Waker => format!("{d}::give({d}::access::raw({e}))"), Type::List(_, inner) if ty.block().is_some() => format!("{d}::block<::{}>({e})", abi.list(inner)), Type::List(_, inner) => { @@ -414,9 +419,13 @@ impl<'a> Header<'a> { match ty { Type::Bool | Type::Int(_) | Type::NonZero(_) | Type::F32 | Type::F64 => e.to_string(), Type::Char => format!("static_cast({e})"), - Type::U128 | Type::Duration | Type::SocketAddr(_) | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { - e.to_string() - } + Type::U128 + | Type::Duration + | Type::SocketAddr(_) + | Type::IpAddr(_) + | Type::Range(_) + | Type::Bound(_) + | Type::Bounds(_) => e.to_string(), Type::List(_, inner) if ty.block().is_some() => format!( "{d}::lift_block<{}>({e}, &::{}_free)", self.owned(inner), @@ -546,6 +555,7 @@ impl<'a> Header<'a> { ("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")), ]; if abi.ranges.values().any(|ty| matches!(ty, Type::Bound(_))) { aliases.push(("bound".to_string(), abi.rt("bound"))); diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 7f3dbb81c..f8516cb48 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -540,6 +540,7 @@ impl<'a> Gen<'a> { Type::U128 => "BigInt".to_string(), Type::Duration => "Duration".to_string(), Type::SocketAddr(_) => "SocketAddr".to_string(), + Type::IpAddr(_) => "io.InternetAddress".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(), @@ -620,6 +621,7 @@ impl<'a> Gen<'a> { | Ty::U128 | Ty::Duration | Ty::SocketAddr + | Ty::IpAddr | Ty::Struct(_) | Ty::Waker | Ty::ForeignFuture @@ -635,6 +637,7 @@ impl<'a> Gen<'a> { 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::Waker => return "_Waker".to_string(), Ty::ForeignFuture => return "_ForeignFuture".to_string(), Ty::ForeignReader => return "_ForeignReader".to_string(), @@ -923,12 +926,20 @@ impl<'a> Gen<'a> { Type::Array(_) => format!("{e}._write({dst});"), Type::SocketAddr(family) => { let family = match family { - Family::Any => "", - Family::V4 => "V4", - Family::V6 => "V6", + 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}');") + } Type::Option(inner) => match inner.base() { Type::Str | Type::String | Type::Path | Type::PathBuf => { format!("a_.string({dst}, {e}, {take});") @@ -1249,6 +1260,7 @@ impl<'a> Gen<'a> { Type::Duration => format!("_n.durationOf({e})"), Type::Array(n) => format!("{}._of({e})", Abi::array_class(*n)), Type::SocketAddr(_) => format!("_n.addrOf({e})"), + Type::IpAddr(_) => format!("_n.ipOf({e})"), Type::Option(inner) => match inner.base() { Type::Object(_) => { let dt = self.dt(inner); diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index d2e02a13c..843185924 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -103,6 +103,7 @@ pub fn rs(ty: &Ty) -> 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::Struct(name) => name.clone(), // A C enum is an `int`. Ty::Enum(_) | Ty::Poll => "i32".to_string(), @@ -624,6 +625,9 @@ impl Gen<'_> { Type::SocketAddr(Family::Any) => "::std::net::SocketAddr".to_string(), Type::SocketAddr(Family::V4) => "::std::net::SocketAddrV4".to_string(), Type::SocketAddr(Family::V6) => "::std::net::SocketAddrV6".to_string(), + Type::IpAddr(Family::Any) => "::std::net::IpAddr".to_string(), + Type::IpAddr(Family::V4) => "::std::net::Ipv4Addr".to_string(), + Type::IpAddr(Family::V6) => "::std::net::Ipv6Addr".to_string(), Type::List(c, inner) => match (c, &**inner) { // A map's items are its key and value. (Collection::BTreeMap | Collection::JsonMap, Type::Tuple(pair)) => { @@ -678,9 +682,9 @@ 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) => format!("{e}.lift(w)"), - Type::SocketAddr(Family::V4) => format!("{e}.lift_v4(w)"), - Type::SocketAddr(Family::V6) => format!("{e}.lift_v6(w)"), + 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::Waker => format!("{e}.lift()"), Type::Future | Type::Reader => format!("{e}.lift(w)"), // A block is copied out whole. @@ -880,6 +884,7 @@ impl Gen<'_> { 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}))"), // 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}))") diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 1a804397e..6aafb8910 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -560,7 +560,7 @@ impl<'a> Gen<'a> { /*\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\ // 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)\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\tcAddr = C.{p}_socket_addr\n\tcIpAddr = C.{p}_ip_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\ @@ -714,6 +714,7 @@ impl<'a> Gen<'a> { 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::List(_, inner) => format!("[]{}", self.go(inner)), Type::Waker => "func()".to_string(), Type::Reader => "io.ReadSeeker".to_string(), @@ -785,6 +786,7 @@ impl<'a> Gen<'a> { Type::Record(_) | Type::Array(_) | Type::SocketAddr(_) + | Type::IpAddr(_) | Type::Range(_) | Type::Bound(_) | Type::Bounds(_) @@ -807,6 +809,7 @@ impl<'a> Gen<'a> { Ty::U128 => "cU128".to_string(), Ty::Duration => "cDuration".to_string(), Ty::SocketAddr => "cAddr".to_string(), + Ty::IpAddr => "cIpAddr".to_string(), Ty::Struct(n) | Ty::Enum(n) => format!("C.{n}"), Ty::Handle { name, .. } | Ty::Future(name) => format!("*C.{name}"), Ty::Waker => "cWaker".to_string(), @@ -896,6 +899,9 @@ impl<'a> Gen<'a> { 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::Waker => format!("waker_({e})"), Type::Reader | Type::Future => unreachable!("{ty:?} crosses only as a parameter"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { @@ -1037,6 +1043,7 @@ impl<'a> Gen<'a> { format!("lift_{}({e})", abi.array(*n)) } Type::SocketAddr(_) => format!("liftAddr_({e})"), + Type::IpAddr(_) => format!("liftIp_({e})"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => { self.convert(ty); format!("lift_{}({e})", abi.range(ty)) diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index 659f525a8..94d15aeea 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -544,6 +544,8 @@ pub enum Type { PathBuf, /// `std::net::SocketAddr`, or `SocketAddrV4` or `SocketAddrV6` for one family. SocketAddr(Family), + /// `std::net::IpAddr`, or `Ipv4Addr` or `Ipv6Addr` for one family. + IpAddr(Family), /// A collection of `T`, which C passes and returns as an array of its items: `Vec` of /// any `T` but `u8`, which is [`Type::Vec`], and behind a reference, `[T]` too; a boxed /// slice; a set; or a map, whose `T` is the tuple of a key and its value. @@ -621,10 +623,10 @@ pub enum Collection { IterRef, } -/// The addresses a socket address type holds. +/// The addresses a socket or IP address type holds. #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, serde::Serialize, serde::Deserialize)] pub enum Family { - /// IPv4 or IPv6: `SocketAddr`. + /// IPv4 or IPv6: `SocketAddr` or `IpAddr`. Any, V4, V6, diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 79d220b00..6bb1760b6 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -669,6 +669,7 @@ impl<'a> Gen<'a> { 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::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"), @@ -730,6 +731,9 @@ impl<'a> Gen<'a> { 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::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")); @@ -797,6 +801,7 @@ impl<'a> Gen<'a> { 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::List(..) if let Some(inner) = ty.block() => block(inner).read(r, &format!("{r}.i32()")), Type::List(_, inner) => { let item = self.read(inner, r, owned); @@ -1048,7 +1053,7 @@ impl<'a> Gen<'a> { _ => format!("a{p}"), }); } - Ty::U128 | Ty::Duration | Ty::SocketAddr | 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) { @@ -3021,7 +3026,7 @@ fn c_slots(abi: &Abi<'_>, 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::Struct(_) | Ty::In(_) => 2, + Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::IpAddr | Ty::Struct(_) | Ty::In(_) => 2, _ => 1, } } @@ -3169,7 +3174,9 @@ 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::Struct(_) | Ty::In(_) => ("jbyteArray", "ByteArray"), + Ty::U128 | Ty::Duration | Ty::SocketAddr | Ty::IpAddr | Ty::Struct(_) | Ty::In(_) => { + ("jbyteArray", "ByteArray") + } Ty::Fn(..) => ("jobject", "Any"), Ty::Waker => ("jobject", "Waker"), Ty::ForeignReader => ("jobject", "java.nio.channels.SeekableByteChannel"), @@ -3325,6 +3332,7 @@ pub(crate) fn rust_c(abi: &Abi<'_>, c: &Ty) -> 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::Struct(name) => format!("crate::{name}"), Ty::Handle { .. } => "*const std::ffi::c_void".to_string(), Ty::Out(inner) => format!("*mut {}", rust_c(abi, inner)), diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 392d94fe8..18a0dcff5 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -74,6 +74,7 @@ const TAKEN: &[&str] = &[ "_type", "_zip", "_datetime", + "_ipaddress", "_enum", "_socket", "_sys", @@ -100,8 +101,16 @@ 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`, -/// `T_RESUME`, `T_WAKER`, and `T_POLLABLE`. -const RUNTIME_TYPES: &[&str] = &["_Future", "_datetime.timedelta", "_resume", "Waker", "_Pollable"]; +/// `T_RESUME`, `T_WAKER`, `T_POLLABLE`, `T_IPV4`, and `T_IPV6`. +const RUNTIME_TYPES: &[&str] = &[ + "_Future", + "_datetime.timedelta", + "_resume", + "Waker", + "_Pollable", + "_ipaddress.IPv4Address", + "_ipaddress.IPv6Address", +]; /// The Python for `abi`, whose header's contract is `contract`, or the names two items /// claim alike. The Python's own contract hashes the header's with everything it and its @@ -176,6 +185,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result Gen<'a> { Family::V6 => V6.to_string(), Family::Any => format!("{V4} | {V6}"), }, + Type::IpAddr(Family::V4) => "_ipaddress.IPv4Address".to_string(), + Type::IpAddr(Family::V6) => "_ipaddress.IPv6Address".to_string(), + Type::IpAddr(Family::Any) => "_ipaddress.IPv4Address | _ipaddress.IPv6Address".to_string(), Type::List(_, inner) if ty.block().is_some() => format!("_array.array[{}]", self.py_ty(inner)), Type::List(_, inner) => format!("list[{}]", self.py_ty(inner)), Type::Waker => "Waker".to_string(), @@ -560,6 +573,7 @@ impl<'a> Gen<'a> { 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::List(_, inner) if ty.block().is_some() => format!("rt::block_in::<{}>({o}, s)", number(inner)), Type::List(..) => format!("r_{}({o}, s, {m}, {t})", self.converter(ty)), Type::Option(inner) => match inner.base() { @@ -638,6 +652,7 @@ impl<'a> Gen<'a> { 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::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() { diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 3b189473d..8a12b02ef 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -87,7 +87,8 @@ 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\ @@ -139,8 +140,8 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result_native`, `_bytes`, and `_socket_addr`. +/// runtime's own are the C ABI's: `Native`, `Bytes`, `SocketAddr`, and `IpAddr` are claimed +/// as `_native`, `_bytes`, `_socket_addr`, and `_ip_addr`. const TAKEN: &[&str] = &[ "Array", "Bool", @@ -154,6 +155,7 @@ const TAKEN: &[&str] = &[ "Hashable", "Int", "Int64", + "IpAddr", "Never", "OpaquePointer", "Optional", @@ -332,6 +334,7 @@ impl<'a> Gen<'a> { Type::U128 => "U128".to_string(), Type::Duration => "Swift.Duration".to_string(), Type::SocketAddr(_) => "SocketAddr".to_string(), + Type::IpAddr(_) => "IpAddr".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(), @@ -380,6 +383,7 @@ impl<'a> Gen<'a> { Ty::U128 => abi.rt("u128"), Ty::Duration => abi.rt("duration"), Ty::SocketAddr => abi.rt("socket_addr"), + Ty::IpAddr => abi.rt("ip_addr"), Ty::Struct(n) | Ty::Enum(n) => n.clone(), Ty::Handle { .. } | Ty::Future(_) => "OpaquePointer?".to_string(), Ty::Waker => abi.rt("waker"), @@ -438,6 +442,7 @@ impl<'a> Gen<'a> { Type::Array(_) => format!("{e}._c"), Type::Duration => format!("Native.duration({e})"), Type::SocketAddr(_) => format!("Native.socketAddr({e})"), + Type::IpAddr(_) => format!("Native.ipAddr({e})"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => format!("{e}._lower()"), Type::Waker => format!("Native.waker({e})"), Type::Future => format!("Native.future({e})"), @@ -523,6 +528,7 @@ impl<'a> Gen<'a> { 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::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() => { diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 3361f0e93..976a73615 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -1915,6 +1915,19 @@ static int script(void) { 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; + 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})}; + 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]); + } + } + 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")}}; @@ -2038,6 +2051,12 @@ 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}); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index e209729da..9eb9ba659 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -1248,6 +1248,27 @@ int script() { 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); + { + 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; + local6.v6 = true; + auto [nextip4, nextip6] = api::next_ips(ip4, ip6); + auto mapped = api::flip(local4); + std::printf("ips"); + 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]); + } + } + std::printf("\n"); + } // serde_json's map iterator, collected as pairs of a key and its document. { diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 884bdf35a..0adfa5200 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -752,6 +752,10 @@ Future script() async { 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(' ')}'); // 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'))]); @@ -1436,6 +1440,13 @@ void broken(String which) { 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(); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 4ed037925..254b27bd4 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -884,6 +884,9 @@ func script() { }), "[]")) 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()) // serde_json's map iterator, collected as pairs of a key and its document. sections := must(api.Sections([]rt.Tuple2[string, *serdejson.Json]{ @@ -1642,6 +1645,13 @@ func broken(which string) { 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) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 22150ebbc..d0ee80d74 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -666,6 +666,13 @@ fun script() { 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) } }}") // 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")))) @@ -1080,6 +1087,13 @@ fun broken(which: String) { 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. diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 9e9f1d092..9497ca357 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -10,6 +10,7 @@ import datetime import gc import io +import ipaddress import math import os import struct @@ -736,6 +737,10 @@ async def polls(): print("numbered " + ", ".join(f"{c.name()} {c.get()}" for c in numbered)) next4, next6 = api.nexts(v4, v6) print(f"nexts {next4[0]} {next4[1]} {next6[1]} {next6[3]}") + nextip4, nextip6 = api.next_ips(ipaddress.IPv4Address("10.0.0.255"), ipaddress.IPv6Address("::ffff")) + mapped = api.flip(ipaddress.IPv4Address("127.0.0.1")) + ips = [nextip4, nextip6, mapped, api.flip(mapped), api.flip(ipaddress.IPv6Address("::1"))] + print("ips " + " ".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"))]) @@ -1553,6 +1558,12 @@ def edit(counter): # An IPv6 address where Rust takes `SocketAddrV4`. case "family": api.nexts(("::", 1, 0, 0), ("::", 1, 0, 0)) + # An IPv6 address where Rust takes `Ipv4Addr`. + case "ip family": + api.next_ips(ipaddress.IPv6Address("::1"), ipaddress.IPv6Address("::1")) + # An IPv6 address with a scope, which Rust's `IpAddr` has no room for. + case "ip scope": + api.flip(ipaddress.IPv6Address("fe80::1%3")) # A future Rust polls, cancelled by its owner rather than by Rust. case "foreign cancelled": diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index e4e3bb5af..1c521c7e5 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -837,6 +837,10 @@ func script() { print("numbered \(listed(numbered) { "\($0.name()) \($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: " "))") // serde_json's map iterator, collected as pairs of a key and its document. var sectionPairs = UniqueArray() @@ -1371,6 +1375,10 @@ func broken(_ which: String) { 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": _ = stray() diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 1dac08c77..4ecbc41cf 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -231,6 +231,8 @@ fn api_c() { ("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"), @@ -859,6 +861,8 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { ("outlived", OUTLIVED), ("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. @@ -959,6 +963,11 @@ fn api_python() { ("foreign cancelled", "a future Rust polls raised"), ("outlived", OUTLIVED), ("family", "ValueError: an IPv6 address has no Rust SocketAddrV4"), + ("ip family", "ValueError: an IPv6 address has no Rust Ipv4Addr"), + ( + "ip scope", + "ValueError: an IPv6 address with a scope has no Rust IpAddr", + ), ("kept threw", "api_Spawned_new: a call into Python raised"), ("returned", "api_made: a call into Python raised"), ("reader kept", "read: a reader kept a view of its buffer past the read"), @@ -1141,6 +1150,7 @@ fn api_swift() { } 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"), @@ -1249,6 +1259,8 @@ fn api_dart() { ), ("outlived", OUTLIVED), ("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"), ] { @@ -1453,6 +1465,8 @@ fn api_go() { ("nil reader", "a nil io.ReadSeeker"), ("returned zero", "a zero Instant, 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"), diff --git a/ffi/tests/fixtures/shapes.rs b/ffi/tests/fixtures/shapes.rs index 28b9359b9..85e88bb16 100644 --- a/ffi/tests/fixtures/shapes.rs +++ b/ffi/tests/fixtures/shapes.rs @@ -36,6 +36,8 @@ enum Shape { PathBuf, /// `SocketAddr`, `SocketAddrV4`, or `SocketAddrV6`. SocketAddr(&'static str), + /// `IpAddr`, `Ipv4Addr`, or `Ipv6Addr`. + IpAddr(&'static str), Range(&'static str), Bound(&'static str), /// `serde_json::Value` @@ -80,6 +82,7 @@ const COVERED: &[(&str, &str)] = &[ ("Path", "PathBuf, which digest_N and peek_N's closure borrow as &Path"), ("PathBuf", "PathBuf"), ("SocketAddr", "SocketAddr, of each family"), + ("IpAddr", "IpAddr, of each family"), ("List", "Vec, Set, Map, and JsonMap"), ("Waker", "none: a waker holds no value"), ("Future", "none: a foreign future is of (), which holds no value"), @@ -102,7 +105,7 @@ const COVERED: &[(&str, &str)] = &[ ("Const", "none: a const argument, which no value has"), ]; -/// Every leaf, once for each integer type and socket address family. +/// Every leaf, once for each integer type and address family. fn leaves() -> Vec { let mut leaves = vec![Shape::Bool]; leaves.extend(INTS.map(Shape::Int)); @@ -122,6 +125,9 @@ fn leaves() -> Vec { Shape::SocketAddr("SocketAddr"), Shape::SocketAddr("SocketAddrV4"), Shape::SocketAddr("SocketAddrV6"), + Shape::IpAddr("IpAddr"), + Shape::IpAddr("Ipv4Addr"), + Shape::IpAddr("Ipv6Addr"), Shape::Range("u32"), Shape::Range("i64"), Shape::Bound("u8"), @@ -276,7 +282,9 @@ impl Emit { "std::path::PathBuf".into(), "std::path::PathBuf::from(r.string())".into(), ), - Shape::SocketAddr(family) => (format!("std::net::{family}"), format!("r.{}()", family.to_lowercase())), + Shape::SocketAddr(family) | Shape::IpAddr(family) => { + (format!("std::net::{family}"), format!("r.{}()", family.to_lowercase())) + } Shape::Range(int) => ( format!("std::ops::Range<{int}>"), format!("(r.int() as {int})..(r.int() as {int})"), @@ -444,12 +452,26 @@ impl Rng { } fn socketaddrv4(&mut self) -> std::net::SocketAddrV4 { - std::net::SocketAddrV4::new((self.next() as u32).into(), self.next() as u16) + std::net::SocketAddrV4::new(self.ipv4addr(), self.next() as u16) } fn socketaddrv6(&mut self) -> std::net::SocketAddrV6 { - let ip = ((self.next() as u128) << 64 | self.next() as u128).into(); - std::net::SocketAddrV6::new(ip, self.next() as u16, self.next() as u32, self.next() as u32) + std::net::SocketAddrV6::new(self.ipv6addr(), self.next() as u16, self.next() as u32, self.next() as u32) + } + + fn ipv4addr(&mut self) -> std::net::Ipv4Addr { + (self.next() as u32).into() + } + + fn ipv6addr(&mut self) -> std::net::Ipv6Addr { + ((self.next() as u128) << 64 | self.next() as u128).into() + } + + fn ipaddr(&mut self) -> std::net::IpAddr { + match self.below(2) { + 0 => self.ipv4addr().into(), + _ => self.ipv6addr().into(), + } } fn socketaddr(&mut self) -> std::net::SocketAddr { From 3073b7d562618b0172972759d5e86e91c9b16f4b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:03:32 -0800 Subject: [PATCH 03/28] docs(ffi): IpAddr's mapping in each binding, and its quest done Co-Authored-By: Claude Opus 5.5 --- .tmp-env.sh | 0 ffi/README.md | 21 +++++++++++++++------ quest/m1/ffi-refusals/README.md | 4 ++-- quest/m1/ffi-refusals/ip-addr.md | 29 ----------------------------- quest/m1/ffi-refusals/no-config.md | 1 - 5 files changed, 17 insertions(+), 38 deletions(-) create mode 100644 .tmp-env.sh delete mode 100644 quest/m1/ffi-refusals/ip-addr.md diff --git a/.tmp-env.sh b/.tmp-env.sh new file mode 100644 index 000000000..e69de29bb diff --git a/ffi/README.md b/ffi/README.md index 78a3ceef1..51458cc09 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -118,6 +118,7 @@ public path names, or an error taken by value, is held by value as an opaque han | `&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 | | `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 | @@ -446,8 +447,8 @@ packages place it in. converts with each item moved, so a returned one passes back even when its items hold a `Copy` object C++ cannot copy. One whose items also move a handle is taken as `taken`, a `std::vector` of them moved in as it is, or any other such - range moved in, each handle moved out of it. A socket address is a `socket_addr` and a - `u128` a `u128`, each the C struct. `Option` is `std::optional`, a tuple `std::tuple`, and `Result` a `result`, whose `value()` aborts on an + range moved in, each handle moved out of it. A socket address is a `socket_addr`, an IP + address an `ip_addr`, and a `u128` a `u128`, each the C struct. `Option` is `std::optional`, a tuple `std::tuple`, and `Result` a `result`, whose `value()` aborts on an error. A `[u8; N]` is a `std::array`. - **Traits** are abstract classes, passed as a `std::unique_ptr` that Rust deletes. - **Closures** are any callable, taken by a template parameter, which Rust calls from any @@ -548,7 +549,9 @@ out, with only its other fields encoded. - 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. A `Vec`, `&[T]`, or `Box<[T]>` of a + 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 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 @@ -644,6 +647,8 @@ caller imports it with no ignores. - A **path** is a `str`, or any path passed in, which crosses as `os.fsencode` encodes it, so one that is not UTF-8 keeps its bytes. A `SocketAddr` is the `socket` module's tuple, `(host, port)` or `(host, port, flowinfo, scope_id)`, of that family for `V4` or `V6`. + An `IpAddr` is an `ipaddress.IPv4Address` or `IPv6Address`, and an `Ipv4Addr` or + `Ipv6Addr` the one of its family; one passed in with a scope raises `ValueError`. A `Vec` or `&[T]` Rust returns is a `list`, and passed in is any iterable other than a `str`, annotated `Iterable`, or `Sequence` in a record, which keeps it. Passing a `Vec` by value closes each object in it. @@ -750,7 +755,8 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod `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. A `Vec`, `&[T]`, or + 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 `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. @@ -867,7 +873,8 @@ reads a go.mod. `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. A `Vec`, `&[T]`, or `Box<[T]>` is a `[]T`; one of a + `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 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 @@ -972,7 +979,9 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass 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`. + 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`. - **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`. diff --git a/quest/m1/ffi-refusals/README.md b/quest/m1/ffi-refusals/README.md index 15f9153be..22a5c4003 100644 --- a/quest/m1/ffi-refusals/README.md +++ b/quest/m1/ffi-refusals/README.md @@ -79,8 +79,8 @@ Decided with the maintainer on 2026-11-12, in the quest audit: `NonZero`, which exports, the rest of the errors at moq's main (exported since by rules for another crate's value, a borrowing future of a function that is not `async`, and an object field that is not `Clone`), and - what the excludes still hide at moq's main, exported since but for - [`IpAddr`](/quest/m1/ffi-refusals/ip-addr.md) and `server::Listener::accept_with`, which + what the excludes still hide at moq's main, exported since (`IpAddr` by its family, as + `SocketAddr` is) but for `server::Listener::accept_with`, which [not-send](/quest/m1/ffi-export/not-send.md) owns; `just ffi census` fails on an exclude that names what exports. The bump is then only the bump. The types refused for not being `Send` (`clock::Entered`, diff --git a/quest/m1/ffi-refusals/ip-addr.md b/quest/m1/ffi-refusals/ip-addr.md deleted file mode 100644 index 4237d86b0..000000000 --- a/quest/m1/ffi-refusals/ip-addr.md +++ /dev/null @@ -1,29 +0,0 @@ -# [S] IpAddr crosses by family - -## Goal - -`std::net::IpAddr`, `Ipv4Addr`, and `Ipv6Addr` export in every binding, as `SocketAddr` -already does: its IP's bytes and whether it is IPv6, with no port. moq-tokio's -`quic::demux::Datagram::local` is the one item at moq's `main` (`a11a584`, measured on -2026-11-14 in [#802](https://github.com/kixel-ai/rust2/pull/802)) refused for it, and the -census lists no `IpAddr` refusal there. - -## Plan - -Decided with the maintainer on 2026-11-13, in -[moq-excluded](https://github.com/kixel-ai/rust2/pull/802)'s Plan: ✅ `IpAddr` crosses as -`SocketAddr` already does, by family. #802 split it out, since it is a new IR type -through the glue and all seven generators, apart from that quest's collector rules. -The maintainer kept this quest on 2026-11-14, at #802's pick: ✅ keep (rejected: drop). - -- A new `_ip_addr` C struct of 16 bytes and an `is_v6` flag, `SocketAddr`'s - without the port, and each binding's own address type where it is exact, as each maps - `SocketAddr` today (Kotlin `InetAddress`, Python `ipaddress`, Swift, Go `netip.Addr`, - Dart `InternetAddress`, C++). Recommended: reuse each binding's `SocketAddr` mapping of - the IP for the address and drop the port, so one family check covers both. -- `Ipv4Addr` and `Ipv6Addr` cross as that struct too; one of the other family passed in - aborts, as `SocketAddrV4` does. -- Prove it in the api fixture's transcript in all seven languages, both families, in and - out. - -Public API: the generated API of each item that newly exports. diff --git a/quest/m1/ffi-refusals/no-config.md b/quest/m1/ffi-refusals/no-config.md index 9b9653380..5134f1bbe 100644 --- a/quest/m1/ffi-refusals/no-config.md +++ b/quest/m1/ffi-refusals/no-config.md @@ -30,5 +30,4 @@ that passes them). ## Required - [The moq pin bump](/quest/m1/ffi-refusals/moq-pin.md) - the pin is at moq's `main`, where the census is taken -- [`IpAddr`](/quest/m1/ffi-refusals/ip-addr.md) - exports `quic::demux::Datagram::local`, the last item moq's main refuses beyond not-send's - [Not Send](/quest/m1/ffi-export/not-send.md) - exports the three types the census still refuses for not being `Send` From 84edf9b10e6e0d49ff5d636433d63314c1d2566f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:05:52 -0800 Subject: [PATCH 04/28] docs(ffi): numbers' doc comment sits on fn numbers again Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 1760742f5..54b6cc5ac 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -974,11 +974,12 @@ fn main() { ); } -/// Print each of `numbers`' lists, its floats as their bits. +/// Lowercase hex of `bytes`, two digits each. fn hex(bytes: &[u8]) -> String { bytes.iter().map(|b| format!("{b:02x}")).collect() } +/// An address's bytes in hex, so the length shows its family. fn ip_hex(ip: std::net::IpAddr) -> String { match ip { std::net::IpAddr::V4(v4) => hex(&v4.octets()), @@ -986,6 +987,7 @@ fn ip_hex(ip: std::net::IpAddr) -> String { } } +/// Print each of `numbers`' lists, its floats as their bits. fn numbers(what: &str, numbers: &api::Numbers) { println!("{what} u16 {:?}", numbers.u16s); println!("{what} u32 {:?}", numbers.u32s); From a5402ed2ba44c71bbdae697418da687a1945e4db Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:51:06 -0800 Subject: [PATCH 05/28] chore(quest): claim quest/m1/ffi-export/lossless.md Quest-Claim: ad6d0b494bc98f5d From 3fb039441114d08e54490fa953eaeaed6f586366 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:27:31 -0800 Subject: [PATCH 06/28] feat(ffi)!: lossless core: a C struct per address family, Hash exported, WTF-8 paths on Windows The C ABI holds each address family in a struct of its own, so no C value of SocketAddrV4 or Ipv4Addr can hold another family's and no lift aborts; a type implementing Hash exports _hash, and C++ specializes std::hash; Windows paths cross as WTF-8 both ways. The api fixture gains the lossless round trips and a hashed Key, C and C++ print them, and the shapes sample every Duration and non-UTF-8 paths, with the quest's KNOWN entries deleted. The other bindings follow in later commits. Co-Authored-By: Claude Opus 5.5 --- .tmp-env.sh | 0 ffi/fixtures/api/examples/transcript.rs | 83 ++++++ ffi/fixtures/api/src/lib.rs | 82 ++++++ ffi/runtime/jni.rs | 78 +++++- ffi/runtime/python.rs | 95 +++++-- ffi/runtime/rt.rs | 344 ++++++++++++++++-------- ffi/src/abi.rs | 38 ++- ffi/src/c.rs | 15 +- ffi/src/collect.rs | 8 +- ffi/src/cpp.rs | 34 ++- ffi/src/dart.rs | 8 +- ffi/src/glue.rs | 42 ++- ffi/src/go.rs | 4 +- ffi/src/ir.rs | 4 + ffi/src/kotlin.rs | 9 +- ffi/src/lock.rs | 1 + ffi/src/python.rs | 21 +- ffi/src/swift.rs | 4 +- ffi/tests/api.c | 143 ++++++++-- ffi/tests/api.cpp | 150 +++++++++-- ffi/tests/fixtures.rs | 4 - ffi/tests/fixtures/shapes.rs | 86 ++---- 22 files changed, 926 insertions(+), 327 deletions(-) delete mode 100644 .tmp-env.sh diff --git a/.tmp-env.sh b/.tmp-env.sh deleted file mode 100644 index e69de29bb..000000000 diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 9b80554a8..8b3b29805 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -887,6 +887,74 @@ 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/é"), api::stray()] + .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( @@ -997,6 +1065,21 @@ fn hex(bytes: &[u8]) -> String { } /// An address's bytes in hex, so the length shows its family. +/// 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() + ), + } +} + fn ip_hex(ip: std::net::IpAddr) -> String { match ip { std::net::IpAddr::V4(v4) => hex(&v4.octets()), diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 007a229ba..48372e8aa 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -4012,3 +4012,85 @@ 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/runtime/jni.rs b/ffi/runtime/jni.rs index ca6684bc1..4dc708d2b 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -1327,37 +1327,95 @@ impl Wire for rt::Duration { } } -impl Wire for rt::SocketAddr { +impl Wire for rt::Ipv4Addr { + 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)); + } +} + +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: std::array::from_fn(|_| r.get()), + 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..76eb18a37 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -1165,7 +1165,7 @@ pub fn duration_out(duration: rt::Duration) -> R { /// Which addresses a socket address type holds. #[derive(Clone, Copy, PartialEq, Eq)] -pub enum Family { +enum Family { Any, V4, V6, @@ -1173,7 +1173,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 +1226,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 +1265,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 +1301,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.rs b/ffi/runtime/rt.rs index 9ac5796d5..0866654fe 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -16,6 +16,12 @@ use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::task::{Context, Wake}; +/// `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) -> ! { @@ -337,24 +343,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. /// @@ -441,27 +463,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()) } @@ -525,19 +529,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..]; } } @@ -585,121 +647,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/src/abi.rs b/ffi/src/abi.rs index c4766d2a1..d343aa51a 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) @@ -772,8 +785,8 @@ impl<'a> Abi<'a> { 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 6bf926d57..e678ed3b9 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -641,8 +641,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 +657,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(_) => return "_Addr".to_string(), + Ty::IpAddr(_) => return "_IpAddr".to_string(), Ty::Waker => return "_Waker".to_string(), Ty::ForeignFuture => return "_ForeignFuture".to_string(), Ty::ForeignReader => return "_ForeignReader".to_string(), diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index e428e28ae..d131b5161 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -103,8 +103,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(), @@ -862,9 +866,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`"), @@ -1106,8 +1109,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}))") @@ -1387,6 +1389,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, @@ -1436,6 +1444,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, @@ -2041,8 +2055,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() => { @@ -2080,13 +2094,13 @@ 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 e8b3e215e..76b499c09 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -816,8 +816,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(_) => "cAddr".to_string(), + Ty::IpAddr(_) => "cIpAddr".to_string(), Ty::Struct(n) | Ty::Enum(n) => format!("C.{n}"), Ty::Handle { name, .. } | Ty::Future(name) => format!("*C.{name}"), Ty::Waker => "cWaker".to_string(), 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 73add9a2a..52e687b6c 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -1079,7 +1079,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) { @@ -3241,7 +3241,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, } } @@ -3386,7 +3386,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"), @@ -3560,8 +3560,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)), 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..5ac0b8806 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -587,8 +587,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 +654,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() { @@ -1979,11 +1977,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 e57b4c3ef..5eaff1e47 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -400,8 +400,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(_) => abi.rt("socket_addr"), + Ty::IpAddr(_) => abi.rt("ip_addr"), Ty::Struct(n) | Ty::Enum(n) => n.clone(), Ty::Handle { .. } | Ty::Future(_) => "OpaquePointer?".to_string(), Ty::Waker => abi.rt("waker"), diff --git a/ffi/tests/api.c b/ffi/tests/api.c index d0eb75c1d..7ee10435f 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -5,6 +5,7 @@ #include #include +#include #include #ifdef __linux__ @@ -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); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 147b79365..c5e766d46 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,132 @@ 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"); + static const std::uint8_t ll_stray[] = {0xff}; + 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/fixtures.rs b/ffi/tests/fixtures.rs index 595de2352..878eb9add 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"), diff --git a/ffi/tests/fixtures/shapes.rs b/ffi/tests/fixtures/shapes.rs index 32adfa287..f43e97d9f 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 { From 49afd92bc507b7e5422a6edf9ddd5e4be6d97da1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:41:54 -0800 Subject: [PATCH 07/28] feat(ffi)!: Python's Duration crosses whole, and a Hash type hashes by Rust's Python's Duration is the runtime's NamedTuple of secs and nanos, made as a tuple, with from_timedelta and to_timedelta raising rather than truncating or clamping. An object or opaque value implementing Hash gets __hash__ from _hash. The api script prints the lossless lines, and checks each conversion's raise. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 22 ++++++++------ ffi/runtime/python.rs | 66 ++++++++++++++++-------------------------- ffi/runtime/rt.py | 29 +++++++++++++++++-- ffi/src/python.rs | 14 ++++++--- ffi/tests/api.py | 67 +++++++++++++++++++++++++++++++++++-------- 5 files changed, 130 insertions(+), 68 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index fa3852d2f..3f68a41c9 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -115,13 +115,16 @@ public path names, or an error taken by value, is held by value as an opaque han | `[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 @@ -673,7 +677,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, @@ -694,8 +698,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 `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`. diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 76eb18a37..a92485ce6 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; @@ -1114,52 +1114,34 @@ 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 as a tuple of its fields is, as its class is one. pub fn duration_out(duration: rt::Duration) -> R { - let class = ty(T_TIMEDELTA); - // SAFETY: the GIL is held. + let secs = uint_out(duration.secs)?; + let nanos = uint_out(duration.nanos.into())?; + // SAFETY: the GIL is held; a tuple class allocates its items, which each set takes. 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 obj = Obj::new(PyType_GenericAlloc(ty(T_DURATION).cast(), 2))?; + PyTuple_SetItem(obj.ptr(), 0, secs.into_ptr()); + PyTuple_SetItem(obj.ptr(), 1, nanos.into_ptr()); + Ok(obj) } } 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/src/python.rs b/ffi/src/python.rs index 5ac0b8806..a8b06610f 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(), @@ -921,7 +922,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); @@ -1015,6 +1016,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`."; diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 663f2b1a7..b0fd5db66 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,34 @@ 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"])) + paths = ["a/é", os.fsdecode(b"\xff")] + 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 +1397,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`, 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 +1452,23 @@ 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(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") + 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") From 54f63f2b9a0d5d683c87c497b45245bc3dfc3ccc Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:42:45 -0800 Subject: [PATCH 08/28] test(ffi): the WTF-8 decoder under Miri, and rustfmt Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 10 ++++++++-- ffi/fixtures/api/src/lib.rs | 5 ++++- ffi/src/glue.rs | 4 +++- ffi/tests/miri/api.rs | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 8b3b29805..b58fd923c 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -908,7 +908,11 @@ fn main() { 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 isize {} {}", + api::same_isize(isize::MIN), + api::same_isize(isize::MAX) + ); println!( "lossless u128 {:032x} {:032x} {:032x}", api::same_u128(0), @@ -952,7 +956,9 @@ fn main() { 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())) + 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 diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 48372e8aa..2cd7ed808 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -4091,6 +4091,9 @@ impl Key { /// 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) + std::hash::BuildHasher::hash_one( + &std::hash::BuildHasherDefault::::default(), + self, + ) } } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index d131b5161..354213379 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -2100,7 +2100,9 @@ impl Gen<'_> { _ => ("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/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); +} From 62007696f810f932c936ac1d44eb816d91f78682 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:03:47 -0800 Subject: [PATCH 09/28] feat(ffi)!: lossless Swift: RustDuration, std::net's address types, PathBuf, Hashable Every type the Swift binding takes or returns now holds every value of its Rust type exactly, with no trap left in any conversion: - Duration is the runtime's RustDuration (secs: UInt64, nanos: UInt32), Hashable, Comparable, Sendable, exact by construction: init(secs:nanos:) throws a CheckError for nanos >= 1e9. `duration` is a total Swift.Duration; RustDuration(_: Swift.Duration) throws for a negative duration, one past Rust's longest, or one with a part below a nanosecond (it used to trap on the first two and silently truncate the third). Total constructors .seconds/.milliseconds/.microseconds/.nanoseconds mirror Rust's from_*. Named RustDuration so it never shadows Swift.Duration in a module. - Addresses mirror std::net, one runtime type per C struct: Ipv4Addr (bits: UInt32) and Ipv6Addr (bits: U128), each with a failable init?(octets:) and an `octets` array, IpAddr an enum of .v4/.v6, SocketAddrV4, SocketAddrV6 (flowinfo, scopeId), and SocketAddr an enum of either with init(ip:port:) and ip/port getters. Each lowers to and lifts from its C struct with no allocation; a family mismatch is unrepresentable, so the old [UInt8] length trap and Rust's family aborts are gone. - Paths are the runtime's PathBuf of its `bytes`, a Bytes sharing Rust's buffer, so a non-UTF-8 path keeps its bytes (it used to trap) and a returned path is no longer copied. PathBuf(String) and a string literal make one; `string` is its text or nil. - A type implementing Hash and PartialEq exports hash(into:), combining _hash's u64, and conforms to Hashable instead of Equatable (under Swift 6.4 for a noncopyable struct); a checked type's hash(into:) throws and conforms to nothing, as its == does. Text, char, integers, and U128 were already exact. api.swift prints the transcript's lossless lines and checks each conversion and each refused value throws; the "family", "ip family", "stray", and "lent stray" abort cases are gone, since none can happen. moq.swift builds its binds from the new address types. Public API (Swift, breaking): Swift.Duration -> RustDuration, String paths -> PathBuf, the runtime SocketAddr/IpAddr structs -> the six std::net types, Hash types gain hash(into:) and Hashable. CheckError may now come from a runtime initializer. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 32 ++-- ffi/moq/moq.swift | 9 +- ffi/runtime/rt.swift | 400 +++++++++++++++++++++++++++++++++--------- ffi/src/swift.rs | 71 +++++--- ffi/tests/api.swift | 158 +++++++++++++---- ffi/tests/fixtures.rs | 4 - 6 files changed, 513 insertions(+), 161 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index fa3852d2f..174a68af7 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -759,7 +759,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 @@ -779,7 +780,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 @@ -793,12 +795,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 @@ -812,17 +816,23 @@ 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. 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. 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/rt.swift b/ffi/runtime/rt.swift index fc14a9461..0e94c5cfa 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 - /// IPv6's flow label, zero for IPv4. + + 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 + /// 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,146 @@ 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 + + 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 + } + } } } @@ -130,9 +327,10 @@ public struct U128: Hashable, Comparable, Sendable, CustomStringConvertible { /// 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. 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 +575,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 +614,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 +644,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 @@ -713,6 +935,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/src/swift.rs b/ffi/src/swift.rs index 5eaff1e47..0e383e79b 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\ @@ -144,8 +148,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", @@ -161,14 +165,20 @@ const TAKEN: &[&str] = &[ "Int", "Int64", "IpAddr", + "Ipv4Addr", + "Ipv6Addr", "Never", "OpaquePointer", "Optional", + "PathBuf", "RandomAccessCollection", + "RustDuration", "Sendable", "SeekOrigin", "SeekableReader", "SocketAddr", + "SocketAddrV4", + "SocketAddrV6", "StaticString", "Swift", "Task", @@ -347,12 +357,17 @@ impl<'a> Gen<'a> { 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(), @@ -400,8 +415,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"), @@ -480,8 +495,7 @@ impl<'a> Gen<'a> { 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})"), @@ -585,8 +599,7 @@ impl<'a> Gen<'a> { 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() => { @@ -818,8 +831,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, @@ -921,8 +934,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(); @@ -956,6 +973,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 { @@ -1250,12 +1275,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" ); } } @@ -1530,8 +1555,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)), diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index b381a21c1..3a2b239c4 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) @@ -1467,6 +1471,107 @@ 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") } + 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) @@ -1491,19 +1596,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 878eb9add..bf4ace36f 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1163,11 +1163,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"), ] { From 352aa73191ed2e316ddc077ebbf1a5910b71841b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:05:01 -0800 Subject: [PATCH 10/28] feat(ffi)!: lossless Dart: U64, RustDuration, PathBuf, std::net addresses, strict text, Hash Every type the Dart binding takes or returns now holds every value of its Rust type. - u64 and usize are the runtime's `U64`, an extension type on the int of its bits: zero cost, unsigned comparisons, checked `U64(int)`, `U64.fromBigInt`, `toInt()`, and `toBigInt()`/`toRadixString()`; `U64.fromBits` takes any bits. An extension type cannot override toString, so interpolation prints the signed int. A u64 block is a `List` backed by a Uint64List, with no copy beyond today's. usize and isize past 32 bits throw a RangeError on a 32-bit target instead of truncating. - Duration is `RustDuration` (U64 secs, nanos), exact by construction; `from(Duration)` and `toDuration()` convert checked instead of truncating to the microsecond and clamping. - Paths are `PathBuf` of the OS's bytes, crossing as bytes: a non-UTF-8 path no longer aborts. `PathBuf(String)` and a checked `toFilePath()`. - Addresses are the runtime's `Ipv4Addr`, `Ipv6Addr` (sealed `IpAddr`), `SocketAddrV4`, `SocketAddrV6` (sealed `SocketAddr`) over the per-family C structs, so a family mismatch is unrepresentable. Checked conversions from dart:io's InternetAddress throw ArgumentError for a Unix address, the other family, or an IPv6 scope. - A String with a lone surrogate throws ArgumentError instead of being replaced by Dart's UTF-8 encoder; abort and reader error messages stay lossy. - A type with Rust's Hash gets hashCode from `_hash`; without it, it still throws. Tests: api.dart prints the new lossless/key lines and checks every checked conversion and rejected value throws; the impossible abort cases (family, ip family, ip unix, stray, lent stray) are gone from fixtures.rs. The shapes driver passes U64 seeds and prints digests unsigned. moq.dart and the bench program follow the new types. Migration: wrap u64/usize ints in `U64(...)` (read back with `.toInt()`/`.bits`), `Duration` in `RustDuration.from(...)`, path Strings in `PathBuf(...)`, and `InternetAddress` in `IpAddr.from(...)`/`Ipv4Addr.from(...)`/`Ipv6Addr.from(...)`. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 59 ++-- ffi/moq/bench/export.dart | 6 +- ffi/moq/moq.dart | 23 +- ffi/runtime/rt.dart | 576 +++++++++++++++++++++++++++-------- ffi/src/dart.rs | 171 +++++++---- ffi/tests/api.dart | 394 +++++++++++++++--------- ffi/tests/fixtures.rs | 5 - ffi/tests/fixtures/shapes.rs | 21 +- 8 files changed, 867 insertions(+), 388 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index fa3852d2f..81c863207 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1030,17 +1030,25 @@ 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. 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 @@ -1048,22 +1056,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`. @@ -1078,8 +1091,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/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.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/runtime/rt.dart b/ffi/runtime/rt.dart index 0b12fd6ed..dfae18f0b 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -3,35 +3,407 @@ // 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); + + 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._(); + + @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); +} - /// IPv6's scope, zero for IPv4. +/// 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); +} + +/// `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 +560,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 +643,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 +693,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 +712,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 +980,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 +1216,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 +1245,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 +1287,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 +1311,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 +1318,15 @@ 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); + + /// 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/src/dart.rs b/ffi/src/dart.rs index e678ed3b9..770790481 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -334,18 +334,26 @@ const TAKEN: &[&str] = &[ "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 +561,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(), @@ -657,8 +668,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(), @@ -937,7 +948,10 @@ impl<'a> Gen<'a> { 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 +972,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 +1082,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 +1231,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 +1265,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 +1304,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 +1358,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 +1394,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 +1443,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 +1475,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 +1551,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 +2591,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 +2609,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 { diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index dfc0f5e53..0c2d2cd06 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,51 @@ 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(' ')}'); + print('lossless path ${[api.PathBuf('a/é'), api.stray()].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 +848,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 +870,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 +879,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 +905,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 +915,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 +1259,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 +1276,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 +1288,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 +1327,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 +1342,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 +1449,90 @@ 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'); + + // 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'); + + // 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 +1543,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 +1571,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 +1584,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 +1599,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/fixtures.rs b/ffi/tests/fixtures.rs index 878eb9add..6dba38fd9 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1273,11 +1273,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}")); diff --git a/ffi/tests/fixtures/shapes.rs b/ffi/tests/fixtures/shapes.rs index f43e97d9f..cac7d568e 100644 --- a/ffi/tests/fixtures/shapes.rs +++ b/ffi/tests/fixtures/shapes.rs @@ -1394,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. From 194e4a660a036e9f45d3bca0c308f0a45697ceeb Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:05:47 -0800 Subject: [PATCH 11/28] feat(ffi)!: lossless Go types: rt.Duration, rt.U128, std::net's addresses, and checked text Every type the Go binding takes or returns now holds every value of its Rust type, and nothing it converts panics or aborts. - `Duration` is `rt.Duration`, unexported seconds and nanoseconds, so it is exact by construction and its zero value is zero: `Secs()`, `Nanos()`, `rt.NewDuration(secs, nanos)` (carrying nanoseconds as `Duration::new`, failing past `Duration::MAX`), `rt.DurationOf(time.Duration)` (failing on a negative one), and `Std()` (failing past 292 years). Replaces `time.Duration`, which panicked both ways. - `u128` is `rt.U128{Hi, Lo}`, with `Big()` and `rt.U128Of(*big.Int)`, replacing a `*big.Int` that panicked out of range and allocated per value. `Option` is now `*rt.U128`. - Addresses mirror std::net: `rt.Ipv4Addr`, `rt.Ipv6Addr`, `rt.SocketAddrV4`, `rt.SocketAddrV6` (flow label and scope kept), and `rt.IpAddr` / `rt.SocketAddr` with unexported fields, built by `rt.IpAddrFromV4` and so on, read by `V4()` / `V6()`. Each has `Netip()` and `rt.Of(netip...)`, failing on the wrong family, a zone an `Ipv6Addr`/`IpAddr` cannot hold, or a flow label netip cannot. Replaces `netip`, which panicked on a flow label, a zone, and the wrong family; the per-family C structs make a wrong family unrepresentable. - A `string` that is not UTF-8 or a `rune` that is not a Unicode scalar, anywhere in a call's arguments, fails the call before it is made with an error matching the new `rt.ErrInvalid`, instead of Rust aborting the process. Top-level text and runes are checked in the prelude; nested ones record the failure in the call's arena, which the existing closed-object check now returns after every argument is lowered. So every function taking text or a char gains an `error` result, as does `Equal` of a value holding text. What a Go closure or trait returns to Rust still aborts on invalid text: Rust, its caller, has no error to take. Paths stay any bytes. - Every checked conversion returns an error matching `rt.ErrInvalid`. tests/api.go prints the transcript's lossless lines and proves each refused value and each failing conversion returns `rt.ErrInvalid`; the `family`, `ip family`, `ip scope`, `text`, and `duration` abort cases are gone, as none can happen any more. moq.go builds its binds from netip through the checked conversions. Migration: `time.Duration` -> `rt.DurationOf(d)` / `d.Std()`; `*big.Int` -> `rt.U128Of(v)` / `v.Big()`; `netip.AddrPort` -> `rt.SocketAddrOf(a)` / `a.Netip()` (and the V4/V6 forms); `netip.Addr` -> `rt.IpAddrOf(ip)` / `ip.Netip()`; add an error check to calls that take text or a char. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 34 +++-- ffi/moq/moq.go | 15 +- ffi/runtime/ffi_cgo.go | 248 +++++++++++++++++--------------- ffi/runtime/rt.go | 289 +++++++++++++++++++++++++++++++++++++ ffi/src/go.rs | 152 +++++++++++++------- ffi/tests/api.go | 313 ++++++++++++++++++++++++++++------------- ffi/tests/fixtures.rs | 5 - 7 files changed, 777 insertions(+), 279 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index fa3852d2f..6dc7cc4ca 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -943,15 +943,31 @@ 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, or a `rune` that is not a Unicode scalar value, + 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`, `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 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/runtime/ffi_cgo.go b/ffi/runtime/ffi_cgo.go index ab13cefa5..553edaebb 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,135 @@ 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)))) } -// 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") - } - return lowerAddr_(a) +func lowerSocketAddrV4_(a rt.SocketAddrV4) cSocketAddrV4 { + return cSocketAddrV4{ip: lowerIpv4Addr_(a.IP), port: C.uint16_t(a.Port)} } -// 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)) - } - if c.flowinfo != 0 { - panic("an IPv6 flow label has no Go form") - } - ip := netip.AddrFrom16([16]byte(b)) - if c.scope_id != 0 { - ip = ip.WithZone(strconv.FormatUint(uint64(c.scope_id), 10)) +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 netip.AddrPortFrom(ip, uint16(c.port)) } -// 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") +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 ip.Zone() != "" { - panic("an IPv6 address with a zone has no Rust IpAddr") +} + +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) } - 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 } -// 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") +func liftSocketAddr_(c cSocketAddr) rt.SocketAddr { + if c.v6 { + return rt.SocketAddrFromV6(liftSocketAddrV6_(*(*cSocketAddrV6)(unsafe.Pointer(&c.addr)))) + } + return rt.SocketAddrFromV4(liftSocketAddrV4_(*(*cSocketAddrV4)(unsafe.Pointer(&c.addr)))) +} + +// What a call returns before it is made once it is given text that is not UTF-8 or a rune +// that is not a Unicode scalar value, which no Rust string or char holds. +var ( + errText_ = rt.Invalid_("a string is not UTF-8") + errRune_ = rt.Invalid_("a rune is not a Unicode scalar value") +) + +// 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 checkStringOption_(s *string) error { + if s == nil { + return nil } - return lowerIp_(ip) + return checkString_(*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])) +// 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 netip.AddrFrom16([16]byte(b)) + return nil } // liftList_ is the array Rust returned, each item lifted by lift, which takes what it @@ -308,8 +295,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 } @@ -331,12 +318,36 @@ 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. +func (a *arena_) bufPath(s string) cBuf { 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) +} + +// 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 { @@ -366,6 +377,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{} @@ -378,9 +396,7 @@ func (a *arena_) bufViewOption(b *rt.Bytes) cBuf { func (a *arena_) hold(h *rt.Handle_, o any) unsafe.Pointer { raw := 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, o}) diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index dd8a482c9..8e9083980 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -639,3 +639,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/src/go.rs b/ffi/src/go.rs index 76b499c09..2b01e8425 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"), ]; @@ -356,6 +353,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_", "NewHandle_", "LiftBytes_", @@ -559,9 +579,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\ @@ -589,7 +609,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\ @@ -719,17 +739,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), @@ -785,7 +805,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(), @@ -793,6 +813,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(_) @@ -816,8 +838,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(), @@ -834,7 +856,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 => 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)), @@ -857,6 +879,21 @@ 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 or a char, whose Go types hold + /// values Rust's do not. + fn fails(&self, ty: &Type) -> bool { + let api = self.abi.api; + match ty { + Type::Object(_) | Type::Str | Type::String | Type::Char => 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) { @@ -895,22 +932,19 @@ 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::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(_) => { @@ -927,7 +961,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); @@ -946,11 +981,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" @@ -1051,8 +1082,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)) @@ -1086,12 +1117,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})") @@ -1435,8 +1463,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)); @@ -2147,7 +2176,9 @@ 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::NonZero(_) | Type::F32 | Type::F64 => self.lower(ty, e), + // Rust, the caller, has no error to take, so one that is no Unicode scalar aborts. + 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:?}"), } @@ -2374,11 +2405,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 { @@ -2440,7 +2472,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); } } } @@ -2469,6 +2501,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; @@ -2515,7 +2548,27 @@ 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})")); + } (ty, Ty::Buf) => { + // Text is checked before the call; a path is any bytes. + let check = match ty.buffer() { + Some(Type::String) => Some("checkString_"), + Some(Type::Option(inner)) if *inner == Type::String => Some("checkStringOption_"), + _ => 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})"), @@ -2551,6 +2604,7 @@ impl<'a> Gen<'a> { } (ty, _) => { arena |= self.arena(ty); + checked |= self.fails(ty); lowered.push(args.len()); args.push(self.lower(ty, &name)); } @@ -2585,17 +2639,17 @@ impl<'a> Gen<'a> { prelude.insert(take_at, "a_.take = true".to_string()); } body.extend(prelude); - 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. + // 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"))); + } // Nothing from here fails before the call, which Rust's buffers given it go to. if arena { body.push("a_.made = true".to_string()); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 26a7735e5..c2e951672 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,12 +712,12 @@ 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 { if p == nil { @@ -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", api.SamePath("a/é"), 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() @@ -1457,7 +1520,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 +1531,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 +1549,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 +1561,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 +1570,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 +1701,85 @@ 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(api.SamePath("\xff") == "\xff", "a path is any bytes") + // 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)) + } + + // 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 +1847,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() diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 878eb9add..e5ad6e146 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1479,13 +1479,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"), From dedc8ec7a940778b8b12463e7e3ed1bf2ae23237 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:10:03 -0800 Subject: [PATCH 12/28] feat(ffi)!: a path that is no Windows path raises in Go and Python Go returns rt.ErrInvalid for a path string that is not WTF-8 on Windows, as it does for text that is not UTF-8, so each function taking a path returns an error; Python raises ValueError for a str whose surrogates make no WTF-8. Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/ffi_cgo.go | 39 ++++++++++++++++++++++++++++++++++++++- ffi/runtime/python.rs | 9 +++++++++ ffi/src/go.rs | 10 ++++++---- ffi/tests/api.go | 6 +++--- 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/ffi/runtime/ffi_cgo.go b/ffi/runtime/ffi_cgo.go index 553edaebb..348f886b6 100644 --- a/ffi/runtime/ffi_cgo.go +++ b/ffi/runtime/ffi_cgo.go @@ -229,8 +229,34 @@ func liftSocketAddr_(c cSocketAddr) rt.SocketAddr { 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") ) +// 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 + } + 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 + } + return nil +} + // checkString_ is the error a call given s returns, or nil for UTF-8. func checkString_(s string) error { if !utf8.ValidString(s) { @@ -239,6 +265,13 @@ func checkString_(s string) error { return nil } +func checkPathOption_(s *string) error { + if s == nil { + return nil + } + return checkPath_(*s) +} + func checkStringOption_(s *string) error { if s == nil { return nil @@ -326,8 +359,12 @@ func (a *arena_) bufString(s string) cBuf { return a.bufPath(s) } -// bufPath is a path's bytes, which C reads during the call, pinned: any a string holds. +// 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 diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index a92485ce6..98fb7a31c 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -1096,6 +1096,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, diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 2b01e8425..8acb85fc4 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -880,12 +880,12 @@ 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 or a char, whose Go types hold - /// values Rust's do not. + /// error: it holds an object, which may be closed, or text, a char, or a path, 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 => true, + Type::Object(_) | Type::Str | Type::String | Type::Char | Type::Path | Type::PathBuf => 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)), @@ -2557,10 +2557,12 @@ impl<'a> Gen<'a> { args.push(format!("C.uint32_t({name})")); } (ty, Ty::Buf) => { - // Text is checked before the call; a path is any bytes. + // 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 { diff --git a/ffi/tests/api.go b/ffi/tests/api.go index c2e951672..de66801f9 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -719,7 +719,7 @@ func script() { 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" } @@ -948,7 +948,7 @@ func script() { fmt.Printf(" %x", must(api.SameChar(c))) } fmt.Println() - fmt.Printf("lossless path %x %x\n", api.SamePath("a/é"), api.SamePath(must(api.Stray()))) + 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}, @@ -1725,7 +1725,7 @@ func checked() { _, 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(api.SamePath("\xff") == "\xff", "a path is any bytes") + 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) From b16f3c5e8337750921b4a43753c7298791113359 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:13:12 -0800 Subject: [PATCH 13/28] feat(ffi)!: a zero passed for a NonZero raises ValueError in Python Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/python.rs | 9 +++++++++ ffi/src/python.rs | 3 ++- ffi/tests/api.py | 4 +++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 98fb7a31c..f07c98c8f 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -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}")) diff --git a/ffi/src/python.rs b/ffi/src/python.rs index a8b06610f..658ef8281 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -579,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})"), diff --git a/ffi/tests/api.py b/ffi/tests/api.py index b0fd5db66..3c5aba3de 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -1398,7 +1398,7 @@ def forwarding(): def values(): """The values Python's types bound: integers checked against their Rust type's range, durations and their conversions to and from `timedelta`, which raise rather than lose - anything, a key hashing by Rust's `Hash`, and on Unix, a path whose bytes are not UTF-8, which crosses as `os.fsdecode` decodes it, + 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): @@ -1466,6 +1466,8 @@ def raises(error, f, what): 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") From 61bb3c51ea0a68300bffcf3620d78d6c0710e4df Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:16:58 -0800 Subject: [PATCH 14/28] feat(ffi)!: a zero passed for a NonZero throws in Dart and fails the call in Go Dart throws ArgumentError from the lowering; Go fails the call with an error matching rt.ErrInvalid, so a function taking a NonZero returns an error. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 13 +++++++------ ffi/runtime/ffi_cgo.go | 14 ++++++++++++-- ffi/runtime/rt.dart | 3 +++ ffi/src/dart.rs | 3 ++- ffi/src/go.rs | 23 ++++++++++++++++------- ffi/tests/api.dart | 2 ++ ffi/tests/api.go | 10 +++++++--- 7 files changed, 49 insertions(+), 19 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index f1c6bc9d9..21c9582bc 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -111,7 +111,7 @@ 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 too; a zero passed in aborts, which each binding but C++ checks first and raises, and `Option>` is `T`'s option | | `[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` | @@ -701,7 +701,7 @@ caller imports it with no ignores. 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 `Range` is a `range`, and an `Err` a raised `Exception` + 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`. @@ -978,8 +978,8 @@ reads a go.mod. `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, or a `rune` that is not a Unicode scalar value, - fails the call taking it, anywhere in its arguments, before it is made, with an error + with `==`. A `string` that is not UTF-8, a `rune` that is not a Unicode scalar value, + 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`, `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. @@ -1076,8 +1076,9 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass `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. A `char` is a `String` of one Unicode scalar, - and any other throws an `ArgumentError`. + `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 diff --git a/ffi/runtime/ffi_cgo.go b/ffi/runtime/ffi_cgo.go index 348f886b6..88ad354fc 100644 --- a/ffi/runtime/ffi_cgo.go +++ b/ffi/runtime/ffi_cgo.go @@ -224,12 +224,13 @@ func liftSocketAddr_(c cSocketAddr) rt.SocketAddr { return rt.SocketAddrFromV4(liftSocketAddrV4_(*(*cSocketAddrV4)(unsafe.Pointer(&c.addr)))) } -// What a call returns before it is made once it is given text that is not UTF-8 or a rune -// that is not a Unicode scalar value, which no Rust string or char holds. +// 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 @@ -378,6 +379,15 @@ func (a *arena_) char(r rune) C.uint32_t { 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 { diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index dfae18f0b..7ee6d839a 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -1319,6 +1319,9 @@ final class _Native { 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; diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 770790481..e1a662980 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -941,7 +941,8 @@ 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})"), diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 8acb85fc4..e62706024 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -856,7 +856,7 @@ impl<'a> Gen<'a> { let api = self.abi.api; match ty { ty if Abi::is_buf(ty) => true, - Type::List(..) | Type::Object(_) | Type::Char => 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)), @@ -880,12 +880,14 @@ 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, or a path, whose Go - /// types hold values Rust's do not, a path's only on Windows. + /// 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 => true, + 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)), @@ -937,7 +939,8 @@ impl<'a> Gen<'a> { 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!("a_.char({e})"), @@ -2176,8 +2179,10 @@ 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 => self.lower(ty, e), - // Rust, the caller, has no error to take, so one that is no Unicode scalar aborts. + 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:?}"), @@ -2556,6 +2561,10 @@ impl<'a> Gen<'a> { )); 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() { diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 0c2d2cd06..2d93776d4 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -1468,6 +1468,8 @@ void bounds() { 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'); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index de66801f9..96c89adf6 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -1050,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 @@ -1731,6 +1731,10 @@ func checked() { _, 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") From 11bcc60414d683e50d646c6ea139e914c50dc441 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:17:16 -0800 Subject: [PATCH 15/28] chore(quest): lossless binding types is done Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-export/README.md | 2 +- quest/m1/ffi-export/lossless.md | 54 --------------------------------- quest/m1/ffi-export/ship.md | 1 - quest/m2/wasm/backend.md | 4 --- 4 files changed, 1 insertion(+), 60 deletions(-) delete mode 100644 quest/m1/ffi-export/lossless.md 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 From 01db934725f5e25d6d0031571a8686e0eca65e95 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:20:35 -0800 Subject: [PATCH 16/28] feat(ffi)!: lossless Kotlin types: runtime Duration, addresses, and PathBuf; text, char, usize checked Every type the Kotlin binding takes or returns now holds every value of its Rust type, and a value Kotlin's type holds that Rust's cannot throws IllegalArgumentException instead of being replaced or aborting. - Duration: a runtime `Duration(secs: ULong, nanos: UInt)`, exact by construction, with `Duration(kotlin.time.Duration)` and `toKotlinDuration()`, which throw for a negative or infinite value in and for one kotlin.time cannot hold exactly out (past Long.MAX_VALUE seconds, or sub-millisecond past ~146 years); `ZERO`, `MAX`, ordering, equality. - Addresses: sealed `IpAddr` of `Ipv4Addr` (value class of its bits) or `Ipv6Addr`; sealed `SocketAddr` of `SocketAddrV4` (value class of ip and port) or `SocketAddrV6` (flow label and scope kept). Each prints as Rust's Display does, and converts to and from java.net's, throwing for an unresolved address, a scoped IpAddr, or a flow label out. The codec follows jni.rs's per-family Wire with no allocation beyond the value. - Paths: a runtime `PathBuf` of the OS's bytes (any on Unix; WTF-8 checked on Windows), from bytes or a String, with `toText()`/`toPath()` checked and `toString()` lossy as Rust's `display`. Passed directly as a byte[], encoded as a Kotlin array ref. - Text: a String with a lone surrogate throws, never `?`. The glue detects it in the one UTF-16 to UTF-8 pass it already made: a direct String is encoded first, before any other argument converts, and an encoded value's texts are listed in a trailer Kotlin's writer appends (their places in refs), which the Reader encodes before it reads any of the value. Either way nothing is converted that would need freeing. Closure returns and closure errors encode strictly (a throw there aborts, as any callback throw does). - char: `_Native.__char` throws unless the Int is a Unicode scalar value. - usize/isize: `_Native.__usize`/`__isize` check against the library's target, read once from a new native `__usize_max`; jni.rs's `narrow` then never fails for a Kotlin value. Not run on a 32-bit target: the bound is `usize::MAX`, so 64-bit admits all and 32-bit throws past u32/i32, by construction. - u128: unchanged checks, now throwing the binding's own `__Invalid`. - Hash: an object's, or an opaque value's compared through Rust, `hashCode` is `_hash` folded as Long.hashCode, when the type implements Hash; 0 otherwise. - `_Native.__Invalid` (an IllegalArgumentException) marks every argument refusal, Kotlin's or the glue's, so a call that moves an object by value leaves it open when it throws. - The new runtime names are reserved (TAKEN), so an export named so takes a trailing `_`. api.kt prints the transcript's lossless lines and checks each conversion and rejection; the stray/family/ip-family/ip-scope abort cases are gone, as none can happen now. moq.kt builds its binds from the runtime addresses. Public API (Kotlin, breaking): Duration, SocketAddr*, IpAddr*/Ipv*Addr, and paths are the runtime's types above instead of kotlin.time.Duration, java.net.InetSocketAddress/InetAddress, and String; ports are UShort. Migrate with `Duration(kd)`, `SocketAddr.of(isa)`, `IpAddr.of(ia)`, `PathBuf(string)` and the `to*` conversions. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 40 ++++-- ffi/moq/moq.kt | 15 +-- ffi/runtime/jni.rs | 157 +++++++++++++++++----- ffi/runtime/rt.kt | 189 ++++++++++++++++----------- ffi/runtime/types.kt | 296 ++++++++++++++++++++++++++++++++++++++++++ ffi/src/kotlin.rs | 202 +++++++++++++++++++--------- ffi/tests/api.kt | 175 ++++++++++++++++++------- ffi/tests/fixtures.rs | 4 - 8 files changed, 833 insertions(+), 245 deletions(-) create mode 100644 ffi/runtime/types.kt diff --git a/ffi/README.md b/ffi/README.md index fa3852d2f..d99c56814 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -546,7 +546,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. @@ -560,8 +561,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()`. @@ -577,21 +578,34 @@ 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, `Option` `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 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/runtime/jni.rs b/ffi/runtime/jni.rs index 4dc708d2b..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 { diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index 822e2321b..f9704792a 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 @@ -221,7 +230,22 @@ class __Writer { (held ?: java.util.ArrayList<__Handle>().also { held = it }).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() { @@ -318,32 +342,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() @@ -351,13 +366,29 @@ 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 `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 } /** @@ -570,7 +601,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/types.kt b/ffi/runtime/types.kt new file mode 100644 index 000000000..3fb94e26e --- /dev/null +++ b/ffi/runtime/types.kt @@ -0,0 +1,296 @@ +// 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" } + 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/kotlin.rs b/ffi/src/kotlin.rs index 52e687b6c..482b70cbf 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"), @@ -740,18 +769,15 @@ impl<'a> Gen<'a> { } Type::F32 => format!("{w}.f32({e})"), Type::F64 => format!("{w}.f64({e})"), - Type::Char => format!("{w}.i32({e})"), - Type::Str | Type::String | 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 +844,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 +919,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 +943,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) => @@ -967,7 +997,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 +1026,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. @@ -1064,8 +1109,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", @@ -1096,14 +1141,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}"), @@ -1320,7 +1366,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")], @@ -1339,11 +1386,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(), ) } }; @@ -1351,10 +1399,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( @@ -1369,6 +1417,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. @@ -1419,14 +1480,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 { @@ -1440,8 +1502,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 { @@ -2042,10 +2105,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 { @@ -2378,7 +2438,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()); } @@ -2847,7 +2910,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), }; @@ -3101,7 +3164,7 @@ impl<'a> Gen<'a> { } 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); @@ -3347,22 +3410,32 @@ 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 bound struct of `int`'s pairs of bounds. fn abi_bound(abi: &Abi<'_>, int: Int) -> String { abi.range(&Type::Bound(int)) @@ -3460,7 +3533,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) }}", @@ -3612,7 +3685,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/tests/api.kt b/ffi/tests/api.kt index 47f1a7c22..4627fe99a 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,110 @@ 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) }}") + println("lossless path ${listOf(PathBuf("a/é"), PathBuf(byteArrayOf(-1))).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") + + // 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") + 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 +1233,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 +1257,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/fixtures.rs b/ffi/tests/fixtures.rs index 878eb9add..f638a3aea 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -860,10 +860,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. From 30116323de42633421b6623fcb662b3ea3010191 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:22:47 -0800 Subject: [PATCH 17/28] feat(ffi)!: a zero passed for a NonZero throws IllegalArgumentException in Kotlin Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 3 ++- ffi/runtime/rt.kt | 9 +++++++++ ffi/src/kotlin.rs | 23 ++++++++++++++++++++--- ffi/tests/api.kt | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 67e7879cb..6f6228af4 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -587,7 +587,8 @@ the rest cross encoded. 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, `Option` `T?`, a tuple `Pair` or + 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, and an `Err` a thrown `Exception` diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index f9704792a..db06da5ee 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -385,6 +385,15 @@ fun __isize(v: Long): Long { 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") diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 482b70cbf..79c53ef0b 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -763,10 +763,14 @@ 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({})", scalar_value(e)), @@ -989,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 => { @@ -3158,10 +3166,14 @@ 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", scalar_value(&e), format!("{p} as u32")), @@ -3436,6 +3448,11 @@ 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)) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 4627fe99a..409bd85b2 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -925,6 +925,7 @@ fun lossless() { // 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") From 790a22ebb55c91bf93e6657dc722796b197992c6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:41:37 -0800 Subject: [PATCH 18/28] feat(ffi)!: a zero passed for a NonZero throws CheckError in Swift `Native.nonzero` checks each `NonZero` the binding lowers, as an argument, an option's value, a list's item, a tuple's element, or a record's or enum's field, throwing the runtime's `CheckError` before any Rust runs instead of aborting in the glue. A function whose lowering can throw now `throws`, and lowers that argument before any write-back or close is deferred, so a throw leaves every argument as it was. A record or enum holding a `NonZero` throws from its trait members, as one holding a checked object does, so it conforms to none of `Equatable`, `Hashable`, `CustomStringConvertible`, and `CustomDebugStringConvertible`. A zero a closure or trait implementation returns traps, as a closed copy does. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 12 +++-- ffi/runtime/rt.swift | 15 +++++- ffi/src/swift.rs | 106 +++++++++++++++++++++++++++++++------------ ffi/tests/api.swift | 13 ++++-- 4 files changed, 107 insertions(+), 39 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index f1c6bc9d9..a443f1c9f 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -799,18 +799,22 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u that is not `Sync` on a second thread, throws a `CheckError`, holding the function and the rule, before any Rust code runs, so the call changed nothing. So every function whose objects can fail a check `throws`, and a 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` + whose getter Swift cannot let throw, which is a method, `firstGet()`. A `NonZero` is + `T`'s Swift integer, and a zero passed for one, as an argument or anywhere in one, such + as an option's, an array's item, or a record's field, throws a `CheckError` too, so + every function taking one `throws`. Of a type whose checks can fail, or a record or enum + holding one or a `NonZero`, `description` and `debugDescription` 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. + copy or a zero `NonZero` a closure or trait implementation 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, 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 + one or a `NonZero`, 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 `mutating`, and assigns `self` what Rust wrote back. - **Bytes** are the runtime's `Bytes`, a `RandomAccessCollection` of `UInt8` whose slices diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 0e94c5cfa..fec4ad24d 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -328,9 +328,10 @@ public struct U128: Hashable, Comparable, Sendable, CustomStringConvertible { /// 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, an item called on a library whose target lacks it, or a value Rust's type -/// cannot hold given a runtime type's initializer. +/// cannot hold, such as a zero passed for a `NonZero` or given a runtime type's initializer. public struct CheckError: Swift.Error, CustomStringConvertible, Sendable { - /// The function called, by its C name, or the runtime initializer. + /// The function called, or the record or enum whose value broke, by its C name, or the + /// runtime initializer. public let function: Swift.String /// What broke. public let message: Swift.String @@ -637,6 +638,16 @@ extension Native { view(path.bytes) { copy($0) } } + /// `value`, a `NonZero`'s integer that `what` passes, throwing for a zero, which Rust's + /// type cannot hold. + @inline(__always) + static func nonzero(_ value: T, _ what: StaticString) throws -> T { + guard value != 0 else { + throw CheckError(function: "\(what)", message: "a NonZero is zero") + } + return value + } + static func scalar(_ code: UInt32) -> Unicode.Scalar { guard let scalar = Unicode.Scalar(code) else { fatalError("a char is not a Unicode scalar") diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 0e383e79b..2308864fc 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -61,6 +61,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result { fresh: usize, /// The targets of the item being written, whose calls check theirs. on: On, + /// The C name of the function or type whose values the lowerings being written pass, + /// which a zero passed for a `NonZero` throws naming. + what: String, } impl<'a> Gen<'a> { @@ -451,11 +455,38 @@ impl<'a> Gen<'a> { } /// Whether lowering a `ty` nested in another value can throw: it holds a class whose - /// value a move takes, closing its handle. + /// value a move takes, closing its handle, or a `NonZero`, which may be zero. fn throwing(&self, ty: &Type) -> bool { let mut handles = Vec::new(); self.abi.api.handles(ty, &mut handles, &mut Vec::new()); - handles.into_iter().any(|i| self.held(i)) + handles.into_iter().any(|i| self.held(i)) || self.zeroable(ty, &mut BTreeSet::new()) + } + + /// Whether a `ty` lowers a `NonZero` anywhere, its records' and enums' fields included, + /// but an opaque record's, which only Rust makes. + fn zeroable(&self, ty: &Type, seen: &mut BTreeSet<(bool, usize)>) -> bool { + let api = self.abi.api; + match ty { + Type::NonZero(_) => true, + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => { + self.zeroable(inner, seen) + } + Type::Tuple(elements) => elements.iter().any(|e| self.zeroable(e, seen)), + // A type already seen is being looked through, so it adds nothing new. + Type::Record(i) => { + let record = &api.records[*i]; + record.opaque.is_none() + && seen.insert((false, *i)) + && record.fields.iter().any(|f| self.zeroable(&f.ty, seen)) + } + Type::Enum(i) => { + seen.insert((true, *i)) + && (api.enums[*i].variants.iter()) + .flat_map(|v| &v.fields) + .any(|f| self.zeroable(&f.ty, seen)) + } + _ => false, + } } /// Whether object `i` is a class a move may close, whose hold for a call can fail. @@ -465,9 +496,11 @@ impl<'a> Gen<'a> { /// `lowered`, a lowering in a function that cannot throw, trapping instead. fn trapping(lowered: String) -> String { - match lowered.contains("try ") { - true => format!("try! {lowered}"), - false => lowered, + match lowered.strip_prefix("try ") { + // Swift refuses a `try` whose operand is another. + Some(rest) => format!("try! {rest}"), + None if lowered.contains("try ") => format!("try! {lowered}"), + None => lowered, } } @@ -490,7 +523,8 @@ 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) => format!("try Native.nonzero({}, \"{}\")", lower_int(*int, e), self.what), Type::Char => format!("{e}.value"), Type::U128 => format!("Native.u128({e})"), Type::Array(_) => format!("{e}._c"), @@ -846,8 +880,10 @@ impl<'a> Gen<'a> { let object = matches!(ty, Type::Object(_)); let unique = self.unique(ty); // One whose checks can fail throws, so conforms to no protocol of these, whose - // requirements cannot throw; any other's checks never fail. + // requirements cannot throw; any other's checks never fail. So does a value holding a + // `NonZero`, which may be zero, though Rust's function takes no status. let status = self.abi.traits_status_of(ty); + let checked = status || self.throwing(ty); // The statements lowering a value receiver for the function `what`, which on a target // the type is not on throws where it can. let value = |what: &str| { @@ -857,7 +893,7 @@ impl<'a> Gen<'a> { }; format!( "{}Native.check()\n\t\t{arena}", - self.on(self.on, what, "\n\t\t", status) + self.on(self.on, what, "\n\t\t", checked) ) }; let lower = match self.throwing(ty) { @@ -895,18 +931,18 @@ impl<'a> Gen<'a> { ), } }; - let throws = match status { + let throws = match checked { true => " throws", false => "", }; // A property's body, in a getter that throws if the call can. - let getter = |body: String| match status { + let getter = |body: String| match checked { true => format!("get throws {{\n\t\t\t{}\n\t\t}}", body.replace("\n\t\t", "\n\t\t\t")), false => body, }; let text = |r: &str| format!("Native.string({r})"); if t.display { - if !status { + if !checked { protocols.push("CustomStringConvertible"); } members.push("description".to_string()); @@ -916,7 +952,7 @@ impl<'a> Gen<'a> { )); } if t.debug { - if !status { + if !checked { protocols.push("CustomDebugStringConvertible"); } members.push("debugDescription".to_string()); @@ -935,7 +971,7 @@ impl<'a> Gen<'a> { } if t.eq { // `Hash` agrees with `PartialEq`, so a type with both hashes as Rust does. - if !status { + if !checked { protocols.push(match t.hash { true => "Hashable", false => "Equatable", @@ -1059,6 +1095,7 @@ impl<'a> Gen<'a> { } }) .collect(); + self.what = c.clone(); let lowered: Vec = (record.fields.iter().zip(&fields)) .map(|(f, (name, _))| { let value = self.lower(&f.ty, &format!("self.{name}")); @@ -1127,6 +1164,7 @@ impl<'a> Gen<'a> { let e = &abi.api.enums[i]; self.on = e.on; let c = abi.enumeration(i); + self.what = c.clone(); let name = self.class(&c); let rust = abi.api.rust(&e.path, &e.args); let ty = Type::Enum(i); @@ -1290,6 +1328,7 @@ impl<'a> Gen<'a> { let callback = &abi.api.callbacks[i]; self.on = callback.on; let c = abi.callback(i); + self.what = c.clone(); let name = self.class(&c); let rust = callback.path.join("::"); let mut body = Vec::new(); @@ -1611,6 +1650,7 @@ impl<'a> Gen<'a> { let abi = self.abi; let export = abi.export(function, owner); let c_name = export.call.name.clone(); + self.what = c_name.clone(); let owner_c = match owner { Some(Ty::Struct(n) | Ty::Enum(n) | Ty::Handle { name: n, .. }) => Some(n.clone()), _ => None, @@ -1684,7 +1724,8 @@ impl<'a> Gen<'a> { } _ => { arena |= self.arena(&ty); - let t = match self.throwing(&ty) { + arg.throws = self.throwing(&ty); + let t = match arg.throws { true => "try ", false => "", }; @@ -1741,6 +1782,8 @@ impl<'a> Gen<'a> { arena |= needs; args.push(arg); } + // A `NonZero` passed, which may be zero, throws as a failed check does. + let throws = throws || args.iter().any(|a| a.throws); let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); // What runs once the call returns, gated on its checks passing when it can fail one: // a failed check, or an argument whose lowering throws, leaves every argument as it @@ -1754,6 +1797,25 @@ impl<'a> Gen<'a> { true => format!("defer {{ if c_ran_ {{ {stmts} }} }}"), false => format!("defer {{ {stmts} }}"), }; + if arena { + prelude.insert( + 1.min(prelude.len()), + format!("let a_ = Native.Arena(\"{c_name}\")\ndefer {{ a_.finish() }}"), + ); + // Past the receiver, which Rust only reads, Rust takes each value passed. + prelude.push("a_.take = true".to_string()); + } + // A lowering that throws runs before any box or buffer is given, which the call + // alone would take: a throw between them would leak it. One in a call that fails no + // check, a `NonZero`'s, runs before any argument's end is deferred too, which only a + // check gates. + if !status || args.iter().any(|a| a.gives || a.takes) { + let receiver = usize::from(receiver.is_some()); + for (k, arg) in args.iter_mut().enumerate().skip(receiver).filter(|(_, a)| a.throws) { + prelude.push(format!("let c_arg{k}_ = {}", arg.exprs[0])); + arg.exprs[0] = format!("c_arg{k}_"); + } + } // Each value passed for `&mut` takes what Rust wrote back, once the call's own // access to it ends, on every path the call ran, a throw included. let mut written = Vec::new(); @@ -1768,26 +1830,10 @@ impl<'a> Gen<'a> { prelude.push(gate(&format!("{name} = {lifted}"))); written.push(out); } - if arena { - prelude.insert( - 1.min(prelude.len()), - format!("let a_ = Native.Arena(\"{c_name}\")\ndefer {{ a_.finish() }}"), - ); - // Past the receiver, which Rust only reads, Rust takes each value passed. - prelude.push("a_.take = true".to_string()); - } let after: Vec = args.iter().flat_map(|a| a.after.clone()).collect(); if !after.is_empty() { prelude.push(gate(&after.join("; "))); } - // A lowering that throws runs before any box or buffer is given, which the call - // alone would take: a throw between them would leak it. - if args.iter().any(|a| a.gives || a.takes) { - for (k, arg) in args.iter_mut().enumerate().filter(|(_, a)| a.throws) { - prelude.push(format!("let c_arg{k}_ = {}", arg.exprs[0])); - arg.exprs[0] = format!("c_arg{k}_"); - } - } let exprs: Vec = args.iter().flat_map(|a| a.exprs.clone()).collect(); // A tuple of one returned whole is its element, written through its one out pointer. diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 3a2b239c4..44cc8afde 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -1013,11 +1013,18 @@ func script() { } catch { check(false, "seekTo throws its io::Error's message") } - print("non zeros \(describeNonZeros(nonZeros()))") + print("non zeros \(try! describeNonZeros(nonZeros()))") for hop in [nil, 1, UInt64.max] as [UInt64?] { - print("hop \(nextHop(hop).map { "Some(\($0))" } ?? "None")") + print("hop \(try! nextHop(hop).map { "Some(\($0))" } ?? "None")") } - print("farther hop \(fartherHop(PairNonZeroU16(left: 3, right: .max)))") + print("farther hop \(try! fartherHop(PairNonZeroU16(left: 3, right: .max)))") + // A zero passed for a `NonZero`, which Swift's integer holds and Rust's type does not, + // throws before any Rust runs, in an option and a record's field too. + check(refused("api_next_hop") { _ = try nextHop(0) }, "a zero NonZero throws") + check( + refused("api_Pair_non_zero_u16") { _ = try fartherHop(PairNonZeroU16(left: 0, right: 1)) }, + "a zero NonZero field throws" + ) targets() } From 1fe1700541c41566f4ec61c5d3e10d6b95c9afd2 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:18:56 -0800 Subject: [PATCH 19/28] fix(ffi)!: a NonZero's option and list are their own C structs Option> and Option shared moq_option_u64, so each binding generated one helper for both: with a NonZero's check, either it lost the check or it refused an Option holding zero, by which came first. A NonZero's key is now non_zero_. moq's C, C++, and Python programs follow the per-family address structs and the runtime's Duration. Co-Authored-By: Claude Opus 5.5 --- ffi/moq/moq.c | 18 +++++++++--------- ffi/moq/moq.cpp | 14 ++++++++------ ffi/moq/moq.py | 3 ++- ffi/src/abi.rs | 6 +++--- ffi/tests/api.c | 6 +++--- 5 files changed, 25 insertions(+), 22 deletions(-) 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.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/src/abi.rs b/ffi/src/abi.rs index d343aa51a..b46673f65 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -594,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); @@ -778,7 +776,9 @@ 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(), diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 7ee10435f..0809e3b62 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -485,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 { @@ -2381,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; } From da31d5356dcdfd278d8139a1775dd305a8f51366 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:28:04 -0800 Subject: [PATCH 20/28] docs(ffi): a NonZero's zero raises in every binding but C and C++ Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffi/README.md b/ffi/README.md index e7e725f25..fd18e7498 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -111,7 +111,7 @@ 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, which each binding but C++ checks first and raises, and `Option>` is `T`'s option | +| `NonZero` of an integer but 128-bit | `T`, in every binding too; a zero passed in aborts in C and C++, and every other binding checks it first and raises its 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` | From 50e52aeaeee6581d85ce3a223b77a057fc183fd4 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:48:52 -0800 Subject: [PATCH 21/28] fix(ffi): Kotlin's SocketAddrV6 refuses a scope java.net cannot hold Inet6Address takes its scope as an Int and ignores a negative one, so a scope past Int.MAX_VALUE converted to an unscoped address. Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/types.kt | 2 ++ ffi/tests/api.kt | 1 + 2 files changed, 3 insertions(+) diff --git a/ffi/runtime/types.kt b/ffi/runtime/types.kt index 3fb94e26e..9cb2778a2 100644 --- a/ffi/runtime/types.kt +++ b/ffi/runtime/types.kt @@ -177,6 +177,8 @@ class SocketAddrV6(val ip: Ipv6Addr, override val port: UShort, val flowinfo: UI 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()) diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 409bd85b2..257816a87 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -953,6 +953,7 @@ fun lossless() { 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") From a08590bd2d30afd2bd9175d91d5b778262cd110a Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 02:17:34 -0800 Subject: [PATCH 22/28] fix(ffi): answer the lossless review round - Python: a Duration Rust returns is made by tuple's own constructor, so CPython 3.14+ caches no zero hash and it finds its equal in a set. - Go: callbacks are given to Rust only once the arena's checks pass, so a failed check no longer leaks the callback's handle. - Dart: PathBuf.fromBytes takes the OS's bytes, refusing on Windows what is not WTF-8; Swift's PathBuf(bytes:) is public. - The transcript's unspellable path is a lone surrogate on Windows, where stray() is absent, and the C++, Python, Kotlin, and Dart tests build the same bytes per platform. - transcript.rs: ip_hex's doc is back on ip_hex. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/api/examples/transcript.rs | 18 ++++++++-- ffi/runtime/python.rs | 17 +++++----- ffi/runtime/rt.dart | 44 +++++++++++++++++++++++++ ffi/runtime/rt.swift | 3 +- ffi/src/go.rs | 12 ++++++- ffi/tests/api.cpp | 6 ++++ ffi/tests/api.dart | 8 ++++- ffi/tests/api.go | 17 ++++++++++ ffi/tests/api.kt | 7 +++- ffi/tests/api.py | 5 ++- ffi/tests/api.swift | 1 + 11 files changed, 123 insertions(+), 15 deletions(-) diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index b58fd923c..7e5798785 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -924,7 +924,7 @@ fn main() { .map(|c| format!("{:x}", u32::from(api::same_char(c)))) .collect(); println!("lossless char {}", chars.join(" ")); - let paths: Vec = [std::path::PathBuf::from("a/é"), api::stray()] + 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(); @@ -1065,12 +1065,25 @@ 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() } -/// An address's bytes in hex, so the length shows its family. /// 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 { @@ -1086,6 +1099,7 @@ fn socket_hex(addr: std::net::SocketAddr) -> String { } } +/// An address's bytes in hex, so the length shows its family. fn ip_hex(ip: std::net::IpAddr) -> String { match ip { std::net::IpAddr::V4(v4) => hex(&v4.octets()), diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index f07c98c8f..50821a85f 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -1150,16 +1150,17 @@ pub fn duration_in(obj: *mut PyObject) -> R { } } -/// The runtime's `Duration`, made as a tuple of its fields is, as its class is one. +/// 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 secs = uint_out(duration.secs)?; - let nanos = uint_out(duration.nanos.into())?; - // SAFETY: the GIL is held; a tuple class allocates its items, which each set takes. + 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 { - let obj = Obj::new(PyType_GenericAlloc(ty(T_DURATION).cast(), 2))?; - PyTuple_SetItem(obj.ptr(), 0, secs.into_ptr()); - PyTuple_SetItem(obj.ptr(), 1, nanos.into_ptr()); - Ok(obj) + 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())) } } diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 7ee6d839a..69086cf6f 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -118,6 +118,10 @@ final class PathBuf { /// `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: on Windows they must be WTF-8, else it throws an + /// [ArgumentError]. + PathBuf.fromBytes(List bytes) : _bytes = _osPath(bytes); + PathBuf._(this._bytes); /// Its bytes, which never change. @@ -389,6 +393,46 @@ final class SocketAddrV6 extends SocketAddr { static SocketAddrV6 _of(_SocketAddrV6 c) => SocketAddrV6._(Ipv6Addr._of(c.ip), c.port, c.flowinfo, c.scope_id); } +/// A copy of `bytes`, throwing an [ArgumentError] on Windows where they are not WTF-8. +typed.Uint8List _osPath(List 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) { diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index fec4ad24d..84b82af04 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -201,7 +201,8 @@ public struct PathBuf: Hashable, Sendable, Swift.ExpressibleByStringLiteral { /// that has a UTF-8 form. public let bytes: Bytes - init(bytes: Bytes) { + /// The path of the OS's `bytes`, which on Unix may be any. + public init(bytes: Bytes) { self.bytes = bytes } diff --git a/ffi/src/go.rs b/ffi/src/go.rs index e62706024..c299ca624 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -2487,6 +2487,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())); @@ -2541,7 +2543,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})")), @@ -2661,6 +2663,14 @@ impl<'a> Gen<'a> { if checked { body.push(format!("if a_.err != nil {{\n\t{}\n}}", fail("a_.err"))); } + // 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 &callbacks { + body.push(format!("l{i}_ := {}", args[i])); + args[i] = format!("l{i}_"); + } + } // Nothing from here fails before the call, which Rust's buffers given it go to. if arena { body.push("a_.made = true".to_string()); diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index c5e766d46..97e956fc0 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -1327,7 +1327,13 @@ int script() { 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(" "); diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 2d93776d4..a3e6b2969 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -819,7 +819,9 @@ Future script() async { 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(' ')}'); - print('lossless path ${[api.PathBuf('a/é'), api.stray()].map((p) => hex(api.samePath(p).bytes)).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( @@ -1507,6 +1509,10 @@ void bounds() { 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"); // An address is its family's, checked out of an InternetAddress. final loopback = InternetAddress('::1'); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index 96c89adf6..c8c53f703 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -1186,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) @@ -1891,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 257816a87..40953f52d 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -894,7 +894,12 @@ fun lossless() { 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) }}") - println("lossless path ${listOf(PathBuf("a/é"), PathBuf(byteArrayOf(-1))).joinToString(" ") { hex(samePath(it).toByteArray()) }}") + // 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)) }}") diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 3c5aba3de..87f3734a7 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -769,7 +769,9 @@ async def polls(): 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"])) - paths = ["a/é", os.fsdecode(b"\xff")] + # 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)) @@ -1454,6 +1456,7 @@ def raises(error, f, what): td = datetime.timedelta 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") diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 44cc8afde..ccaa0a56e 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -1554,6 +1554,7 @@ func lossless() { 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)) From 2f9c10db0bd868c5ac7bb3a34515f1b4e2bdb9c4 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 08:48:06 -0800 Subject: [PATCH 23/28] fix(ffi): Dart's PathBuf.fromBytes refuses what is not a byte Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.dart | 10 +++++++--- ffi/tests/api.dart | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index 69086cf6f..75094d8c1 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -118,8 +118,8 @@ final class PathBuf { /// `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: on Windows they must be WTF-8, else it throws an - /// [ArgumentError]. + /// 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); @@ -393,8 +393,12 @@ final class SocketAddrV6 extends SocketAddr { static SocketAddrV6 _of(_SocketAddrV6 c) => SocketAddrV6._(Ipv6Addr._of(c.ip), c.port, c.flowinfo, c.scope_id); } -/// A copy of `bytes`, throwing an [ArgumentError] on Windows where they are not WTF-8. +/// 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'); diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index a3e6b2969..5d013028b 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -1513,6 +1513,8 @@ void bounds() { 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'); From e71954ed396e37083619103673f7c72cb8e5cc8f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:06:23 -0800 Subject: [PATCH 24/28] feat(ffi)!: Swift's NonZero is the runtime's NonZero A NonZero crosses as the runtime's NonZero, whose throwing initializer refuses a zero, instead of T's integer checked at each call. No function taking one throws for it, and a record or enum holding one keeps Equatable, Hashable, and its description protocols, as moq's MoqQuicRate does. This reverts the Swift half of 790a22ebb. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 19 ++++---- ffi/runtime/rt.swift | 37 +++++++++----- ffi/src/swift.rs | 112 ++++++++++++++----------------------------- ffi/tests/api.swift | 22 ++++----- 4 files changed, 79 insertions(+), 111 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 8dda48353..01525c6fc 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -111,7 +111,7 @@ 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 in C and C++, and every other binding checks it first and raises its error. Its option and list are its own, `_option_non_zero_` beside `T`'s | +| `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` | @@ -815,22 +815,18 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u that is not `Sync` on a second thread, throws a `CheckError`, holding the function and the rule, before any Rust code runs, so the call changed nothing. So every function whose objects can fail a check `throws`, and a 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()`. A `NonZero` is - `T`'s Swift integer, and a zero passed for one, as an argument or anywhere in one, such - as an option's, an array's item, or a record's field, throws a `CheckError` too, so - every function taking one `throws`. Of a type whose checks can fail, or a record or enum - holding one or a `NonZero`, `description` and `debugDescription` + 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 `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 or a zero `NonZero` a closure or trait implementation returns, which has no caller - to throw to, traps instead. + 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, 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 or a `NonZero`, and `Default` a static `default()`. An **opaque value** is a struct holding its C + 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 `mutating`, and assigns `self` what Rust wrote back. - **Bytes** are the runtime's `Bytes`, a `RandomAccessCollection` of `UInt8` whose slices @@ -846,7 +842,10 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u 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. An `Err` is thrown: an exported type as itself, which conforms to + 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 diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 84b82af04..8da6add05 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -326,13 +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, an item called on a library whose target lacks it, or a value Rust's type -/// cannot hold, such as a zero passed for a `NonZero` or given a runtime type's initializer. +/// 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, or the record or enum whose value broke, by its C name, or the - /// runtime initializer. + /// The function called, by its C name, or the runtime initializer. public let function: Swift.String /// What broke. public let message: Swift.String @@ -639,16 +660,6 @@ extension Native { view(path.bytes) { copy($0) } } - /// `value`, a `NonZero`'s integer that `what` passes, throwing for a zero, which Rust's - /// type cannot hold. - @inline(__always) - static func nonzero(_ value: T, _ what: StaticString) throws -> T { - guard value != 0 else { - throw CheckError(function: "\(what)", message: "a NonZero is zero") - } - return value - } - static func scalar(_ code: UInt32) -> Unicode.Scalar { guard let scalar = Unicode.Scalar(code) else { fatalError("a char is not a Unicode scalar") diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 2308864fc..7e0e51716 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -61,7 +61,6 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result { fresh: usize, /// The targets of the item being written, whose calls check theirs. on: On, - /// The C name of the function or type whose values the lowerings being written pass, - /// which a zero passed for a `NonZero` throws naming. - what: String, } impl<'a> Gen<'a> { @@ -357,7 +354,8 @@ 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(), @@ -455,38 +453,11 @@ impl<'a> Gen<'a> { } /// Whether lowering a `ty` nested in another value can throw: it holds a class whose - /// value a move takes, closing its handle, or a `NonZero`, which may be zero. + /// value a move takes, closing its handle. fn throwing(&self, ty: &Type) -> bool { let mut handles = Vec::new(); self.abi.api.handles(ty, &mut handles, &mut Vec::new()); - handles.into_iter().any(|i| self.held(i)) || self.zeroable(ty, &mut BTreeSet::new()) - } - - /// Whether a `ty` lowers a `NonZero` anywhere, its records' and enums' fields included, - /// but an opaque record's, which only Rust makes. - fn zeroable(&self, ty: &Type, seen: &mut BTreeSet<(bool, usize)>) -> bool { - let api = self.abi.api; - match ty { - Type::NonZero(_) => true, - Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => { - self.zeroable(inner, seen) - } - Type::Tuple(elements) => elements.iter().any(|e| self.zeroable(e, seen)), - // A type already seen is being looked through, so it adds nothing new. - Type::Record(i) => { - let record = &api.records[*i]; - record.opaque.is_none() - && seen.insert((false, *i)) - && record.fields.iter().any(|f| self.zeroable(&f.ty, seen)) - } - Type::Enum(i) => { - seen.insert((true, *i)) - && (api.enums[*i].variants.iter()) - .flat_map(|v| &v.fields) - .any(|f| self.zeroable(&f.ty, seen)) - } - _ => false, - } + handles.into_iter().any(|i| self.held(i)) } /// Whether object `i` is a class a move may close, whose hold for a call can fail. @@ -496,11 +467,9 @@ impl<'a> Gen<'a> { /// `lowered`, a lowering in a function that cannot throw, trapping instead. fn trapping(lowered: String) -> String { - match lowered.strip_prefix("try ") { - // Swift refuses a `try` whose operand is another. - Some(rest) => format!("try! {rest}"), - None if lowered.contains("try ") => format!("try! {lowered}"), - None => lowered, + match lowered.contains("try ") { + true => format!("try! {lowered}"), + false => lowered, } } @@ -524,7 +493,7 @@ impl<'a> Gen<'a> { Type::Unit => unreachable!("no value is unit"), Type::Bool | Type::F32 | Type::F64 => e.to_string(), Type::Int(int) => lower_int(*int, e), - Type::NonZero(int) => format!("try Native.nonzero({}, \"{}\")", lower_int(*int, e), self.what), + 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"), @@ -628,7 +597,8 @@ 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)), @@ -880,10 +850,8 @@ impl<'a> Gen<'a> { let object = matches!(ty, Type::Object(_)); let unique = self.unique(ty); // One whose checks can fail throws, so conforms to no protocol of these, whose - // requirements cannot throw; any other's checks never fail. So does a value holding a - // `NonZero`, which may be zero, though Rust's function takes no status. + // requirements cannot throw; any other's checks never fail. let status = self.abi.traits_status_of(ty); - let checked = status || self.throwing(ty); // The statements lowering a value receiver for the function `what`, which on a target // the type is not on throws where it can. let value = |what: &str| { @@ -893,7 +861,7 @@ impl<'a> Gen<'a> { }; format!( "{}Native.check()\n\t\t{arena}", - self.on(self.on, what, "\n\t\t", checked) + self.on(self.on, what, "\n\t\t", status) ) }; let lower = match self.throwing(ty) { @@ -931,18 +899,18 @@ impl<'a> Gen<'a> { ), } }; - let throws = match checked { + let throws = match status { true => " throws", false => "", }; // A property's body, in a getter that throws if the call can. - let getter = |body: String| match checked { + let getter = |body: String| match status { true => format!("get throws {{\n\t\t\t{}\n\t\t}}", body.replace("\n\t\t", "\n\t\t\t")), false => body, }; let text = |r: &str| format!("Native.string({r})"); if t.display { - if !checked { + if !status { protocols.push("CustomStringConvertible"); } members.push("description".to_string()); @@ -952,7 +920,7 @@ impl<'a> Gen<'a> { )); } if t.debug { - if !checked { + if !status { protocols.push("CustomDebugStringConvertible"); } members.push("debugDescription".to_string()); @@ -971,7 +939,7 @@ impl<'a> Gen<'a> { } if t.eq { // `Hash` agrees with `PartialEq`, so a type with both hashes as Rust does. - if !checked { + if !status { protocols.push(match t.hash { true => "Hashable", false => "Equatable", @@ -1095,7 +1063,6 @@ impl<'a> Gen<'a> { } }) .collect(); - self.what = c.clone(); let lowered: Vec = (record.fields.iter().zip(&fields)) .map(|(f, (name, _))| { let value = self.lower(&f.ty, &format!("self.{name}")); @@ -1164,7 +1131,6 @@ impl<'a> Gen<'a> { let e = &abi.api.enums[i]; self.on = e.on; let c = abi.enumeration(i); - self.what = c.clone(); let name = self.class(&c); let rust = abi.api.rust(&e.path, &e.args); let ty = Type::Enum(i); @@ -1328,7 +1294,6 @@ impl<'a> Gen<'a> { let callback = &abi.api.callbacks[i]; self.on = callback.on; let c = abi.callback(i); - self.what = c.clone(); let name = self.class(&c); let rust = callback.path.join("::"); let mut body = Vec::new(); @@ -1650,7 +1615,6 @@ impl<'a> Gen<'a> { let abi = self.abi; let export = abi.export(function, owner); let c_name = export.call.name.clone(); - self.what = c_name.clone(); let owner_c = match owner { Some(Ty::Struct(n) | Ty::Enum(n) | Ty::Handle { name: n, .. }) => Some(n.clone()), _ => None, @@ -1724,8 +1688,7 @@ impl<'a> Gen<'a> { } _ => { arena |= self.arena(&ty); - arg.throws = self.throwing(&ty); - let t = match arg.throws { + let t = match self.throwing(&ty) { true => "try ", false => "", }; @@ -1782,8 +1745,6 @@ impl<'a> Gen<'a> { arena |= needs; args.push(arg); } - // A `NonZero` passed, which may be zero, throws as a failed check does. - let throws = throws || args.iter().any(|a| a.throws); let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); // What runs once the call returns, gated on its checks passing when it can fail one: // a failed check, or an argument whose lowering throws, leaves every argument as it @@ -1797,25 +1758,6 @@ impl<'a> Gen<'a> { true => format!("defer {{ if c_ran_ {{ {stmts} }} }}"), false => format!("defer {{ {stmts} }}"), }; - if arena { - prelude.insert( - 1.min(prelude.len()), - format!("let a_ = Native.Arena(\"{c_name}\")\ndefer {{ a_.finish() }}"), - ); - // Past the receiver, which Rust only reads, Rust takes each value passed. - prelude.push("a_.take = true".to_string()); - } - // A lowering that throws runs before any box or buffer is given, which the call - // alone would take: a throw between them would leak it. One in a call that fails no - // check, a `NonZero`'s, runs before any argument's end is deferred too, which only a - // check gates. - if !status || args.iter().any(|a| a.gives || a.takes) { - let receiver = usize::from(receiver.is_some()); - for (k, arg) in args.iter_mut().enumerate().skip(receiver).filter(|(_, a)| a.throws) { - prelude.push(format!("let c_arg{k}_ = {}", arg.exprs[0])); - arg.exprs[0] = format!("c_arg{k}_"); - } - } // Each value passed for `&mut` takes what Rust wrote back, once the call's own // access to it ends, on every path the call ran, a throw included. let mut written = Vec::new(); @@ -1830,10 +1772,26 @@ impl<'a> Gen<'a> { prelude.push(gate(&format!("{name} = {lifted}"))); written.push(out); } + if arena { + prelude.insert( + 1.min(prelude.len()), + format!("let a_ = Native.Arena(\"{c_name}\")\ndefer {{ a_.finish() }}"), + ); + // Past the receiver, which Rust only reads, Rust takes each value passed. + prelude.push("a_.take = true".to_string()); + } let after: Vec = args.iter().flat_map(|a| a.after.clone()).collect(); if !after.is_empty() { prelude.push(gate(&after.join("; "))); } + // A lowering that throws runs before any box or buffer is given, which the call + // alone would take: a throw between them would leak it. + if args.iter().any(|a| a.gives || a.takes) { + for (k, arg) in args.iter_mut().enumerate().filter(|(_, a)| a.throws) { + prelude.push(format!("let c_arg{k}_ = {}", arg.exprs[0])); + arg.exprs[0] = format!("c_arg{k}_"); + } + } let exprs: Vec = args.iter().flat_map(|a| a.exprs.clone()).collect(); // A tuple of one returned whole is its element, written through its one out pointer. diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index ccaa0a56e..31f8460aa 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -1013,18 +1013,18 @@ func script() { } catch { check(false, "seekTo throws its io::Error's message") } - print("non zeros \(try! describeNonZeros(nonZeros()))") + print("non zeros \(describeNonZeros(nonZeros()))") for hop in [nil, 1, UInt64.max] as [UInt64?] { - print("hop \(try! nextHop(hop).map { "Some(\($0))" } ?? "None")") - } - print("farther hop \(try! fartherHop(PairNonZeroU16(left: 3, right: .max)))") - // A zero passed for a `NonZero`, which Swift's integer holds and Rust's type does not, - // throws before any Rust runs, in an option and a record's field too. - check(refused("api_next_hop") { _ = try nextHop(0) }, "a zero NonZero throws") - check( - refused("api_Pair_non_zero_u16") { _ = try fartherHop(PairNonZeroU16(left: 0, right: 1)) }, - "a zero NonZero field throws" - ) + 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() } From 4244c0c89a8244ed4782da673262a84b60e84176 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:09:01 -0800 Subject: [PATCH 25/28] test(ffi): the runtime-names check reads a generic Swift type's name Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.swift | 2 +- ffi/src/swift.rs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 8da6add05..d96d1dae8 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -327,7 +327,7 @@ public struct U128: Hashable, Comparable, Sendable, CustomStringConvertible { } /// Rust's `NonZero`: an integer that is never zero. -public struct NonZero: Hashable, Comparable, Sendable, CustomStringConvertible { +public struct NonZero: Hashable, Comparable, Sendable, CustomStringConvertible { /// The integer, never zero, as Rust's `get`. public let value: T diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 7e0e51716..e4cbcdf80 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -2461,7 +2461,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('.')) { From eb97f4e19477f6a31ca43ae883b8492f390f150b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:22:23 -0800 Subject: [PATCH 26/28] fix(ffi): Dart reserves the core names its runtime uses unqualified Comparable, which RustDuration implements, and Expando, which lends bytes, are dart:core's names an exported type could shadow. A test now checks every unqualified type the runtime names is its own or reserved. Co-Authored-By: Claude Opus 5.5 --- ffi/src/dart.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 1dacdcd58..220ef65fd 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -329,8 +329,10 @@ const TAKEN: &[&str] = &[ "BigInt", "Cancelable", "Cancelled", + "Comparable", "Duration", "Exception", + "Expando", "FormatException", "Function", "Future", @@ -650,13 +652,11 @@ impl<'a> Gen<'a> { !matches!( ty, Ty::Buf - | Ty::U128 - | Ty::Duration + | Ty::U128 | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) - | Ty::Waker - | Ty::ForeignFuture + | Ty::Waker | Ty::ForeignFuture | Ty::ForeignReader ) } @@ -2740,3 +2740,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"); + } + } + } +} From a07916c8c6b23c7c517dc7636c0d6b6e91a77d80 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:22:43 -0800 Subject: [PATCH 27/28] style(ffi): rustfmt dart.rs Co-Authored-By: Claude Opus 5.5 --- ffi/src/dart.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 220ef65fd..4b1c16605 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -652,11 +652,13 @@ impl<'a> Gen<'a> { !matches!( ty, Ty::Buf - | Ty::U128 | Ty::Duration + | Ty::U128 + | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) - | Ty::Waker | Ty::ForeignFuture + | Ty::Waker + | Ty::ForeignFuture | Ty::ForeignReader ) } From 8f0f355b2443b669c107a203054eea2a8c628194 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:41:34 -0800 Subject: [PATCH 28/28] docs(ffi): a Go path that is no Windows path fails the call too Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 01525c6fc..7a81c65e6 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1002,8 +1002,9 @@ reads a go.mod. `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, - 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`, `Equal` of a 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