Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1244,15 +1244,19 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass

- **Names** are the C names without the prefix: a type in Pascal case and a member in
camel case, so `moq_track_Producer` is `TrackProducer`, and its method
`moq_track_Producer_append_group` is `TrackProducer.appendGroup`, and a constant in
upper case (`LIMIT`). A function of a type
`moq_track_Producer_append_group` is `TrackProducer.appendGroup`, and a constant too
(`limit`), as Dart's lints name one. A function of a type
that returns it is a constructor, which may share its name with a field: an object's
`new` is its unnamed one (`Counter('c', 1)`), and any other a named one
(`Position.group`). A type's other functions are static members. Two types whose
names would match are kept apart as Kotlin's are (`TokioConnect_Error`). A reserved word takes
a trailing `_` (`Point.default_()`), and names that clash, such as one the runtime
declares or a data enum's variant class, are kept apart as Kotlin's are. The glue's C
names are `<prefix>_dart_`, with a `_` more for each C name of the export already there.
names would match are kept apart by a number (`Cancelled2`), as a type's name holds no
`_`, which Dart's lints refuse. A reserved word takes a trailing `_` (`Point.default_()`),
and any other member that clashes takes a trailing `_` (`foo_`) when that is free, else a number then a `_` (`default2_`); names that clash,
such as one the runtime declares or a data enum's variant class, are kept apart the same
way. The glue's C names are `<prefix>_dart_`, with a `_` more for each C name of the
export already there. The generated file passes `dart analyze --fatal-infos` with
`package:lints/core.yaml`, the lints pub.dev scores a package with ([`tests/dart_lints.yaml`](tests/dart_lints.yaml)),
and no `ignore_for_file`.
- **Objects** are final classes owning their handle. Of a checked type, `close()` drops
the value through Rust's own `_drop`, as Rust's `drop` would, and frees the handle in
the same crossing, and a `close()` while a call borrows the value, from a closure that
Expand Down
13 changes: 13 additions & 0 deletions ffi/fixtures/edge/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "edge-fixture"
description = "A small API, whose generated Dart has no use for most of its runtime"
version = "0.0.0"
edition = "2024"
license = "MIT OR Apache-2.0"
publish = false

[lib]
name = "edge"

[lints]
workspace = true
20 changes: 20 additions & 0 deletions ffi/fixtures/edge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! A small API of shapes whose Dart leaves part of the generator's output unused, each of which
//! Dart's analyzer would report once nothing refers to it.

/// An array taken and never returned, so Dart never reads one.
pub fn sum(bytes: [u8; 32]) -> u32 {
bytes.iter().map(|b| u32::from(*b)).sum()
}

/// An array returned and never taken, so Dart never writes one.
pub fn zeros() -> [u8; 4] {
[0; 4]
}

