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>
impl<T> ThinCell<T>
Sourcepub fn try_unwrap(self) -> Result<T, Self>
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.
Sourcepub unsafe fn unwrap_unchecked(self) -> T
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>
impl<T: ?Sized> ThinCell<T>
Sourcepub fn leak(self) -> *mut ()
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.
Sourcepub unsafe fn from_raw(ptr: *mut ()) -> Self
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.
Sourcepub fn borrow(&self) -> Ref<'_, T>
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);Sourcepub fn try_borrow(&self) -> Option<Ref<'_, T>>
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 availableSourcepub unsafe fn borrow_unchecked(&mut self) -> &mut T
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.
Sourcepub unsafe fn new_unsize<U>(
data: U,
coerce: impl Fn(*const Inner<U>) -> *const Inner<T>,
) -> Self
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., someInner<dyn Trait>orInner<[_]> - with same address (bare data pointer without metadata) as input
Sourcepub unsafe fn unsize<U: ?Sized>(
self,
coerce: impl Fn(*const Inner<T>) -> *const Inner<U>,
) -> ThinCell<U>
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.
Sourcepub unsafe fn unsize_unchecked<U: ?Sized>(
self,
coerce: impl Fn(*const Inner<T>) -> *const Inner<U>,
) -> ThinCell<U>
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
ThinCellmust have unique ownership (count == 1) - The
ThinCellmust not be borrowed coercehas the same requirements asThinCell::new_unsize.
In particular, first two requirement is the exact state after
ThinCell::new.
Sourcepub fn ptr_eq(&self, other: &Self) -> bool
pub fn ptr_eq(&self, other: &Self) -> bool
Returns true if the two ThinCells point to the same allocation.
Sourcepub unsafe fn downcast_unchecked<U>(self) -> ThinCell<U>
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]>
impl<T, const N: usize> ThinCell<[T; N]>
Sourcepub fn unsize_slice(self) -> ThinCell<[T]>
pub fn unsize_slice(self) -> ThinCell<[T]>
Coerce an array ThinCell to a slice one.
Source§impl<T: Any + ?Sized> ThinCell<T>
impl<T: Any + ?Sized> ThinCell<T>
Sourcepub fn downcast<U: Any>(self) -> Result<ThinCell<U>, DowncastError<T>>
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 typeUand is not currently borrowedErr(DowncastError::Borrowed(self))if the inner value is currently borrowedErr(DowncastError::Type(self))if the inner value is not of typeU
Trait Implementations§
Source§impl<T: Ord + ?Sized> Ord for ThinCell<T>
impl<T: Ord + ?Sized> Ord for ThinCell<T>
Source§fn cmp(&self, other: &Self) -> Ordering
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq + ?Sized> PartialEq for ThinCell<T>
impl<T: PartialEq + ?Sized> PartialEq for ThinCell<T>
Source§impl<T: Ord + ?Sized> PartialOrd for ThinCell<T>
impl<T: Ord + ?Sized> PartialOrd for ThinCell<T>
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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.
impl<T: Eq + ?Sized> Eq for ThinCell<T>
impl<T: ?Sized> Unpin for ThinCell<T>
ThinCell is Unpin as it does not move its inner data.