#len #collection #lock #cache #mutex #rw-lock #parking-lot

len-caching-lock

Atomically cached len(), for use with collections contained in parking_lot Mutex and RwLock

Show the crate…

1 unstable release

Uses old Rust 2015

0.1.1 Feb 28, 2021

#9 in #parking-lot


Used in 9 crates (via verification)

GPL-3.0 license

13KB
244 lines

This crate allows automatic caching of T.len() with an api that allows drop in replacement for parking_lot Mutex and RwLock for most common use-cases.

This crate implements Len for the following types: std::collections::{VecDeque, LinkedList, HashMap, BTreeMap, HashSet, BTreeSet, BinaryHeap}

Example

extern crate len_caching_lock;
use len_caching_lock::LenCachingMutex;

fn main() {
   	let vec: Vec<i32> = Vec::new();
   	let len_caching_mutex = LenCachingMutex::new(vec);
   	assert_eq!(len_caching_mutex.lock().len(), len_caching_mutex.load_len());
   	len_caching_mutex.lock().push(0);
   	assert_eq!(1, len_caching_mutex.load_len());
   }
   ```

Dependencies

~0.6–1MB
~14K SLoC