[−][src]Struct cffi::BoxMarshaler
The Box
marshaler is the catch-all just-throw-it-on-the-heap opaque pointer solution.
It supports the following modes of operation:
To the foreign interface:
T
→*const/mut T
Box<T>
→*const/*mut T
From the foreign interface:
*const/mut T
→Box<T>
(owned)*const T
→&T
(ref)*mut T
→&mut T
(mut ref)
Freeing T
Your foreign code should ensure that they call BoxMarshaler::<*mut/const T, Box<T>>::from_foreign
,
which will allow you to consume the boxed T
and allow it to drop as per Rust's usual rules.
Example
use cursed::{BoxMarshaler, FromForeign, ToForeign}; struct Something { data: Vec<u8> } fn demo() { let something = Something { data: vec![1, 3, 55] }; // BoxMarshaler::to_foreign is Infallible let ptr: *const Something = BoxMarshaler::to_foreign(something).unwrap(); /* send `ptr` over ffi, process it in some way, etc */ // This isn't infallible though, checks for null pointers. let boxed: Box<Something> = match BoxMarshaler::from_foreign(ptr) { Ok(v) => v, Err(e) => panic!("!") }; // Let the boxed item drop and it is freed. :) }
Trait Implementations
impl<T> FromForeign<*const T, Box<T>> for BoxMarshaler<T>
[src]
type Error = Box<dyn Error>
unsafe fn from_foreign(foreign: *const T) -> Result<Box<T>, Self::Error>
[src]
impl<T> InputType for BoxMarshaler<T>
[src]
impl<T> ReturnType for BoxMarshaler<T>
[src]
type Foreign = *const T
fn foreign_default() -> Self::Foreign
[src]
impl<T> ToForeign<Box<T>, *const T> for BoxMarshaler<T>
[src]
type Error = Infallible
fn to_foreign(local: Box<T>) -> Result<*const T, Self::Error>
[src]
impl<T: ?Sized> ToForeign<Result<Box<T>, Box<dyn Error + 'static>>, *const T> for BoxMarshaler<T>
[src]
Auto Trait Implementations
impl<T: ?Sized> RefUnwindSafe for BoxMarshaler<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T: ?Sized> Send for BoxMarshaler<T> where
T: Send,
T: Send,
impl<T: ?Sized> Sync for BoxMarshaler<T> where
T: Sync,
T: Sync,
impl<T: ?Sized> Unpin for BoxMarshaler<T> where
T: Unpin,
T: Unpin,
impl<T: ?Sized> UnwindSafe for BoxMarshaler<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,