pub struct LazyLock<T, F = fn() -> T, R = Spin> { /* private fields */ }Expand description
A value which is initialized on the first access.
This type is a thread-safe LazyLock, and can be used in statics.
§Examples
use std::collections::HashMap;
use spin::LazyLock;
static HASHMAP: LazyLock<HashMap<i32, String>> = LazyLock::new(|| {
println!("initializing");
let mut m = HashMap::new();
m.insert(13, "Spica".to_string());
m.insert(74, "Hoyten".to_string());
m
});
fn main() {
println!("ready");
std::thread::spawn(|| {
println!("{:?}", HASHMAP.get(&13));
}).join().unwrap();
println!("{:?}", HASHMAP.get(&74));
// Prints:
// ready
// initializing
// Some("Spica")
// Some("Hoyten")
}Implementations§
Source§impl<T, F, R> LazyLock<T, F, R>
impl<T, F, R> LazyLock<T, F, R>
Sourcepub fn as_mut_ptr(&self) -> *mut T
pub fn as_mut_ptr(&self) -> *mut T
Retrieves a mutable pointer to the inner data.
This is especially useful when interfacing with low level code or FFI where the caller explicitly knows that it has exclusive access to the inner data. Note that reading from this pointer is UB until initialized or directly written to.
Source§impl<T, F: FnOnce() -> T, R: RelaxStrategy> LazyLock<T, F, R>
impl<T, F: FnOnce() -> T, R: RelaxStrategy> LazyLock<T, F, R>
Trait Implementations§
impl<T, F: Send> Sync for LazyLock<T, F>
Auto Trait Implementations§
impl<T, F = fn() -> T, R = Spin> !Freeze for LazyLock<T, F, R>
impl<T, F = fn() -> T, R = Spin> !RefUnwindSafe for LazyLock<T, F, R>
impl<T, F = fn() -> T, R = Spin> !Sync for LazyLock<T, F, R>
impl<T, F, R> Send for LazyLock<T, F, R>
impl<T, F, R> Unpin for LazyLock<T, F, R>
impl<T, F, R> UnsafeUnpin for LazyLock<T, F, R>where
F: UnsafeUnpin,
T: UnsafeUnpin,
impl<T, F, R> UnwindSafe for LazyLock<T, F, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more