Struct atomic_once_cell::AtomicLazy
source · [−]pub struct AtomicLazy<T, F = fn() -> T> { /* private fields */ }Expand description
A thread-safe value which is initialized on the first access.
Blocking
Calling AtomicLazy::force() – directly or through Deref
– might block and should not be called from an interrupt
handler. To use AtomicLazy in an interrupt handler, the
following pattern is recommended:
use atomic_once_cell::AtomicLazy;
static LAZY: AtomicLazy<String> = AtomicLazy::new(|| "Hello, World!".to_owned());
fn interrupt_handler() {
let item = AtomicLazy::get(&LAZY).unwrap_or_else(|| unreachable!());
assert_eq!(*item, "Hello, World!");
// [...]
}
fn main() {
AtomicLazy::init(&LAZY);
assert_eq!(*LAZY, "Hello, World!");
// [...] <- Enable interrupt here
interrupt_handler(); // interrupt handler called
// asynchronously at some point
// [...]
}Implementations
sourceimpl<T, F> AtomicLazy<T, F>
impl<T, F> AtomicLazy<T, F>
sourceimpl<T, F> AtomicLazy<T, F> where
F: FnOnce() -> T,
impl<T, F> AtomicLazy<T, F> where
F: FnOnce() -> T,
sourcepub fn force(this: &Self) -> &T
pub fn force(this: &Self) -> &T
Forces the evaluation of this lazy value and returns a reference to the result.
This is equivalent to the Deref impl, but is explicit.
Blocking
This method might block and should not be used from an
interrupt handler. Blocking is based on
crossbeam::utils::Backoff,
and will be reduced to a spin lock in #[no_std]
environments.
Examples
use atomic_once_cell::AtomicLazy;
let lazy = AtomicLazy::new(|| 92);
assert_eq!(AtomicLazy::force(&lazy), &92);
assert_eq!(&*lazy, &92);sourcepub fn init(this: &Self)
pub fn init(this: &Self)
Like AtomicLazy::force(), but without returing a reference.
Examples
use atomic_once_cell::AtomicLazy;
let lazy = AtomicLazy::new(|| 92);
AtomicLazy::init(&lazy);
assert_eq!(&*lazy, &92);sourcepub fn get(this: &Self) -> Option<&T>
pub fn get(this: &Self) -> Option<&T>
Gets the reference to the underlying value.
Returns None if the cell is not initialized.
Examples
use atomic_once_cell::AtomicLazy;
let lazy = AtomicLazy::new(|| 92);
assert_eq!(AtomicLazy::get(&lazy), None);
assert_eq!(AtomicLazy::force(&lazy), &92);
assert_eq!(AtomicLazy::get(&lazy), Some(&92));Trait Implementations
sourceimpl<T: Debug, F> Debug for AtomicLazy<T, F>
impl<T: Debug, F> Debug for AtomicLazy<T, F>
sourceimpl<T: Default> Default for AtomicLazy<T>
impl<T: Default> Default for AtomicLazy<T>
sourcefn default() -> AtomicLazy<T>
fn default() -> AtomicLazy<T>
Creates a new lazy value using Default as the initializing function.
sourceimpl<T, F> Deref for AtomicLazy<T, F> where
F: FnOnce() -> T,
impl<T, F> Deref for AtomicLazy<T, F> where
F: FnOnce() -> T,
impl<T, F> Sync for AtomicLazy<T, F> where
T: Send + Sync,
F: Send,
Auto Trait Implementations
impl<T, F = fn() -> T> !RefUnwindSafe for AtomicLazy<T, F>
impl<T, F> Send for AtomicLazy<T, F> where
F: Send,
T: Send,
impl<T, F> Unpin for AtomicLazy<T, F> where
F: Unpin,
T: Unpin,
impl<T, F> UnwindSafe for AtomicLazy<T, F> where
F: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more