Generic functions to work with opaque pointers when use FFI to expose Rust structs
With this crate you can manage raw pointers easily to expose structs that will be
use as opaque pointers from C or C++ calling to Rust functions to use it. This
can be used with cbindgen crate with option parse.parse_deps = true
for it will generate opaque C/C++ structs to use pointers in the arguments.
You can find more information about using Rust from other languages in The Rust FFI Omnibus objects section of Jake Goulding.
```rust struct TestIt { value: u8, }
impl TestIt { pub fn new(value: u8) -> Self { Self { value, } } pub fn add(&mut self, value: u8) { self.value += value; } pub fn get(&self) -> u8 { self.value } }
/// TestIt new method.
pub extern fn testitnew(value: u8) -> *mut TestIt { opaquepointer::raw(TestIt::new(value)) }
/// TestIt add method.
pub extern fn testitadd(testit: *mut TestIt, value: u8) { let testit = unsafe { opaquepointer::mut_object(testit) }; testit.add(value); }
/// TestIt get method.
pub extern fn testitget(testit: *const TestIt) -> u8 { let testit = unsafe { opaquepointer::object(testit) }; testit.get() }
/// TestIt free.
pub extern fn testitfree(testit: *mut TestIt) { unsafe { opaquepointer::free(testit) } } ```
std
: activated by default, it is required for functions using stdalloc
: alternative to compile without std but some functions will not be availablec-types
: it allow to use C types (like pointers to C strings) and requires std featurepanic-if-null
: it will check if a pointer is null to panic before to use a null pointer