From 93e0a4215c915b98cf433ac6d0bcfbc60f0168ec Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Tue, 10 Oct 2017 20:08:11 -0700 Subject: Switch to parking_lot::{Mutex, RwLock} Switch to the `parking_lot` crate's implementations of `std::sync::Mutex` and `std::sync::RwLock`, which are more efficient. A writeup on why `parking_lot` is more efficient can be read here: Upgrade path: Modify `mutex.lock().unwrap()` usage to `mutex.lock()` (not needing to unwrap or handle a result), and `rwlock.read().unwrap()`/`rwlock.write().unwrap()` usage to `rwlock.read()` and `rwlock.write()`. For example, modify: ```rust use serenity::CACHE; println!("{}", CACHE.read().unwrap().user.id); ``` to: ```rust use serenity::CACHE; println!("{}", CACHE.read().user.id); ``` --- src/internal/rwlock_ext.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/internal') diff --git a/src/internal/rwlock_ext.rs b/src/internal/rwlock_ext.rs index 8266cdf..6235370 100644 --- a/src/internal/rwlock_ext.rs +++ b/src/internal/rwlock_ext.rs @@ -1,3 +1,4 @@ +use parking_lot::RwLock as ParkingLotRwLock; use std::sync::{Arc, RwLock}; pub trait RwLockExt { @@ -16,3 +17,15 @@ impl RwLockExt for Arc> { f(&mut w) } } + +impl RwLockExt for Arc> { + fn with Y>(&self, f: F) -> Y { + let r = self.read(); + f(&r) + } + + fn with_mut Y>(&self, mut f: F) -> Y { + let mut w = self.write(); + f(&mut w) + } +} -- cgit v1.2.3