Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
feat: Implement Defalt, From. derive Debug
Browse files Browse the repository at this point in the history
Remove load_is_empty
  • Loading branch information
mattrutherford committed Dec 11, 2018
1 parent 775dd31 commit 5d7b350
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
4 changes: 1 addition & 3 deletions util/len-caching-lock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
//! 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());
//! // Also has convenience method to check if empty
//! assert!(!len_caching_mutex.load_is_empty());
//! }
//! ```
Expand All @@ -57,7 +55,7 @@ pub trait Len {
}

impl<T> Len for Vec<T> {
fn len(&self) -> usize { self.len() }
fn len(&self) -> usize { Vec::len(self) }
}

impl<T> Len for VecDeque<T> {
Expand Down
29 changes: 14 additions & 15 deletions util/len-caching-lock/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,27 @@ use Len;
/// needing to lock, is advantageous.
/// When the Guard is released, `T`'s `len()` will be cached.
/// The cached `len()` may be at most 1 lock behind current state.
#[derive(Debug)]
pub struct LenCachingMutex<T> {
data: Mutex<T>,
len: AtomicUsize,
}

impl<T: Len + Default> Default for LenCachingMutex<T> {
fn default() -> Self {
LenCachingMutex::new(T::default())
}
}

impl<T: Len> From<T> for LenCachingMutex<T> {
fn from(data: T) -> Self {
LenCachingMutex::new(data)
}
}

impl<T: Len> LenCachingMutex<T> {
/// Constructs a new LenCachingMutex
pub fn new(data: T) -> LenCachingMutex<T> {
pub fn new(data: T) -> Self {
LenCachingMutex {
len: AtomicUsize::new(data.len()),
data: Mutex::new(data),
Expand All @@ -46,11 +59,6 @@ impl<T: Len> LenCachingMutex<T> {
self.len.load(Ordering::SeqCst)
}

/// Convenience method to check if collection T `is_empty()`
pub fn load_is_empty(&self) -> bool {
self.len.load(Ordering::SeqCst) == 0
}

/// Delegates to `parking_lot::Mutex`
/// [`lock()`](../../lock_api/struct.Mutex.html#method.lock).
pub fn lock(&self) -> CachingMutexGuard<T> {
Expand Down Expand Up @@ -137,13 +145,4 @@ mod tests {
lcm.lock().push_front(4);
assert_eq!(lcm.load_len(), 1);
}

#[test]
fn load_is_empty_works() {
let v = vec![1,2,3];
let lcm = LenCachingMutex::new(v);
assert!(!lcm.load_is_empty());
lcm.lock().clear();
assert!(lcm.load_is_empty());
}
}
29 changes: 14 additions & 15 deletions util/len-caching-lock/src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,27 @@ use Len;
/// Can be used in place of a [`RwLock`](../../lock_api/struct.RwLock.html) where
/// reading `T`'s `len()` without needing to lock, is advantageous.
/// When the WriteGuard is released, `T`'s `len()` will be cached.
#[derive(Debug)]
pub struct LenCachingRwLock<T> {
data: RwLock<T>,
len: AtomicUsize,
}

impl<T: Len + Default> Default for LenCachingRwLock<T> {
fn default() -> Self {
LenCachingRwLock::new(T::default())
}
}

impl<T: Len> From<T> for LenCachingRwLock<T> {
fn from(data: T) -> Self {
LenCachingRwLock::new(data)
}
}

impl<T: Len> LenCachingRwLock<T> {
/// Constructs a new LenCachingRwLock
pub fn new(data: T) -> LenCachingRwLock<T> {
pub fn new(data: T) -> Self {
LenCachingRwLock {
len: AtomicUsize::new(data.len()),
data: RwLock::new(data),
Expand All @@ -45,11 +58,6 @@ impl<T: Len> LenCachingRwLock<T> {
self.len.load(Ordering::SeqCst)
}

/// Convenience method to check if collection T `is_empty()`
pub fn load_is_empty(&self) -> bool {
self.len.load(Ordering::SeqCst) == 0
}

/// Delegates to `parking_lot::RwLock`
/// [`write()`](../../lock_api/struct.RwLock.html#method.write).
pub fn write(&self) -> CachingRwLockWriteGuard<T> {
Expand Down Expand Up @@ -149,15 +157,6 @@ mod tests {
assert_eq!(lcl.load_len(), 1);
}

#[test]
fn load_is_empty_works() {
let v = vec![1,2,3];
let lcl = LenCachingRwLock::new(v);
assert!(!lcl.load_is_empty());
lcl.write().clear();
assert!(lcl.load_is_empty());
}

#[test]
fn read_works() {
let v = vec![1,2,3];
Expand Down

0 comments on commit 5d7b350

Please sign in to comment.