ffi_box

Macro ffi_box 

Source
macro_rules! ffi_box {
    (@impl, $type:ident, $boxed_type:ident, new($func:path)) => { ... };
    (@impl, $type:ident, $boxed_type:ident, clone($func:path)) => { ... };
    ($type:ident, $boxed_type:ident, debug, delete($delete_func:path) $(, $func_type:ident($func:path))*) => { ... };
    ($type:ident, $boxed_type:ident, delete($delete_func:path) $(, $func_type:ident($func:path))*) => { ... };
}
Expand description

Generates an owned type BoxedTypeName for a FFI type TypeName.

The generated type BoxedTypeName behaves like Box<TypeName> with a custom destructor. Box types implement: Drop, Deref, DerefMut, AsRef, Borrow, Clone, Default.

§Example

mod bindings {
    pub enum TypeName {}

    extern {
        pub fn type_name_create() -> *mut TypeName;
        pub fn type_name_release(object: *mut TypeName);
        pub fn type_name_clone(object: *const TypeName) -> *mut TypeName;
    }
}
use bindings::TypeName as TypeName;
easy_ffi_wrapper::ffi_box!(
    TypeName,
    BoxedTypeName,
    debug, // optional; omit if debug output is not supported
    delete(bindings::type_name_release),
    new(bindings::type_name_create),  // optional default constructor
    clone(bindings::type_name_clone)  // optional copy constructor
);

§Rationale behind

If Rust’s built-in box type Box<T> had a facility to provide a custom destructor, we would not need to generate such box types, since we could just use the built-in type.