Struct cffi::BoxMarshaler [−][src]
pub struct BoxMarshaler<T: ?Sized>(_);
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 cffi::{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, Global>> for BoxMarshaler<T>
[src]
impl<T> FromForeign<*const T, Box<T, Global>> for BoxMarshaler<T>
[src]impl<T> InputType for BoxMarshaler<T>
[src]
impl<T> InputType for BoxMarshaler<T>
[src]type Foreign = *const T
type ForeignTraitObject = ()
impl<T> ReturnType for BoxMarshaler<T>
[src]
impl<T> ReturnType for BoxMarshaler<T>
[src]type Foreign = *const T
type ForeignTraitObject = ()
fn foreign_default() -> Self::Foreign
[src]
fn foreign_default_trait_object() -> Self::ForeignTraitObject
[src]
impl<T> ToForeign<Box<T, Global>, *const T> for BoxMarshaler<T>
[src]
impl<T> ToForeign<Box<T, Global>, *const T> for BoxMarshaler<T>
[src]type Error = Infallible
fn to_foreign(local: Box<T>) -> Result<*const T, Self::Error>
[src]
Auto Trait Implementations
impl<T: ?Sized> RefUnwindSafe for BoxMarshaler<T> where
T: RefUnwindSafe,
impl<T: ?Sized> RefUnwindSafe for BoxMarshaler<T> where
T: RefUnwindSafe,
impl<T: ?Sized> Send for BoxMarshaler<T> where
T: Send,
impl<T: ?Sized> Send for BoxMarshaler<T> where
T: Send,
impl<T: ?Sized> Sync for BoxMarshaler<T> where
T: Sync,
impl<T: ?Sized> Sync for BoxMarshaler<T> where
T: Sync,
impl<T: ?Sized> Unpin for BoxMarshaler<T> where
T: Unpin,
impl<T: ?Sized> Unpin for BoxMarshaler<T> where
T: Unpin,
impl<T: ?Sized> UnwindSafe for BoxMarshaler<T> where
T: UnwindSafe,
impl<T: ?Sized> UnwindSafe for BoxMarshaler<T> where
T: UnwindSafe,