Struct RefBox

Source
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> RefBox<T>

Source

pub fn new(value: T) -> Self

Creates a new RefBox reference counted pointer.

Source§

impl<T: ?Sized> RefBox<T>

Source

pub fn try_borrow_mut(&self) -> Result<Borrow<'_, T>, BorrowError>

Tries to borrow the data mutably.

§Returns
  • Ok(Borrow) if the borrow was successful
  • Err(BorrowError::Borrowed) if the data was already borrowed
Source

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.

Source

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 successful
  • Err(BorrowError::Borrowed) if the data was already borrowed
Source

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.

Source

pub fn try_downgrade(&self) -> Option<Weak<T>>

Tries to create a Weak pointer to the data.

§Returns
  • Some(Weak) if it was successful.
  • None if the number of weak pointers overflowed u32::MAX.
Source

pub fn weak_count(&self) -> u32

Returns the number of Weaks pointing to this RefBox.

Source

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.

Source

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.

Source

pub fn as_ptr(&self) -> *const T

Returns a raw pointer to T.

Source

pub fn into_raw(self) -> *mut RefBoxHeap<T>

Turns the RefBox into a raw pointer.

Source

pub unsafe fn from_raw(ptr: *mut RefBoxHeap<T>) -> Self

Creates a RefBox from a raw pointer.

§Safety

Ensure ptr is a valid pointer to a RefBoxHeap<T> instance.

Source

pub unsafe fn cast<U>(self) -> RefBox<U>

Casts a RefBox<T> to a RefBox<U>.

§Safety

Ensure T can be safely cast to U.

Trait Implementations§

Source§

impl<T: ?Sized + Debug> Debug for RefBox<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Tries to access the data and write a debug representation of it. If accessing fails, it only writes the pointer value.

Source§

impl<T: Default> Default for RefBox<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ?Sized> Drop for RefBox<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<T> for RefBox<T>

Source§

fn from(val: T) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized> PartialEq<RefBox<T>> for Weak<T>

Source§

fn eq(&self, other: &RefBox<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: ?Sized> PartialEq<Weak<T>> for RefBox<T>

Source§

fn eq(&self, other: &Weak<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T> Freeze for RefBox<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for RefBox<T>

§

impl<T> !Send for RefBox<T>

§

impl<T> !Sync for RefBox<T>

§

impl<T> Unpin for RefBox<T>
where T: Unpin + ?Sized,

§

impl<T> !UnwindSafe for RefBox<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.