Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 22 additions & 8 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,13 @@ the rest cross encoded.
name, as does a type named as the runtime or a generated class. Any other top-level name,
such as a function's or an exception's, takes a trailing `_` where a type, the runtime, or
an earlier name has it. A keyword is quoted in backticks, and a member named as one Kotlin
gives its type takes a trailing `_`. A constructor such as `new` stays a companion function: `Counter.new(1)`.
gives its type takes a trailing `_`. Names of one scope that Kotlin spells alike, such as
the fields `new` and `new_` in camel case, are kept apart in any order, as C++'s are: one
spelled as its Rust name keeps it, and the rest take a trailing `_` until free, in Rust's
name order (`new_` and `new`). A companion's functions are a scope apart from the
instance's. A call's parameters are kept apart the same way, but by a number, since the
call's own locals end in `_`: Rust's `(new_, new)` are `(new2, new)`, so a named argument
reads as Rust's. A constructor such as `new` stays a companion function: `Counter.new(1)`.
- **Objects** are classes owning their handle, `AutoCloseable`, which each call holds
until it returns, so the handle is freed only once no call holds it; a call after
`close()` throws. Of a checked type, `close()` drops the value through Rust's own
Expand Down Expand Up @@ -869,7 +875,8 @@ caller imports it with no ignores.
snake_case without their type's name (`moq.TrackProducer.append_group`), and two types
whose names would match kept apart as Kotlin's are (`TokioConnect_Error`), as are
top-level names that clash. A keyword, a member Python gives every such class, or a
method named as a field takes a trailing `_`. A member or top-level name may be a
method named as a field takes a trailing `_`, as does a field named as a member a trait
gives its type (`default_` beside `Default`'s `default()`). A member or top-level name may be a
builtin's (`bytes`, `int`): the module reads each builtin through an alias (`_bytes`).
Constructors such as `new` stay static methods: `Counter.new(1)`. The module's `__all__`
lists every export, so `from moq import *` reaches one whose Rust name starts with `_`
Expand Down Expand Up @@ -990,8 +997,10 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u
- **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
keyword is quoted in backticks, and a member named as one Swift gives its type, such as
`init`, or as a field or member before it, takes a trailing `_`. Names that clash are
kept apart as Kotlin's are. Parameters have no labels but an optional borrowed object's,
`init`, or as a field, takes a trailing `_`, as does a record's field named as a member
`Display` or `Debug` gives it (`description_`). Names that clash are kept apart as Kotlin's
are, in any order, a method by its name with its labels: Rust's `delete()` and
`delete_(a, b)` are `delete()` and `delete(_:_:)`. Parameters have no labels but an optional borrowed object's,
so a call reads as Rust's, and a
constructor such as `new` stays a static function: `Counter.new(1)`.
- **Objects** are noncopyable structs (`~Copyable`) owning their handle, freed in `deinit`,
Expand Down Expand Up @@ -1182,10 +1191,12 @@ other than one in its own form, `module <path>` and `go 1.24`.
returns that type, or an `Option` of it (moq's `clock::interval` is
`kio.NewClockInterval`), and otherwise takes a trailing `_` (`mux::codec::opus::config`
is `mux.CodecOpusConfig_`).
A method named `Close` or `Error`, or as a field or a method before it, and a parameter
A method named `Close` or `Error`, or as a field, and a parameter
named as a Go keyword, a predeclared name, or a package the code uses, take a trailing
`_` too, as does a variant's or error's name or any other in a package that another
there, or the runtime's, takes.
there, or the runtime's, takes. Names of one scope that Pascal case spells alike, such
as the fields `new` and `new_`, are kept apart as Kotlin's are, in any order: `New` and
`New_`.
- **Objects** are pointers to structs owning their handle, each an `io.Closer`, which
each call holds until it returns, so the handle is freed only once no call holds it. Of
a checked type, `Close()` drops the value through Rust's own `_drop`, as Rust's `drop`
Expand Down Expand Up @@ -1338,7 +1349,8 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass
(`Position.group`). A type's other functions are static members. Two types whose
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,
and any other member that clashes takes a trailing `_` (`foo_`) when that is free, else a number then a `_` (`default2_`),
in any order, as Kotlin's do: one spelled as its Rust name keeps it; 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
Expand Down Expand Up @@ -1570,7 +1582,9 @@ packs the api fixture's with npm, installs it into a project of its own, type-ch
and `load`. Names are the C names without the prefix: a type in Pascal case, and a
function in camel case, a constant's in upper case; a reserved word is exported as is
from a local with a trailing `$`, so `Counter.new("c", 1)` and `Point.default()` work
through the module's namespace.
through the module's namespace. Names of one scope that camel case spells alike are kept
apart as Kotlin's are, in any order: functions `erase_` and `erase` are `erase_` and
`erase`, and fields `new_` and `new`, as no field takes a trailing `_`, are `new2` and `new`.
- **Values**: a record is a plain object of its fields in camel case, positional ones `_0`,
`_1`, and so on; an opaque value an `rt.Opaque`, its bytes, which only Rust makes. A plain
enum is a union of its variants' names, and an enum with data a union of objects tagged
Expand Down
166 changes: 161 additions & 5 deletions ffi/fixtures/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5095,6 +5095,27 @@ pub mod clash {

/// Nothing, named and typed as the method a JVM finalizer overrides.
pub fn finalize(&self) {}

/// Seven, by a static function that camel case names as the method `count`.
pub fn count_() -> u32 {
7
}

/// The count, by a method that camel case names as the static `count_`.
pub fn count(&self) -> u32 {
self.count
}

/// A store of five, by a constructor that camel case names as the method `open`.
pub fn open_() -> Store {
Store { count: 5 }
}

/// The count plus two, wrapping past `u32::MAX`, by a method that camel case names as
/// the constructor `open_`.
pub fn open(&self) -> u32 {
self.count.wrapping_add(2)
}
}

/// A level, whose values are named as the members `Debug` and `Default` give a type, as
Expand Down Expand Up @@ -5152,6 +5173,25 @@ pub mod clash {
}
}

