Struct ExclusiveCell

Source
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> !Freeze for ExclusiveCell<T>

§

impl<T> !RefUnwindSafe for ExclusiveCell<T>

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.