nim-ffi/examples/timer/rust_native_bindings
Ivan FB 7a5ab67831
refactor(codegen): reconcile Rust crate names with the native/cbor convention
Aligns the Rust crate names with the C generator and the symbol naming: the
native (zero-serialization, same-process) crate is the bare `<lib>` and the
CBOR (inter-process) crate carries the `_cbor` suffix — mirroring `<lib>.h` /
`<lib>_cbor.h` and the `<name>` / `<name>_cbor` exports.

Previously the native crate was `<lib>_native` and the CBOR crate was the bare
`<lib>`, which is backwards from the symbol convention. Only the Cargo package
name changes; the linked dylib stays `lib<lib>.dylib` (the `#[link]` name and
build.rs are untouched).

Consumers updated: rust_client depends on `my_timer_cbor` and imports from it;
the native demo imports from the bare `my_timer`.

Validated: rust_client builds against the renamed CBOR crate; the native demo
round-trips version / echo / event / complex / schedule against the bare crate;
check_bindings_rust regen is deterministic.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 19:38:35 +02:00
..

Rust bindings — native (generated)

Generated native (zero-serialization) Rust crate for the timer library — the Rust counterpart of c_bindings / go_bindings / cpp_native_bindings, and the native sibling of the CBOR crate in ../rust_bindings.

File Description
src/ffi.rs #[repr(C)] POD mirrors + extern "C" native entry points.
src/types.rs Idiomatic structs + to_c/from_c (a holder owns the CStrings for the call).
src/api.rs <Lib>Node — methods marshal typed args in / read typed struct returns out; blocking via std::sync::mpsc. No CBOR.
examples/demo.rs A small consumer.
let node = MyTimerNode::new(TimerConfig { name: "my-app".into() })?;
println!("{}", node.version()?);
let r = node.echo(EchoRequest { message: "hello".into(), delay_ms: 5 })?;   // -> EchoResponse

Regenerate with nimble genbindings_rust_native.

Status

Requests are fully supported: scalar / string / bool / float / nested struct and now sequences (Vec) and optionals (Option) — create, version, echo, complex, schedule all generate and round-trip typed values. to_c returns a holder that owns the CStrings and C-array backing (heap, so the C struct's raw pointers stay valid across the move and for the call).

Native typed events are supported too: add_<event>_listener takes a closure receiving the payload as a borrowed idiomatic struct (the trampoline reads the raw C-POD directly — no CBOR), the handle goes through remove_event_listener, and the node owns the boxed closures for their lifetime.

Still to come: the native-bare / _cbor filename reconciliation. Linking is left to the consumer (-L <dir> -l my_timer + rpath, as in examples/demo.rs); a build.rs that compiles the dylib can be added later.