aboutsummaryrefslogtreecommitdiff
path: root/src/internal/rwlock_ext.rs
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-08-24 15:26:49 +0200
committeracdenisSK <[email protected]>2017-08-24 16:36:01 +0200
commitb3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3 (patch)
tree315e16f7b252d22b5f832302e722a85c9e6a9b6e /src/internal/rwlock_ext.rs
parentAllow FromStr for User to use REST (#147) (diff)
downloadserenity-b3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3.tar.xz
serenity-b3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3.zip
Revamp `RwLock` usage in the lib
Also not quite sure if they goofed rustfmt or something, but its changes it did were a bit bizarre.
Diffstat (limited to 'src/internal/rwlock_ext.rs')
-rw-r--r--src/internal/rwlock_ext.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/internal/rwlock_ext.rs b/src/internal/rwlock_ext.rs
new file mode 100644
index 0000000..8266cdf
--- /dev/null
+++ b/src/internal/rwlock_ext.rs
@@ -0,0 +1,18 @@
+use std::sync::{Arc, RwLock};
+
+pub trait RwLockExt<T> {
+ fn with<Y, F: Fn(&T) -> Y>(&self, f: F) -> Y;
+ fn with_mut<Y, F: FnMut(&mut T) -> Y>(&self, f: F) -> Y;
+}
+
+impl<T> RwLockExt<T> for Arc<RwLock<T>> {
+ fn with<Y, F: Fn(&T) -> Y>(&self, f: F) -> Y {
+ let r = self.read().unwrap();
+ f(&r)
+ }
+
+ fn with_mut<Y, F: FnMut(&mut T) -> Y>(&self, mut f: F) -> Y {
+ let mut w = self.write().unwrap();
+ f(&mut w)
+ }
+}