13 releases

0.2.12 May 15, 2025
0.2.11 Mar 20, 2025
0.2.10 Feb 17, 2025
0.2.8 Dec 29, 2024
0.1.0 Mar 13, 2022

#32 in Concurrency

Download history 25684/week @ 2025-02-02 42270/week @ 2025-02-09 32372/week @ 2025-02-16 33152/week @ 2025-02-23 38709/week @ 2025-03-02 45110/week @ 2025-03-09 41414/week @ 2025-03-16 37942/week @ 2025-03-23 40608/week @ 2025-03-30 40546/week @ 2025-04-06 109563/week @ 2025-04-13 186707/week @ 2025-04-20 148539/week @ 2025-04-27 178262/week @ 2025-05-04 202217/week @ 2025-05-11 201526/week @ 2025-05-18

736,024 downloads per month
Used in 105 crates (22 directly)

MIT license

45KB
762 lines

boxcar

crates.io github docs.rs

A concurrent, append-only vector.

The vector provided by this crate supports lock-free get and push operations. The vector grows internally but never reallocates, so element addresses are stable for the lifetime of the vector. Additionally, both get and push run in constant-time.

Examples

Appending an element to a vector and retrieving it:

let vec = boxcar::Vec::new();
let i = vec.push(42);
assert_eq!(vec[i], 42);

The vector can be modified by multiple threads concurrently:

let vec = boxcar::Vec::new();

// Spawn a few threads that append to the vector.
std::thread::scope(|s| for i in 0..6 {
    let vec = &vec;

    s.spawn(move || {
        // Push through the shared reference.
        vec.push(i);
    });
});

for i in 0..6 {
    assert!(vec.iter().any(|(_, &x)| x == i));
}

Elements can be mutated through fine-grained locking:

let vec = boxcar::Vec::new();

std::thread::scope(|s| {
    // Insert an element.
    vec.push(std::sync::Mutex::new(0));

    s.spawn(|| {
        // Mutate through the lock.
        *vec[0].lock().unwrap() += 1;
    });
});

let x = vec[0].lock().unwrap();
assert_eq!(*x, 1);

Dependencies

~0–18MB
~292K SLoC