Attribute Macro interoptopus::ffi_type[][src]

#[ffi_type]
This is supported on crate feature 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:

AttributeOnExplanation
name="X"struct,enumUses name as the base interop name instead of the item’s Rust name.1
namespace="X"struct,enumIn backends that support multiple namespaces, determine where this should go.
skip(x)struct,enumSkip field or variant x in the definition, e.g., some x of PhantomData.
patterns(p)struct,enumMark this type as part of a pattern, see below.
opaquestructCreates an opaque type without fields. Can only be used behind a pointer.
surrogates(x="y")structInvoke function y to provide a CTypeInfo for field x, see below.
debug*Print generated helper code in console.

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:

PatternOnExplanation
success_enumenumDenotes 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,
}