abi-vtable-macro
#[abi_vtable] — annotate a Rust trait and get the complete C contract for
dyn-loader's abi mode:
a #[repr(C)] vtable struct, one extern "C" thunk per method, a static
vtable instance, a #[no_mangle] getter — and a host-side safe wrapper
(CalcHost) with one safe method per trait method — all generated.
use abi_vtable;
;
// One line: static instance + thunks + vtable + getter.
abi_vtable_impl_calc!;
The host (Rust, C, C++, Zig, ...) loads calc_get_vtable and calls through
the struct of function pointers — see dyn-loader's AbiTable. A Rust host
that declares the same trait with the same #[abi_vtable] attribute also
gets the generated CalcHost wrapper and calls it safely:
let module = unsafe ;
let host = new;
let sum = host.add; // safe — no unsafe block needed
Rules
- Field order of the generated vtable is the protocol: it follows trait method declaration order. Never reorder after release.
- All fn pointers are
extern "C"(cdecl), ctx-leading (*mut c_voidfirst). $instancemust be const-constructible (unit struct,const fn, literal).
License
MIT