pub struct CovariantUnsafeCell<T: ?Sized>(/* private fields */);covariant_unsafe_cell #159735)Expand description
Covariant version of UnsafeCell.
Implementations§
Source§impl<T> CovariantUnsafeCell<T>
impl<T> CovariantUnsafeCell<T>
Sourcepub const fn new(value: T) -> CovariantUnsafeCell<T>
🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)
pub const fn new(value: T) -> CovariantUnsafeCell<T>
covariant_unsafe_cell #159735)Constructs a new instance of CovariantUnsafeCell which will wrap the specified value.
All access to the inner value through &CovariantUnsafeCell<T> requires unsafe code.
§Examples
Sourcepub const fn into_inner(self) -> T
🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)
pub const fn into_inner(self) -> T
covariant_unsafe_cell #159735)Unwraps the value, consuming the cell.
§Examples
Source§impl<T: ?Sized> CovariantUnsafeCell<T>
impl<T: ?Sized> CovariantUnsafeCell<T>
Sourcepub const fn get(&self) -> NonNull<T>
🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)
pub const fn get(&self) -> NonNull<T>
covariant_unsafe_cell #159735)Gets a mutable non-null pointer to the wrapped value.
This can be cast to a pointer of any kind. When creating (shared or mutable) references, you
must uphold the aliasing rules; see the UnsafeCell type-level docs for more discussion
and caveats.
§Examples
Sourcepub const fn get_mut(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)
pub const fn get_mut(&mut self) -> &mut T
covariant_unsafe_cell #159735)Returns a mutable reference to the underlying data.
This call borrows the CovariantUnsafeCell mutably (at compile-time) which guarantees that
we possess the only reference.
§Examples
Sourcepub const fn raw_get(this: *const Self) -> *mut T
🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)
pub const fn raw_get(this: *const Self) -> *mut T
covariant_unsafe_cell #159735)Gets a mutable pointer to the wrapped value.
The difference from get is that this function accepts a raw pointer,
which is useful to avoid the creation of temporary references.
This can be cast to a pointer of any kind. When creating (shared or mutable) references, you
must uphold the aliasing rules; see [the UnsafeCell type-level docs] for more discussion
and caveats.
§Examples
Gradual initialization of an CovariantUnsafeCell requires raw_get, as
calling get would require creating a reference to uninitialized data:
#![feature(covariant_unsafe_cell)]
use std::cell::CovariantUnsafeCell;
use std::mem::MaybeUninit;
let m = MaybeUninit::<CovariantUnsafeCell<i32>>::uninit();
unsafe { CovariantUnsafeCell::raw_get(m.as_ptr()).write(5); }
// avoid below which references to uninitialized data
// unsafe { CovariantUnsafeCell::get(&*m.as_ptr()).write(5); }
let uc = unsafe { m.assume_init() };
assert_eq!(uc.into_inner(), 5);