FFI
This crate helps expose Rust code via FFI to other languages, particularly C, by
providing an attribute #[ffi]
which automatically implements call forwarding from
C-compatible function pointers to Rust impl
methods.
This is particularly helpful when writing a shared libary in Rust which may be
dlopen
-ed by a C program like QEMU.
Example
In this example, imagine run_callback
is in a C library you don't own. However, you
want to use the common userdata
parameter of callback-registering functions to call
through to your struct instance. ffi
allows you to export your impl
's methods as
C FFI compatible functions so you can easily call back and forth between languages.
ffi
makes no assumptions about the parameter ordering of the callback types the C
code calling your Rust code expects, so it makes it easy to customize the generation for
your needs. It also provides utilities for automatically deriving conversions from raw
pointers like From<*mut T> for &mut Foo
.
use ffi;
extern "C"
extern "C"