| Crates.io | take-once |
| lib.rs | take-once |
| version | 0.1.3 |
| created_at | 2024-12-14 02:28:50.863838+00 |
| updated_at | 2025-08-26 23:15:50.332286+00 |
| description | A thread-safe container for one-time storage and one-time consumption of a value. |
| homepage | |
| repository | https://github.com/AmitPr/take-once |
| max_upload_size | |
| id | 1482798 |
| size | 28,642 |
A thread-safe container for one-time storage and one-time consumption of a value.
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.
This crate uses shuttle for (more) exhaustive testing. To run the shuttle tests, run:
cargo test --features _shuttle
MIT