Attribute Macro interoptopus::ffi_type

source ·
#[ffi_type]
Available 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,enumDetermine which namespace or file item should go. 2
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. 2
opaquestructCreates an opaque type without fields. Can only be used behind a pointer.
visibility(x="v")structOverride visibility for field x as public or private; _ means all fields. 2
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.

2 Will not be reflected in C backend, but available to languages supporting them, e.g., C# will emit field visibility and generate classes from service patterns.

§Types and the Inventory

In contrast to functions and constants most types annotated with #[ffi_type] will be detected automatically and need no mention in the inventory function.

The exception are types that do not show up as fields of another type, or inside a function signature.

§Patterns

Patterns allow you to write, and backends to generate more idiomatic code. The following patterns are currently supported by this annotation:

PatternOnExplanation
ffi_errorenumDenotes this as a FFIError.

§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,
}