pub trait ForeignOwnable: Sized {
    type Borrowed<'a>;

    // Required methods
    fn into_foreign(self) -> *const c_void;
    unsafe fn borrow<'a>(ptr: *const c_void) -> Self::Borrowed<'a>;
    unsafe fn from_foreign(ptr: *const c_void) -> Self;
}
Expand description

Used to transfer ownership to and from foreign (non-Rust) languages.

Ownership is transferred from Rust to a foreign language by calling Self::into_foreign and later may be transferred back to Rust by calling Self::from_foreign.

This trait is meant to be used in cases when Rust objects are stored in C objects and eventually “freed” back to Rust.

Required Associated Types§

source

type Borrowed<'a>

Type of values borrowed between calls to ForeignOwnable::into_foreign and ForeignOwnable::from_foreign.

Required Methods§

source

fn into_foreign(self) -> *const c_void

Converts a Rust-owned object to a foreign-owned one.

The foreign representation is a pointer to void.

source

unsafe fn borrow<'a>(ptr: *const c_void) -> Self::Borrowed<'a>

Borrows a foreign-owned object.

Safety

ptr must have been returned by a previous call to ForeignOwnable::into_foreign for which a previous matching ForeignOwnable::from_foreign hasn’t been called yet.

source

unsafe fn from_foreign(ptr: *const c_void) -> Self

Converts a foreign-owned object back to a Rust-owned one.

Safety

ptr must have been returned by a previous call to ForeignOwnable::into_foreign for which a previous matching ForeignOwnable::from_foreign hasn’t been called yet. Additionally, all instances (if any) of values returned by ForeignOwnable::borrow for this object must have been dropped.

Implementations on Foreign Types§

source§

impl<T: 'static> ForeignOwnable for Box<T>

source§

impl ForeignOwnable for ()

§

type Borrowed<'a> = ()

source§

fn into_foreign(self) -> *const c_void

source§

unsafe fn borrow<'a>(_: *const c_void) -> Self::Borrowed<'a>

source§

unsafe fn from_foreign(_: *const c_void) -> Self

Implementors§

source§

impl<T: 'static> ForeignOwnable for Arc<T>

§

type Borrowed<'a> = ArcBorrow<'a, T>

This documentation is an old archive. Please see https://rust.docs.kernel.org instead.