Skip to main content

NicheCell

Struct NicheCell 

Source
pub struct NicheCell<T>(UnsafeCell<Option<T>>);
Expand description

A datatype with interior mutability, which can be uninitialized via [Option]. This datatype is designed to be used with values that can take advantage of niche optimization (where the datatype T has a value which is guaranteed to be invalid, like a NonZeroU64 being zero)

The caller may also use this datatype if [Option] instead of MaybeUninit is necessary for code safety, as this type returns [None] on calling Self::get if Self::init has not been run yet, compared to UncheckedCell where calling UncheckedCell::get before UncheckedCell::init is undefined behavior.

Tuple Fields§

§0: UnsafeCell<Option<T>>

Implementations§

Source§

impl<T> NicheCell<T>

Source

pub const fn new() -> Self

Constructor; creates a new NicheCell containing [None]

Source

pub unsafe fn init(&self, value: T)

Set the inner value

§Safety

The caller must guarantee the following:

  • That this method has never been called before and will never be called again
  • That at the time of calling this method, no references or pointers to this data exist
  • While this method is being called, no other CPU is working with the given data
Source

pub unsafe fn get(&self) -> &Option<T>

Get a reference to the inner value

§Safety

The caller must guarantee that at this method’s entire execution time, no mutable references or pointers to this data exist or will exist

Trait Implementations§

Source§

impl<T: Send + Sync> Sync for NicheCell<T>

Safety: this type can be shared across CPUs as the callers have the responsibility to ensure proper data safety because of the unsafe contracts on init and get

Auto Trait Implementations§

§

impl<T> !Freeze for NicheCell<T>

§

impl<T> !RefUnwindSafe for NicheCell<T>

§

impl<T> Send for NicheCell<T>
where UnsafeCell<Option<T>>: Send,

§

impl<T> Unpin for NicheCell<T>
where UnsafeCell<Option<T>>: Unpin,

§

impl<T> UnsafeUnpin for NicheCell<T>
where UnsafeCell<Option<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for NicheCell<T>
where UnsafeCell<Option<T>>: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = !

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.