https://github.com/ibraheemdev/boxcar
A concurrent, append-only vector.
https://github.com/ibraheemdev/boxcar
Last synced: about 1 year ago
JSON representation
A concurrent, append-only vector.
- Host: GitHub
- URL: https://github.com/ibraheemdev/boxcar
- Owner: ibraheemdev
- License: mit
- Created: 2022-03-13T20:01:38.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-05-15T11:27:24.000Z (about 1 year ago)
- Last Synced: 2025-05-15T11:32:15.601Z (about 1 year ago)
- Language: Rust
- Size: 73.2 KB
- Stars: 164
- Watchers: 4
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `boxcar`
[
](https://crates.io/crates/boxcar)
[
](https://github.com/ibraheemdev/boxcar)
[
](https://docs.rs/boxcar)
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:
```rust
let vec = boxcar::Vec::new();
let i = vec.push(42);
assert_eq!(vec[i], 42);
```
The vector can be modified by multiple threads concurrently:
```rust
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:
```rust
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);
```