ThinCell

Struct ThinCell 

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

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

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 unsafe fn downcast_unchecked<U>(self) -> ThinCell<U>

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

§Safety

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

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.

§Panics

Panics if the value is already borrowed. For a non-panicking variant, use try_borrow.

§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-panicking 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 currently borrowed.

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 T, e.g., some dyn Trait with concrete type U
  • 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 with some checks.

§Safety

coerce function must ensure the returned pointer is:

  • a valid unsizing of T, e.g., some dyn Trait with concrete type U
  • with same address (bare data pointer without metadata) as input

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

The returned U must be a valid unsizing of T, i.e., some dyn Trait with concrete type T. &U must be a fat pointer. If not, this will fail to compile.

§Safety

The ThinCell must:

  • have unique ownership (count == 1)
  • not be borrowed

In particular, this is the exact state after ThinCell::new.

coerce function must ensure the returned pointer:

  • is a valid unsizing of T, e.g., some dyn Trait with concrete type U
  • consists the same address (bare data pointer without metadata) as the input
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§

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

Source

pub fn downcast<U: Any>(self) -> Option<ThinCell<U>>

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

Returns Some(ThinCell<U>) if the inner value is of type U, or None otherwise.

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

Source§

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

Compares the inner values for equality.

§Panics

Panics if either ThinCell is currently borrowed.

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.

§Panics

Panics 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 + Send> 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> 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.