ValueBox

Crates.io MIT licensed

ValueBox allows developers to pass Rust-allocated structures over ffi. The value-box crate handles most typical use-cases when creating ffi bindings to rust libraries such as: - Return newly allocated Rust structures passing ownership to the caller. - Receiving the previously created value box and calling associated functions taking the rust structure by reference, mutable reference, cloning the value or taking the value. - Finally, dropping the box with the Rust structure in it. - Supports Box<dyn MyTrait>. - ValueBox is defined as #[transparent] - Error handling via custom Error and Result.

Example:

```rust use value_box::{ReturnBoxerResult, ValueBox, ValueBoxPointer};

[no_mangle]

pub fn libraryobjectcreate() -> *mut ValueBox { ValueBox::new(MyObject::new()).into_raw() }

[no_mangle]

pub fn libraryobjectissomething(object: *mut ValueBox) -> bool { object .toref() .map(|object| object.issomething()) .unwrapor(false) }

[no_mangle]

pub fn libraryobjectbyref(object: *mut ValueBox) { object.toref().map(|object| object.by_ref()).log(); }

[no_mangle]

pub fn libraryobjectbymut(object: *mut ValueBox) { object.toref().map(|mut object| object.by_mut()).log(); }

[no_mangle]

pub fn libraryobjectbyvalue(object: *mut ValueBox) { object.takevalue().map(|object| object.by_value()).log(); }

[no_mangle]

pub fn libraryobjectbyvalueclone(object: *mut ValueBox) { object .toref() .map(|object| object.clone().byvalue()) .log(); }

[no_mangle]

pub fn libraryobjectrelease(object: *mut ValueBox) { object.release(); }

[derive(Debug, Clone)]

pub struct MyObject {} impl MyObject { pub fn new() -> Self { Self {} }

pub fn by_ref(&self) {}
pub fn by_mut(&mut self) {}
pub fn by_value(self) {}
pub fn is_something(&self) -> bool {
    true
}

} ```