/// Digits whose fields are named as the members Swift's protocols for `Display` and `Debug`
/// give a type.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Described {
pub description: u8,
pub debug_description: u8,
}

impl std::fmt::Display for Described {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.description, self.debug_description)
}
}

/// `described`'s fields as decimal digits, from `description` up.
pub fn described(described: Described) -> u32 {
u32::from(described.description) * 10 + u32::from(described.debug_description)
}

/// Bytes read one at a time, whose methods go vet reads as `io.ByteScanner`'s, as
/// untrusted's `Reader::read_byte` is.
pub struct Reader {
Expand Down Expand Up @@ -5216,22 +5256,132 @@ pub mod clash {
}
}

/// A byte, whose methods `delete` and `delete_` C++ escapes alike, as `delete_`.
/// A byte, whose methods `delete_` and `delete` C++ escapes alike, as `delete_`, and the
/// rest name alike where their case drops the `_`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Escaped {
pub new: u8,
}

impl Escaped {
/// `new` plus the arguments, by parameters C++ escapes alike.
pub fn delete_(&self, new: u8, new_: u8) -> u8 {
self.new + new * 10 + new_ * 100
}

/// `new`, by a method C++ escapes as `delete_`.
pub fn delete(&self) -> u8 {
self.new
}
}

/// `new` plus the arguments, by parameters C++ escapes alike.
pub fn delete_(&self, new: u8, new_: u8) -> u8 {
self.new + new * 10 + new_ * 100
}
/// Digits whose fields differ only by trailing underscores, which camel case drops, and
/// are named as the members `Debug` and `Default` give a type.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Underscored {
pub new_: u8,
pub new: u8,
pub debug: u8,
pub default: u8,
}

