Macro ffi_opaque::opaque[][src]

macro_rules! opaque {
    ($(
        $(#[$meta:meta])*
        $vis:vis struct $name:ident;
    )+) => { ... };
}

Creates one or more types capable of representing opaque structs in FFI situations.

The resulting type:

  • cannot be constructed outside of the module it is defined in
  • ensures proper pointer alignment
  • is !Send, !Sync, !Unpin
  • is FFI safe

Example

Given the following C headers:

typedef struct leveldb_options_t leveldb_options_t;

leveldb_options_t* leveldb_options_create();

We can represent the opaque struct leveldb_options_t on the Rust side like this:

use ffi_opaque::opaque;

opaque! {
    /// And we can document the type.
    pub struct leveldb_options_t;
}

extern "C" {
    pub fn leveldb_options_create() -> *mut leveldb_options_t;
}

Example 2

Multiple definitions are possible:

use ffi_opaque::opaque;

opaque! {
    /// Documentation for type_1;
    pub struct type_1;
    /// Documentation for type_2;
    pub struct type_2;
}