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>
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 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 ensure that the inner value is actually of type U.
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.
§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);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-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 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 currently borrowed.
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
T, e.g., somedyn Traitwith concrete typeU - 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 with some checks.
§Safety
coerce function must ensure the returned pointer is:
- a valid unsizing of
T, e.g., somedyn Traitwith concrete typeU - with same address (bare data pointer without metadata) as input
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
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., somedyn Traitwith concrete typeU - consists the same address (bare data pointer without metadata) as the input
Trait Implementations§
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>
impl<T: Eq + ?Sized> Eq for ThinCell<T>
impl<T: ?Sized + Send> Unpin for ThinCell<T>
ThinCell is Unpin as it does not move its inner data.