pub struct BoxMarshaler<T: ?Sized>(/* private fields */);Expand description
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 TBox<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§
Source§impl<T> FromForeign<*const T, Box<T>> for BoxMarshaler<T>
impl<T> FromForeign<*const T, Box<T>> for BoxMarshaler<T>
Source§impl<T> InputType for BoxMarshaler<T>
impl<T> InputType for BoxMarshaler<T>
Source§impl<T> ReturnType for BoxMarshaler<T>
impl<T> ReturnType for BoxMarshaler<T>
type Foreign = *const T
type ForeignTraitObject = ()
fn foreign_default() -> Self::Foreign
fn foreign_default_trait_object() -> Self::ForeignTraitObject
Auto Trait Implementations§
impl<T> Freeze for BoxMarshaler<T>where
T: ?Sized,
impl<T> RefUnwindSafe for BoxMarshaler<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for BoxMarshaler<T>
impl<T> Sync for BoxMarshaler<T>
impl<T> Unpin for BoxMarshaler<T>
impl<T> UnsafeUnpin for BoxMarshaler<T>where
T: ?Sized,
impl<T> UnwindSafe for BoxMarshaler<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more