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 TBox<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.


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

Auto Trait Implementations

impl<T: ?Sized> RefUnwindSafe for BoxMarshaler<T> where
    T: RefUnwindSafe

impl<T: ?Sized> Send for BoxMarshaler<T> where
    T: Send

impl<T: ?Sized> Sync for BoxMarshaler<T> where
    T: Sync

impl<T: ?Sized> Unpin for BoxMarshaler<T> where
    T: Unpin

impl<T: ?Sized> UnwindSafe for BoxMarshaler<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized

impl<T> Borrow<T> for T where
    T: ?Sized

impl<T> BorrowMut<T> for T where
    T: ?Sized

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.