Attribute Macro interoptopus::ffi_type [−][src]
#[ffi_type]
derive
only.Expand description
Enable a struct
or enum
to appear in generated bindings.
This will derive CTypeInfo
based on the visible information in the type definition. This
is the preferred way of enabling FFI types; although in some cases (e.g., when dealing with
types outside of your control) you will have to implement a surrogate manually, see below.
A number of attributes are available:
Attribute | On | Explanation |
---|---|---|
name="X" | struct ,enum | Uses name as the base interop name instead of the item’s Rust name.1 |
namespace="X" | struct ,enum | In backends that support multiple namespaces, determine where this should go. |
skip(x) | struct,enum | Skip field or variant x in the definition, e.g., some x of PhantomData . |
patterns(p) | struct ,enum | Mark this type as part of a pattern, see below. |
opaque | struct | Creates an opaque type without fields. Can only be used behind a pointer. |
surrogates(x="y") | struct | Invoke function y to provide a CTypeInfo for field x , see below. |
1 While a type’s name must be unique (even across modules) backends are free to further transform this name, e.g., by converting
MyVec
to LibraryMyVec
. In other words, using name
will change a type’s name, but not using name
is no guarantee the final name will
not be modified.
In contrast to functions and constants types annotated with #[ffi_type]
will be detected
automatically and do not have to be explicitly mentioned for the definition of the inventory_function!()
.
Surrogates
When dealing with types outside of your control you will not be able to implement CTypeInfo
for them.
Instead you need a surrogate, a helper function which returns that info for the type.
The surrogate’s signature is:
fn some_foreign_type() -> CType { // Return an appropriate CType }
Patterns
Patterns allow you to write, and backends to generate more idiomatic code. The following patterns are currently supported by this annotation:
Pattern | On | Explanation |
---|---|---|
success_enum | enum | Denotes this as a SuccessEnum . |
Examples
use interoptopus::ffi_type; #[ffi_type(opaque, name = "MyVec")] #[derive(Copy, Clone, Debug)] #[repr(C)] pub struct Vec2f32 { pub x: f32, pub y: f32, pub z: f32, }