read-write-store

Crates.ioread-write-store
lib.rsread-write-store
version0.2.0
sourcesrc
created_at2021-09-27 12:08:03.423457
updated_at2021-10-15 10:08:41.503088
descriptionA concurrent, unordered collection for Rust, where each element has an internally generated ID and a read-write lock.
homepagehttps://github.com/LlewVallis/read-write-store
repositoryhttps://github.com/LlewVallis/read-write-store
max_upload_size
id456960
size124,740
(LlewVallis)

documentation

https://docs.rs/read-write-store

README

Read Write Store

Crates.io docs.rs

A concurrent, unordered collection for Rust, where each element has an internally generated ID and a read-write lock.

A store has O(1) time complexity for insertion, removal and lookups, although memory allocations triggered by insertion may cause a performance spike.

Example

// Note that we only need an immutable reference to the store
let store = RwStore::new();

// Inserting an element yields an ID
let id = store.insert(42);

{
    // You can read the element's value using that ID
    let read_lock = store.read(id).unwrap();
    assert_eq!(*read_lock, 42);

    // Concurrent reads are possible
    assert!(store.read_with_timeout(id, DontBlock).is_ok());
    // But reading and writing at the same time won't work
    assert!(store.write_with_timeout(id, DontBlock).is_err());
}

{
    // You can also acquire a write lock using an ID
    let mut write_lock = store.write(id).unwrap();
    *write_lock = 24;
    assert_eq!(*write_lock, 24);

    // Predictably, you cannot have multiple writers
    assert!(store.write_with_timeout(id, DontBlock).is_err());
}

// Elements can of course be removed using their ID
assert_eq!(store.remove(id), Some(24));

// Now if we try to read using the ID, it will fail gracefully
assert!(store.read(id).is_none());

Documentation

The latest documentation can be found here.

Commit count: 12

cargo fmt