take-once 0.1.3

A thread-safe container for one-time storage and one-time consumption of a value.
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented3 out of 7 items with examples
  • Size
  • Source code size: 28.64 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.54 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • AmitPr/take-once
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • AmitPr

take-once

License Cargo Documentation

A thread-safe container for one-time storage and one-time consumption of a value.

Usage

use take_once::TakeOnce;

let cell = TakeOnce::new();

// Initial store succeeds
assert_eq!(cell.store(42), None);

// Subsequent stores return the provided value
assert_eq!(cell.store(24), Some(24));

// Take the value
assert_eq!(cell.take(), Some(42));

// Can't take twice
assert_eq!(cell.take(), None);

// Can be used across threads via `Arc`
use std::sync::Arc;
use std::thread;

let cell = Arc::new(TakeOnce::new());

let cell_clone = cell.clone();
let handle = thread::spawn(move || {
    assert_eq!(cell_clone.take(), Some(42));
});
handle.join().unwrap();

assert_eq!(cell.take(), Some(42));

See the documentation for more details.

Testing

This crate uses shuttle for (more) exhaustive testing. To run the shuttle tests, run:

cargo test --features _shuttle

License

MIT