/// An edit, whose values differ only by a trailing underscore, which Swift's case drops.
#[allow(non_camel_case_types, reason = "the clash under test")]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Op {
Delete_,
Delete,
}

/// A change, whose payload's fields differ only by a trailing underscore.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Change {
Set { new_: u8, new: u8 },
Clear,
}

/// `underscored`'s fields as decimal digits, from `new_` up, after `op`'s and `change`'s,
/// to show each crossed to its own place.
pub fn underscored(underscored: Underscored, op: Op, change: Change) -> u32 {
let Underscored {
new_,
new,
debug,
default,
} = underscored;
let change = match change {
Change::Set { new_, new } => u32::from(new_) * 10 + u32::from(new),
Change::Clear => 0,
};
let digits = [new_, new, debug, default]
.iter()
.fold(0, |n, &d| n * 10 + u32::from(d));
(op as u32 + 1) * 1_000_000 + change * 10_000 + digits
}

/// The underscored record with each field one more, wrapping past 255, and the change with
/// each swapped.
pub fn underscored_next(underscored: Underscored, change: Change) -> (Underscored, Change) {
let Underscored {
new_,
new,
debug,
default,
} = underscored;
let next = Underscored {
new_: new_.wrapping_add(1),
new: new.wrapping_add(1),
debug: debug.wrapping_add(1),
default: default.wrapping_add(1),
};
let change = match change {
Change::Set { new_, new } => Change::Set { new_: new, new: new_ },
Change::Clear => Change::Clear,
};
(next, change)
}

/// An eraser, implemented in each binding, whose methods and parameters differ only by
/// trailing underscores.
pub trait Eraser {
/// The arguments as decimal digits.
fn delete_(&self, new_: u8, new: u8) -> u8;

/// One.
fn delete(&self) -> u8;
}

/// `eraser`'s `delete_(1, 2)`, then its `delete()`, as decimal digits.
pub fn erased(eraser: impl Eraser) -> u32 {
u32::from(eraser.delete_(1, 2)) * 10 + u32::from(eraser.delete())
}

/// Two, by a name that differs only by a trailing underscore from `erase`'s.
pub fn erase_() -> u8 {
2
}

/// One.
pub fn erase() -> u8 {
1
}

/// One more than the store's count, or none without one, by a name that differs only by
/// a trailing underscore from `lend`'s: Swift's overload leaving out `store` would be
/// `lend()` too. The sum saturates at `u32::MAX`.
pub fn lend_(store: Option<&Store>) -> u32 {
store.map_or(0, |store| store.count.saturating_add(1))
}

/// Nine.
pub fn lend() -> u32 {
9
}

/// Ten times what `work_` returns, plus `work`, by parameters that differ only by a
/// trailing underscore, the closure's first.
pub fn run(work_: impl Fn() -> u8, work: u8) -> u32 {
u32::from(work_()) * 10 + u32::from(work)
}

/// The tone, or no tones where it is silent: an error Go names as `VecClashTone`'s variant.
Expand Down Expand Up @@ -5615,6 +5765,12 @@ pub fn __all__(note: u8) -> u8 {
note + 2
}

/// Three more than `note`, wrapping past 255, by the name Python would give `__all__`, which
/// this one keeps.
pub fn __all___(note: u8) -> u8 {
note.wrapping_add(3)
}

/// A dial, whose docs carry each rustdoc form a binding renders as its own: links to a
/// method [`Self::turn`], a field [`Dial::level`], a variant [`Error::Overflow`], a
/// function [`advance`], a constant [`LIMIT`], an object [`Counter`], a callback's method
Expand Down
2 changes: 1 addition & 1 deletion ffi/fixtures/lock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub fn smoothStep() -> u32 {
2
}

/// Kept function whose camel name is claimed by the added function first.
/// Kept function, which moves past the added `smoothStep` in camel case, in Rust's name order.
pub fn smooth_step() -> u32 {
1
}
Expand Down
Loading