Struct ImmutableCell

Source
pub struct ImmutableCell<T> { /* private fields */ }

Implementations§

Source§

impl<T> ImmutableCell<T>

Source

pub fn new() -> ImmutableCell<T>

Source

pub fn get_or_init<F>(&self, f: F) -> &T
where F: FnOnce() -> T,

Source

pub fn get_or<E>(&self, error: E) -> Result<&T, E>

Methods from Deref<Target = OnceLock<T>>§

1.70.0 · Source

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 · Source

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 · Source

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));
}
Source

pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)>

🔬This is a nightly-only experimental API. (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((&current_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 · Source

pub fn get_or_init<F>(&self, f: F) -> &T
where 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);
Source

pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
where F: FnOnce() -> Result<T, E>,

🔬This is a nightly-only experimental API. (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))

Trait Implementations§

Source§

impl<T> Default for ImmutableCell<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Deref for ImmutableCell<T>

Source§

type Target = OnceLock<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> !Freeze for ImmutableCell<T>

§

impl<T> RefUnwindSafe for ImmutableCell<T>

§

impl<T> Send for ImmutableCell<T>
where T: Send,

§

impl<T> Sync for ImmutableCell<T>
where T: Sync + Send,

§

impl<T> Unpin for ImmutableCell<T>
where T: Unpin,

§

impl<T> UnwindSafe for ImmutableCell<T>
where T: UnwindSafe,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V