| Crates.io | ffi_trait |
| lib.rs | ffi_trait |
| version | 1.2.0 |
| created_at | 2024-07-21 05:21:24.800167+00 |
| updated_at | 2025-06-01 09:21:48.60538+00 |
| description | FFI-safe trait vtables |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1310119 |
| size | 8,796 |
ffi_trait
This crate has an vtable_for_trait! macro that will generate an FFI-safe vtable for the trait that you declare inside it
vtable_for_trait! {
pub trait Foo {
fn foo(&self, x: i32) -> f32;
fn bar(&mut self);
}
pub vtable FooVtable;
}
will turn into something like
pub trait Foo {
fn foo(&self, x: i32) -> f32;
fn bar(&mut self);
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct FooVtable {
pub drop_vtable: DropVtable,
pub foo: unsafe extern "C" fn(NonNull<()>, i32) -> f32,
pub bar: unsafe extern "C" fn(NonNull<()>),
}
unsafe impl<T: Foo> VtableFor<T> for FooVtable {
const VTABLE: &'static Self = {
// extern "C" compatible thunks
&Self {
drop_vtable: *<DropVtable as VtableFor<T>>::VTABLE,
// methods
}
};
}
impl<'a> Foo for RefDyn<'a, FooVtable> {
// methods
}
impl<'a> Foo for OwnDyn<'a, FooVtable> {
// methods
}