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