Expand description

This crate provides the WaitCell type.

A WaitCell<T> is a thread-safe cell which can only be written once while shared. Attempts to read the value before it is written cause the reading thread to block. In this way, it is like a “synchronous” Future.

Because the value can be written to only once while shared, WaitCell provides access to its value by shared reference (&T). To use this reference from multiple threads, T must be Sync.

Example

use std::time::Duration;
use std::thread::{ sleep, spawn };
use waitcell::WaitCell;

static CELL: WaitCell<u64> = WaitCell::new();

fn main() {
    let setter = spawn(|| {
        sleep(Duration::from_secs(1));
        CELL.init(42);
    });

    // Prints "42".
    println!("{}", CELL.get());
}

License

waitcell is licensed under the terms of the Apache License, Version 2.0 or the MIT License.

Development

waitcell is developed at GitLab.

Structs

A cell type containing a value which may not yet be available.