Struct Ref

Source
pub struct Ref<T: ?Sized> { /* private fields */ }
Expand description

A weak reference-counted reference to a RefBox.

See the module documentation for more information.

Implementations§

Source§

impl<T: ?Sized> Ref<T>

Source

pub fn try_borrow_mut<'rc>(&'rc self) -> Result<Borrow<'rc, 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
  • Err(BorrowError::Dropped) if the owning RefBox was dropped
Source

pub fn try_borrow_mut_or_else<'rc, E>( &'rc self, err_borrowed: impl FnOnce() -> E, err_dropped: impl FnOnce() -> E, ) -> Result<Borrow<'rc, 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 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 successful
  • Err(BorrowError::Borrowed) if the data was already borrowed
  • Err(BorrowError::Dropped) if the owning RefBox was dropped
Source

pub fn try_clone(&self) -> Option<Ref<T>>

Tries to clone the weak reference to the data.

§Returns
  • Some(Ref) if it was succesful.
  • None if the number of weaks overflowed u32::MAX.
Source

pub fn refcount(&self) -> u32

Returns the number of Refs that point to the same data as this Ref.

Source

pub fn is_alive(&self) -> bool

Returns true if the owner of the data is alive.

Source

pub fn is_borrowed(&self) -> bool

Returns true if the data is currently mutably borrowed.

Source

pub fn is(&self, owner: &RefBox<T>) -> bool

Returns true if this Ref and the supplied RefBox point to the same instance.

Source

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
  1. Ensure there are no mutable references to T.
  2. Ensure T is fully initialized (don’t use this in RefBox::new_cyclic).
  3. Ensure the owning RefBox is alive for the entire lifetime of the returned reference.
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 or dropped.

§Safety
  1. Ensure there are no other references to T.
  2. Ensure T is fully initialized (don’t use this in RefBox::new_cyclic).
  3. Ensure the owning RefBox is alive for the entire lifetime of the returned reference.
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 Ref into a raw pointer.

Source

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

Creates a Ref from a raw pointer.

Source

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

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

Trait Implementations§

Source§

impl<T: ?Sized> Clone for Ref<T>

Source§

fn clone(&self) -> Self

Copies the reference and increases the reference counter.

§Panics

Panics if the number of Refs overflows u32::MAX.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: ?Sized> Debug for Ref<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

fn eq(&self, other: &Ref<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<RefBox<T>> for Ref<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 for Ref<T>

Source§

fn eq(&self, other: &Self) -> bool

Returns true if both Refs point to the same instance.

This compares the pointers, not the actual object itself, which means it is very fast.

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> Eq for Ref<T>

Auto Trait Implementations§

§

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

§

impl<T> !RefUnwindSafe for Ref<T>

§

impl<T> !Send for Ref<T>

§

impl<T> !Sync for Ref<T>

§

impl<T> Unpin for Ref<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for Ref<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.