pub struct Weak<T: ?Sized> { /* private fields */ }
Expand description
A weak reference-counted reference to a RefBox
.
See the module documentation for more information.
§Accessing the data behind the Weak
See Weak::try_borrow_mut
, Weak::try_borrow_mut_or_else
and
Weak::try_access_mut
.
Note: because all borrows are guarded by a single flag, only one borrow is possible at a time and all borrows are mutable.
Implementations§
Source§impl<T: ?Sized> Weak<T>
impl<T: ?Sized> Weak<T>
Sourcepub fn try_borrow_mut(&self) -> Result<Borrow<'_, T>, BorrowError>
pub fn try_borrow_mut(&self) -> Result<Borrow<'_, T>, BorrowError>
Sourcepub fn try_borrow_mut_or_else<E>(
&self,
err_borrowed: impl FnOnce() -> E,
err_dropped: impl FnOnce() -> E,
) -> Result<Borrow<'_, T>, E>
pub fn try_borrow_mut_or_else<E>( &self, err_borrowed: impl FnOnce() -> E, err_dropped: impl FnOnce() -> E, ) -> Result<Borrow<'_, T>, E>
Tries to borrow the data mutably and returns a custom error if borrowing fails.
Sourcepub fn try_access_mut<R, F: FnOnce(&mut T) -> R>(
&self,
op: F,
) -> Result<R, BorrowError>
pub fn try_access_mut<R, F: FnOnce(&mut T) -> R>( &self, op: F, ) -> Result<R, BorrowError>
Provides access to the data through a closure.
If the data is already borrowed or the owning RefBox
is dropped,
the closure is not executed and an error is returned. Otherwise, the
closure is executed and the output of the closure is returned.
§Returns
Ok(R)
if the access was successfulErr(BorrowError::Borrowed)
if the data was already borrowedErr(BorrowError::Dropped)
if the owningRefBox
was dropped
Sourcepub fn try_clone(&self) -> Option<Weak<T>>
pub fn try_clone(&self) -> Option<Weak<T>>
Tries to clone the weak reference to the data.
§Returns
Some(Weak)
if it was successful.None
if the number of weak pointers overflowedu32::MAX
.
Sourcepub fn weak_count(&self) -> u32
pub fn weak_count(&self) -> u32
Returns the total number of Weak
pointers that point to the same instance as this one.
Sourcepub fn is_borrowed(&self) -> bool
pub fn is_borrowed(&self) -> bool
Returns true if the data is currently mutably borrowed.
Sourcepub fn is(&self, owner: &RefBox<T>) -> bool
pub fn is(&self, owner: &RefBox<T>) -> bool
Returns true if this Weak
and the supplied RefBox
point to the same instance.
Sourcepub unsafe fn get_unchecked(&self) -> &T
pub unsafe fn get_unchecked(&self) -> &T
Returns an immutable reference to the data without checking if the data is already mutably borrowed or dropped.
§Safety
- Ensure there are no mutable references to
T
. - Ensure
T
is fully initialized (don’t use this inRefBox::new_cyclic
). - Ensure the owning
RefBox
is alive for the entire lifetime of the returned reference.
Sourcepub unsafe fn get_mut_unchecked(&mut self) -> &mut T
pub unsafe fn get_mut_unchecked(&mut self) -> &mut T
Returns a mutable reference to the data without checking if the data is already mutably borrowed or dropped.
§Safety
- Ensure there are no other references to
T
. - Ensure
T
is fully initialized (don’t use this inRefBox::new_cyclic
). - Ensure the owning
RefBox
is alive for the entire lifetime of the returned reference.