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

    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;

    unsafe fn borrow_mut(ptr: *const c_void) -> ScopeGuard<Self, fn(_: 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

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

Required Methods

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

The foreign representation is a pointer to void.

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. Additionally, all instances (if any) of values returned by ForeignOwnable::borrow_mut for this object must have been dropped.

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 and ForeignOwnable::borrow_mut for this object must have been dropped.

Provided Methods

Mutably 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. Additionally, all instances (if any) of values returned by ForeignOwnable::borrow and ForeignOwnable::borrow_mut for this object must have been dropped.

Implementations on Foreign Types

Implementors