#container-storage #one-time #consumption #thread-safe #store #arc

take-once

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

4 releases

0.1.3 Aug 26, 2025
0.1.2 Dec 14, 2024
0.1.1 Dec 14, 2024
0.1.0 Dec 14, 2024

#370 in Concurrency

Download history 82/week @ 2025-12-09 102/week @ 2025-12-16 119/week @ 2025-12-23 96/week @ 2025-12-30 84/week @ 2026-01-06 101/week @ 2026-01-13 174/week @ 2026-01-20 130/week @ 2026-01-27 118/week @ 2026-02-03 82/week @ 2026-02-10 129/week @ 2026-02-17 120/week @ 2026-02-24 91/week @ 2026-03-03 111/week @ 2026-03-10 167/week @ 2026-03-17 101/week @ 2026-03-24

487 downloads per month
Used in 5 crates (4 directly)

MIT license

17KB
238 lines

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

No runtime deps