pub struct ExclusiveCell<T>where
    T: ?Sized,{ /* private fields */ }
Expand description

A synchronization primitive which can be accessed only once.

This type is a thread-safe cell, and can be used in statics. ExclusiveCell provides a mutable reference to the contents without RAII guards, but only on the first try.

Relation with other types

ExclusiveCell is complementary to OnceCell with regards to Mutex and RwLock:

CMutexRwLockOnceCellExclusiveCell
&C providesMutexGuardRwLock{Read,Write}Guard&T&mut

A OnceCell can be emulated using a RwLock by only ever calling try_read and leaking the RwLockReadGuard. Similarily, ExclusiveCell can be emulated using a RwLock by only ever calling try_write and leaking the RwLockWriteGuard.

In contrast to OnceCell but similarly to Mutex and RwLock, the contents of a ExclusiveCell have to be initialized at creation.

Similarities with cortex_m::singleton

ExclusiveCell can be used similarily to cortex_m::singleton to create a mutable reference to a statically allocated value. In contrast to cortex_m::singleton, ExclusiveCell is thread safe and does not require using macros.

Examples

use exclusive_cell::ExclusiveCell;

static EXCLUSIVE_CELL: ExclusiveCell<usize> = ExclusiveCell::new(5);

let number = EXCLUSIVE_CELL.take().unwrap();
assert_eq!(number, &mut 5);

assert!(EXCLUSIVE_CELL.take().is_none());

Implementations§

source§

impl<T> ExclusiveCell<T>

source

pub const fn new(val: T) -> ExclusiveCell<T>

Creates a new ExclusiveCell containing the given value.

Examples
use exclusive_cell::ExclusiveCell;

let exclusive_cell = ExclusiveCell::new(5);
source

pub fn into_inner(self) -> T

Unwraps the value.

Examples
use exclusive_cell::ExclusiveCell;

let exclusive_cell = ExclusiveCell::new(5);
let number = exclusive_cell.into_inner();

assert_eq!(number, 5);
source§

impl<T> ExclusiveCell<T>where T: ?Sized,

source

pub fn take(&self) -> Option<&mut T>

Takes the mutable reference to the wrapped value.

Only the first call returns Some. All subsequent calls return None.

Examples
use exclusive_cell::ExclusiveCell;

let exclusive_cell = ExclusiveCell::new(5);

let number = exclusive_cell.take().unwrap();
assert_eq!(number, &mut 5);

assert!(exclusive_cell.take().is_none());
source

pub fn is_taken(&self) -> bool

Returns true if the mutable reference has been taken.

Examples
use exclusive_cell::ExclusiveCell;

let exclusive_cell = ExclusiveCell::new(5);
assert!(!exclusive_cell.is_taken());

let number = exclusive_cell.take().unwrap();
assert!(exclusive_cell.is_taken());
source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

Since this method borrows ExclusiveCell mutably, it is statically guaranteed that no borrows to the underlying data exists.

Examples
use exclusive_cell::ExclusiveCell;

let mut exclusive_cell = ExclusiveCell::new(5);

let number = exclusive_cell.get_mut();
assert_eq!(number, &mut 5);

assert!(!exclusive_cell.is_taken());

Trait Implementations§

source§

impl<T> Debug for ExclusiveCell<T>where T: Debug + ?Sized,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<T> Default for ExclusiveCell<T>where T: Default,

source§

fn default() -> ExclusiveCell<T>

Returns the “default value” for a type. Read more
source§

impl<T> From<T> for ExclusiveCell<T>

source§

fn from(value: T) -> ExclusiveCell<T>

Converts to this type from the input type.
source§

impl<T> Send for ExclusiveCell<T>where T: Send + ?Sized,

source§

impl<T> Sync for ExclusiveCell<T>where T: Send + ?Sized,

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ExclusiveCell<T>

§

impl<T: ?Sized> Unpin for ExclusiveCell<T>where T: Unpin,

§

impl<T: ?Sized> UnwindSafe for ExclusiveCell<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

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

source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

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

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

Performs the conversion.