Ivan FB 88c7c8b8b7
feat(codegen): native typed events for the Rust generator
Adds native (zero-CBOR) event support to the Rust generator, mirroring the
cpp_native / Go event path: a per-event `add_<wire>_listener` registrar takes a
closure, boxes it, and registers it through the bare `<lib>_add_event_listener`
(native) entry point. The extern "C" trampoline reads the payload as the raw
C-POD struct and hands the consumer a borrowed idiomatic value via from_c — no
serialization on the hot path.

The node owns the boxed closures in a Mutex<HashMap<id, Box<dyn Any>>> keyed by
listener id so they outlive the call, and `remove_event_listener` drops them and
calls the bare remove entry point. Event externs are only emitted when the
library declares events, so event-free crates stay minimal.

Verified end-to-end: the demo registers a listener, echo fires on_echo_fired
inline, the typed EchoEvent reaches the closure, and removal returns true.

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

1.8 KiB

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.