pub struct RefBox<T: ?Sized> { /* private fields */ }
Expand description
A smart pointer with many reference-counted weak references.
See the module documentation for more information.
§Accessing the data behind the RefBox
See RefBox::try_borrow_mut
, RefBox::try_borrow_mut_or_else
and
RefBox::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> RefBox<T>
impl<T: ?Sized> RefBox<T>
Sourcepub fn try_borrow_mut(&self) -> Result<Borrow<'_, T>, BorrowError>
pub fn try_borrow_mut(&self) -> Result<Borrow<'_, T>, BorrowError>
Tries to borrow the data mutably.
§Returns
Ok(Borrow)
if the borrow was successfulErr(BorrowError::Borrowed)
if the data was already borrowed
Sourcepub fn try_borrow_mut_or_else<E>(
&self,
err_borrowed: impl FnOnce() -> E,
) -> Result<Borrow<'_, T>, E>
pub fn try_borrow_mut_or_else<E>( &self, err_borrowed: 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, 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 borrowed
Sourcepub fn downgrade(&self) -> Weak<T>
pub fn downgrade(&self) -> Weak<T>
Creates a Weak
reference to the data.
§Panics
Panics if the number of Refs overflows u32::MAX
.
See Self::try_downgrade
for a version that does not panic.
Sourcepub fn try_downgrade(&self) -> Option<Weak<T>>
pub fn try_downgrade(&self) -> Option<Weak<T>>
Sourcepub fn weak_count(&self) -> u32
pub fn weak_count(&self) -> u32
Returns the number of Weak
s pointing to this RefBox.
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.
§Safety
Ensure there are no mutable references to T
.
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.
§Safety
Ensure there are no other references to T
.