Trait kernel::types::ForeignOwnable
source · 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§
sourcetype Borrowed<'a>
type Borrowed<'a>
Type of values borrowed between calls to ForeignOwnable::into_foreign
and
ForeignOwnable::from_foreign
.
Required Methods§
sourcefn into_foreign(self) -> *const c_void
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.
sourceunsafe fn borrow<'a>(ptr: *const c_void) -> Self::Borrowed<'a>
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.
sourceunsafe fn from_foreign(ptr: *const c_void) -> Self
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.