Skip to main content

ThinCell

Struct ThinCell 

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

A compact (1-usize), single-threaded smart pointer combining Rc and RefCell with only borrow_mut.

Implementations§

Source§

impl<T> ThinCell<T>

Source

pub fn new(data: T) -> Self

Creates a new ThinCell wrapping the given data.

Source

pub fn try_unwrap(self) -> Result<T, Self>

Consumes the ThinCell and try to get inner value.

Returns the inner value in Ok if there are no other owners and it is not currently borrowed, return Err(self) otherwise.

Source

pub unsafe fn unwrap_unchecked(self) -> T

Consumes the ThinCell, returning the inner value.

§Safety

The caller must guarantee that there are no other owners and it is not currently borrowed.

Source§

impl<T: ?Sized> ThinCell<T>

Source

pub fn leak(self) -> *mut ()

Leaks the ThinCell, returning a raw pointer to the inner allocation.

The returned pointer points to the inner allocation. To restore the ThinCell, use ThinCell::from_raw.

Source

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

Reconstructs a ThinCell<T> from a raw pointer.

§Safety

The pointer must have been obtained from a previous call to ThinCell::leak, and the ThinCell must not have been dropped in the meantime.

Source

pub fn count(&self) -> usize

Returns the number of owners.

Source

pub fn borrow(&self) -> Ref<'_, T>

Borrows the value mutably.

Returns a Ref guard that provides mutable access to the inner value. The borrow lasts until the guard is dropped. See module-level documentation for details on borrowing behavior.

§Examples
let cell = ThinCell::new(5);

{
    let mut borrowed = cell.borrow();
    *borrowed = 10;
} // borrow is released here

assert_eq!(*cell.borrow(), 10);
Source

pub fn try_borrow(&self) -> Option<Ref<'_, T>>

Attempts to borrow the value mutably.

Returns Some(Ref) if the value is not currently borrowed, or None if it is already borrowed.

This is the non-blocking variant of borrow.

§Examples
let cell = ThinCell::new(5);

let borrow1 = cell.borrow();
assert!(cell.try_borrow().is_none()); // Already borrowed
drop(borrow1);
assert!(cell.try_borrow().is_some()); // Now available
Source

pub unsafe fn borrow_unchecked(&mut self) -> &mut T

Get a mutable reference to the inner value without any checks.

§Safety

The caller must guarantee that there are no other owners and it is not borrowed now and during the entire lifetime of the returned reference.

Source

pub unsafe fn new_unsize<U>( data: U, coerce: impl Fn(*const Inner<U>) -> *const Inner<T>, ) -> Self

Creates a new ThinCell<U> from data: U and coerces it to ThinCell<T>.

§Safety

coerce function must ensure the returned pointer is:

  • a valid unsizing of Inner<T>, e.g., some Inner<dyn Trait> or Inner<[_]>
  • with same address (bare data pointer without metadata) as input
Source

pub unsafe fn unsize<U: ?Sized>( self, coerce: impl Fn(*const Inner<T>) -> *const Inner<U>, ) -> ThinCell<U>

Manually coerce to unsize.

§Safety

coerce has the same requirements as ThinCell::new_unsize.

§Panics

Panics if the ThinCell is currently shared (count > 1) or borrowed.

See ThinCell::unsize_unchecked for details.

Source

pub unsafe fn unsize_unchecked<U: ?Sized>( self, coerce: impl Fn(*const Inner<T>) -> *const Inner<U>, ) -> ThinCell<U>

Manually coerce to unsize without checks.

§Safety
  • The ThinCell must have unique ownership (count == 1)
  • The ThinCell must not be borrowed
  • coerce has the same requirements as ThinCell::new_unsize.

In particular, first two requirement is the exact state after ThinCell::new.

Source

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

Returns the raw pointer to the inner allocation.

Source

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

Returns true if the two ThinCells point to the same allocation.

Source

pub unsafe fn downcast_unchecked<U>(self) -> ThinCell<U>

Downcasts the ThinCell<T> to ThinCell<U>.

§Safety

The caller must make sure that the inner value is actually of type U.

Source§

impl<T, const N: usize> ThinCell<[T; N]>

Source

pub fn unsize_slice(self) -> ThinCell<[T]>

Coerce an array ThinCell to a slice one.

Source§

impl<T: Any + ?Sized> ThinCell<T>

Source

pub fn downcast<U: Any>(self) -> Result<ThinCell<U>, DowncastError<T>>

Attempts to downcast the ThinCell<T> to ThinCell<U>.

§Returns
  • Ok(ThinCell<U>) if the inner value is of type U and is not currently borrowed
  • Err(DowncastError::Borrowed(self)) if the inner value is currently borrowed
  • Err(DowncastError::Type(self)) if the inner value is not of type U

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

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

Source§

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

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

impl<T: Default> Default for ThinCell<T>

Source§

fn default() -> Self

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

impl<T: Display + ?Sized> Display for ThinCell<T>

Source§

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

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

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Ord + ?Sized> Ord for ThinCell<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

Compares the inner values.

This will block on sync version or panic on unsync version if either ThinCell is currently borrowed.

1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + ?Sized> PartialEq for ThinCell<T>

Source§

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

Compares the inner values for equality.

This will block on sync version or panic on unsync version if either ThinCell is currently borrowed.

If a shallow comparison is desired, use ptr_eq instead.

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: Ord + ?Sized> PartialOrd for ThinCell<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

Compares the inner values.

This will block on sync version or panic on unsync version if either ThinCell is currently borrowed.

1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq + ?Sized> Eq for ThinCell<T>

Source§

impl<T: ?Sized> Unpin for ThinCell<T>

ThinCell is Unpin as it does not move its inner data.

Auto Trait Implementations§

§

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

§

impl<T> !RefUnwindSafe for ThinCell<T>

§

impl<T> !Send for ThinCell<T>

§

impl<T> !Sync for ThinCell<T>

§

impl<T> UnsafeUnpin for ThinCell<T>
where T: ?Sized,

§

impl<T> UnwindSafe for ThinCell<T>
where T: UnwindSafe + ?Sized,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.