Struct owning_ref::OwningHandle
source · pub struct OwningHandle<O, H>where
O: StableAddress,
H: Deref,{ /* private fields */ }Expand description
OwningHandle is a complement to OwningRef. Where OwningRef allows
consumers to pass around an owned object and a dependent reference,
OwningHandle contains an owned object and a dependent object.
OwningHandle can encapsulate a RefMut along with its associated
RefCell, or an RwLockReadGuard along with its associated RwLock.
However, the API is completely generic and there are no restrictions on
what types of owning and dependent objects may be used.
OwningHandle is created by passing an owner object (which dereferences
to a stable address) along with a callback which receives a pointer to
that stable location. The callback may then dereference the pointer and
mint a dependent object, with the guarantee that the returned object will
not outlive the referent of the pointer.
Since the callback needs to dereference a raw pointer, it requires unsafe
code. To avoid forcing this unsafety on most callers, the ToHandle trait is
implemented for common data structures. Types that implement ToHandle can
be wrapped into an OwningHandle without passing a callback.
Implementations§
source§impl<O, H> OwningHandle<O, H>
impl<O, H> OwningHandle<O, H>
source§impl<O, H> OwningHandle<O, H>
impl<O, H> OwningHandle<O, H>
source§impl<O, H> OwningHandle<O, H>where
O: StableAddress,
H: Deref,
impl<O, H> OwningHandle<O, H>where
O: StableAddress,
H: Deref,
sourcepub fn new_with_fn<F>(o: O, f: F) -> Self
pub fn new_with_fn<F>(o: O, f: F) -> Self
Create a new OwningHandle. The provided callback will be invoked with
a pointer to the object owned by o, and the returned value is stored
as the object to which this OwningHandle will forward Deref and
DerefMut.
sourcepub fn try_new<F, E>(o: O, f: F) -> Result<Self, E>
pub fn try_new<F, E>(o: O, f: F) -> Result<Self, E>
Create a new OwningHandle. The provided callback will be invoked with
a pointer to the object owned by o, and the returned value is stored
as the object to which this OwningHandle will forward Deref and
DerefMut.
sourcepub fn into_owner(self) -> O
pub fn into_owner(self) -> O
Discards the dependent object and returns the owner.