/// A record only on one target, whose only items are the checks of its derived `Debug`: the
/// target check is no function's.
#[cfg(target_arch = "wasm32")]
#[derive(Debug)]
pub struct Wasm {
pub n: u32,
}
6 changes: 6 additions & 0 deletions ffi/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ dart-program dir main:
printf '{"configVersion": 2, "packages": [{"name": "moq", "rootUri": "file://%s/dart/", "packageUri": "./"}]}\n' "$out" > "$dir/.dart_tool/package_config.json"
printf 'format-version: [1, 0, 0]\nnative-assets:\n %s_%s:\n "package:moq/moq.dart": [absolute, "%s"]\n' "{{ os() }}" "$arch" "$out/target/release/$lib" > "$dir/native_assets.yaml"
dart analyze --fatal-infos "$dir"
# pub.dev scores the library with the lints in tests/dart_lints.yaml, which the analyzer reads from the directory it analyzes.
rm -rf "$dir-lint"
mkdir "$dir-lint"
cp "$out"/dart/*.dart "$dir-lint"
cp tests/dart_lints.yaml "$dir-lint/analysis_options.yaml"
dart analyze --fatal-infos "$dir-lint"
sdk=$(dirname "$(dirname "$(realpath "$(command -v dart)")")")
"$sdk/bin/dartaotruntime" "$sdk/bin/snapshots/gen_kernel_aot.dart.snapshot" --platform "$sdk/lib/_internal/vm_platform_strong.dill" \
--packages "$dir/.dart_tool/package_config.json" --native-assets "$dir/native_assets.yaml" -o "$dir/main.dill" "$dir/main.dart"
Expand Down
2 changes: 1 addition & 1 deletion ffi/moq/bench/export.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Future<Span> throughput(moq.TrackProducer track, moq.TrackSubscriber subscriber,
Future<Map<String, Span>> round() async {
final out = <String, Span>{};
final broadcast = moq.BroadcastInfo().produce();
final track = broadcast.createTrack('bench', moq.TrackInfo.default_().withTimescale(moq.Timescale.MICRO));
final track = broadcast.createTrack('bench', moq.TrackInfo.default_().withTimescale(moq.Timescale.micro));
final frame = Uint8List(100);
micros = 0;

Expand Down
4 changes: 2 additions & 2 deletions ffi/moq/moq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ Future<void> catalog() async {
// a JSON document, and a section Dart parses from text, set in the lent catalog and read
// back as text.
final status = producer.jsonSnapshotConfig(
publisher.createTrack('status', moq.TrackInfo.default_().withTimescale(moq.Timescale.MILLI)),
publisher.createTrack('status', moq.TrackInfo.default_().withTimescale(moq.Timescale.milli)),
moq.MuxJsonConfig.default_(),
moq.MuxJsonOptions.default_(),
);
Expand Down Expand Up @@ -310,7 +310,7 @@ void config() {
client.close();
url.close();
// A `Vec<String>`, as a list.
expect(moq.ALPNS.isNotEmpty, 'a constant slice crosses as a list');
expect(moq.alpns.isNotEmpty, 'a constant slice crosses as a list');
final alpns = versions.alpns();
expect(alpns.isNotEmpty && alpns.every((a) => a.isNotEmpty), 'each version has an ALPN');
versions.close();
Expand Down
17 changes: 14 additions & 3 deletions ffi/runtime/rt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ final class RustDuration implements Comparable<RustDuration> {
@override
String toString() => '${secs.toRadixString(10)}.${nanos.toString().padLeft(9, '0')}s';

// ignore: unused_element
void _put(_Duration c) {
c.secs = secs.bits;
c.nanos = nanos;
}

// ignore: unused_element
static RustDuration _of(_Duration c) => RustDuration._(U64.fromBits(c.secs), c.nanos);
}

Expand All @@ -122,6 +124,7 @@ final class PathBuf {
/// byte or, on Windows, they are not WTF-8.
PathBuf.fromBytes(List<int> bytes) : _bytes = _osPath(bytes);

// ignore: unused_element
PathBuf._(this._bytes);

/// Its bytes, which never change.
Expand Down Expand Up @@ -167,6 +170,7 @@ sealed class IpAddr {
@override
String toString() => toInternetAddress().address;

// ignore: unused_element
static void _put(_IpAddr c, IpAddr v) {
switch (v) {
case Ipv4Addr():
Expand All @@ -178,6 +182,7 @@ sealed class IpAddr {
}
}

// ignore: unused_element
static IpAddr _of(_IpAddr c) => c.v6 ? Ipv6Addr._of(c.ip.v6) : Ipv4Addr._of(c.ip.v4);
}

Expand Down Expand Up @@ -303,6 +308,7 @@ sealed class SocketAddr {
IpAddr get ip;
int get port;

// ignore: unused_element
static void _put(_SocketAddr c, SocketAddr v) {
switch (v) {
case SocketAddrV4():
Expand All @@ -314,6 +320,7 @@ sealed class SocketAddr {
}
}

// ignore: unused_element
static SocketAddr _of(_SocketAddr c) => c.v6 ? SocketAddrV6._of(c.addr.v6) : SocketAddrV4._of(c.addr.v4);
}

Expand Down Expand Up @@ -387,10 +394,10 @@ final class SocketAddrV6 extends SocketAddr {
ip._put(c.ip);
c.port = port;
c.flowinfo = flowinfo;
c.scope_id = scopeId;
c.scopeId = scopeId;
}

static SocketAddrV6 _of(_SocketAddrV6 c) => SocketAddrV6._(Ipv6Addr._of(c.ip), c.port, c.flowinfo, c.scope_id);
static SocketAddrV6 _of(_SocketAddrV6 c) => SocketAddrV6._(Ipv6Addr._of(c.ip), c.port, c.flowinfo, c.scopeId);
}

/// A copy of `bytes`, throwing an [ArgumentError] where one is not a byte or, on Windows,
Expand Down Expand Up @@ -484,6 +491,7 @@ extension type Cancelable<T>._(Future<T> _future) implements Future<T> {

/// The future of `call`, whose start left the future in Rust that pended: polled again
/// here with a waker, which its first poll had no use for.
// ignore: unused_element
Cancelable<T> _call<T>(_Call<T> call) {
final future = call._done.future;
_n.calls[future] = call;
Expand Down Expand Up @@ -534,6 +542,7 @@ final class _Call<T> {
}

/// Complete it with what Rust returned, from its poll.
// ignore: unused_element
void _ready(T value) => _done.complete(value);

/// Complete it with the error Rust returned, from its poll.
Expand Down Expand Up @@ -618,7 +627,7 @@ final class _SocketAddrV6 extends ffi.Struct {
@ffi.Uint32()
external int flowinfo;
@ffi.Uint32()
external int scope_id;
external int scopeId;
}

final class _Socket extends ffi.Union {
Expand Down Expand Up @@ -802,6 +811,7 @@ abstract base class _Object implements ffi.Finalizable {

/// The garbage collector's hold of an owned one's handle, from when the nursery pushed it
/// out until freed.
// ignore: unused_field
ffi.Pointer<ffi.Void> _own = ffi.nullptr;

/// Its slot in the nursery while there, else -1.
Expand Down Expand Up @@ -829,6 +839,7 @@ abstract base class _Object implements ffi.Finalizable {
}

/// The handle for a leaf call, which needs no hold: no Dart runs to close it meanwhile.
// ignore: unused_element
ffi.Pointer<ffi.Void> get _live {
if (_closed) throw StateError('$_type is closed');
return _raw;
Expand Down
Loading