pub struct ImmutableCell<T> { /* private fields */ }Implementations§
Source§impl<T> ImmutableCell<T>
impl<T> ImmutableCell<T>
Methods from Deref<Target = OnceLock<T>>§
1.70.0 · Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets the reference to the underlying value.
Returns None if the cell is uninitialized, or being initialized.
This method never blocks.
1.86.0 · Sourcepub fn wait(&self) -> &T
pub fn wait(&self) -> &T
Blocks the current thread until the cell is initialized.
§Example
Waiting for a computation on another thread to finish:
use std::thread;
use std::sync::OnceLock;
let value = OnceLock::new();
thread::scope(|s| {
s.spawn(|| value.set(1 + 1));
let result = value.wait();
assert_eq!(result, &2);
})1.70.0 · Sourcepub fn set(&self, value: T) -> Result<(), T>
pub fn set(&self, value: T) -> Result<(), T>
Initializes the contents of the cell to value.
May block if another thread is currently attempting to initialize the cell. The cell is
guaranteed to contain a value when set returns, though not necessarily the one provided.
Returns Ok(()) if the cell was uninitialized and
Err(value) if the cell was already initialized.
§Examples
use std::sync::OnceLock;
static CELL: OnceLock<i32> = OnceLock::new();
fn main() {
assert!(CELL.get().is_none());
std::thread::spawn(|| {
assert_eq!(CELL.set(92), Ok(()));
}).join().unwrap();
assert_eq!(CELL.set(62), Err(62));
assert_eq!(CELL.get(), Some(&92));
}Sourcepub fn try_insert(&self, value: T) -> Result<&T, (&T, T)>
🔬This is a nightly-only experimental API. (once_cell_try_insert)
pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)>
once_cell_try_insert)Initializes the contents of the cell to value if the cell was uninitialized,
then returns a reference to it.
May block if another thread is currently attempting to initialize the cell. The cell is
guaranteed to contain a value when try_insert returns, though not necessarily the
one provided.
Returns Ok(&value) if the cell was uninitialized and
Err((¤t_value, value)) if it was already initialized.
§Examples
#![feature(once_cell_try_insert)]
use std::sync::OnceLock;
static CELL: OnceLock<i32> = OnceLock::new();
fn main() {
assert!(CELL.get().is_none());
std::thread::spawn(|| {
assert_eq!(CELL.try_insert(92), Ok(&92));
}).join().unwrap();
assert_eq!(CELL.try_insert(62), Err((&92, 62)));
assert_eq!(CELL.get(), Some(&92));
}1.70.0 · Sourcepub fn get_or_init<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
pub fn get_or_init<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
Gets the contents of the cell, initializing it to f() if the cell
was uninitialized.
Many threads may call get_or_init concurrently with different
initializing functions, but it is guaranteed that only one function
will be executed.
§Panics
If f() panics, the panic is propagated to the caller, and the cell
remains uninitialized.
It is an error to reentrantly initialize the cell from f. The
exact outcome is unspecified. Current implementation deadlocks, but
this may be changed to a panic in the future.
§Examples
use std::sync::OnceLock;
let cell = OnceLock::new();
let value = cell.get_or_init(|| 92);
assert_eq!(value, &92);
let value = cell.get_or_init(|| unreachable!());
assert_eq!(value, &92);Sourcepub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
🔬This is a nightly-only experimental API. (once_cell_try)
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
once_cell_try)Gets the contents of the cell, initializing it to f() if
the cell was uninitialized. If the cell was uninitialized
and f() failed, an error is returned.
§Panics
If f() panics, the panic is propagated to the caller, and
the cell remains uninitialized.
It is an error to reentrantly initialize the cell from f.
The exact outcome is unspecified. Current implementation
deadlocks, but this may be changed to a panic in the future.
§Examples
#![feature(once_cell_try)]
use std::sync::OnceLock;
let cell = OnceLock::new();
assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
assert!(cell.get().is_none());
let value = cell.get_or_try_init(|| -> Result<i32, ()> {
Ok(92)
});
assert_eq!(value, Ok(&92));
assert_eq!(cell.get(), Some(&92))