2 个不稳定版本
0.2.0 | 2021 年 10 月 15 日 |
---|---|
0.1.0 | 2021 年 9 月 27 日 |
566 在 并发 中
115KB
3K SLoC
读写存储
一个 Rust 的并发、无序集合,其中每个元素都有一个内部生成的 ID 和一个读写锁。
存储的插入、删除和查找的时间复杂度为 O(1),尽管插入引起的内存分配可能会导致性能波动。
示例
// 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());
文档
最新文档可在此处找到:https://docs.rs/read-write-store。
依赖项
~485KB