Struct TakeStatic

Source
pub struct TakeStatic<T: ?Sized> { /* private fields */ }
Expand description

A synchronization primitive which can be accessed only once.

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

§Similarities with takecell::TakeCell

TakeStatic is similar to takecell::TakeCell in that both can be used to create singletons. Additionally, TakeCells can be created at runtime, making them suitable for other use-cases such as doing work only once. TakeStatic, on the other hand, is specialized to creating statics through take_static, allowing even !Send types to be used and not requiring explicitly naming the wrapper type.

§Similarities with cortex_m::singleton

TakeStatic can be used similarly to cortex_m::singleton to create a mutable reference to a statically allocated value. In contrast to cortex_m::singleton, TakeStatic is thread safe.

§Examples

use take_static::take_static;

take_static! {
    static NUMBER: usize = 5;
}

assert_eq!(NUMBER.take(), Some(&mut 5));
assert_eq!(NUMBER.take(), None);

Implementations§

Source§

impl<T: ?Sized> TakeStatic<T>

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 take_static::take_static;

take_static! {
    static TAKE_STATIC: usize = 5;
}

let number = TAKE_STATIC.take().unwrap();
assert_eq!(number, &mut 5);
assert!(TAKE_STATIC.take().is_none());
Source

pub fn is_taken(&self) -> bool

Returns true if the mutable reference has been taken.

§Examples
use take_static::take_static;

take_static! {
    static TAKE_STATIC: usize = 5;
}

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

Trait Implementations§

Source§

impl<T: ?Sized> Debug for TakeStatic<T>

Source§

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

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

impl<T: ?Sized> Sync for TakeStatic<T>

Auto Trait Implementations§

§

impl<T> !Freeze for TakeStatic<T>

§

impl<T> !RefUnwindSafe for TakeStatic<T>

§

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

§

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

§

impl<T> UnwindSafe for TakeStatic<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<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.