#value #bag #locking #container #pattern #ibag

ibag

A thread-safe, immutable bag for holding any value

6 releases

new 0.3.4 Apr 16, 2025
0.3.3 Apr 10, 2025
0.2.1 Apr 9, 2025

#243 in Concurrency

Download history 259/week @ 2025-04-04 289/week @ 2025-04-11

548 downloads per month
Used in 2 crates (via rioc)

MIT license

22KB
447 lines

iBag - Thread-safe Immutable Bag

A Rust library providing a thread-safe, immutable bag container that allows safe concurrent access to shared data.

Features

  • Thread-safe immutable container
  • Read and write operations with automatic locking
  • Closure-based access patterns
  • Automatic Clone, Send and Sync implementations

Installation

Add this to your Cargo.toml:

[dependencies]
ibag = "0.3"

Usage

use ibag::iBag;

// Create a new iBag
let bag = iBag::new(42);

// Read access
let value = bag.with_read(|val| *val);
assert_eq!(value, 42);

// Write access
bag.with(|val| *val = 100);

// Thread-safe operations
let bag = Arc::new(bag);
let handles: Vec<_> = (0..10).map(|_| {
    let bag = bag.clone();
    thread::spawn(move || {
        bag.with(|val| *val += 1);
    })
}).collect();

for handle in handles {
    handle.join().unwrap();
}

API Documentation

See the API documentation for complete details.

License

MIT

No runtime deps