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, returning the inner value if there are no other owners and it is not currently borrowed.

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 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 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.

§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 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 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.

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Unpin for ThinCell<T>
where T: Unpin + ?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, 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.