parking_method/
writelock_method.rs1use crate::*;
2
3pub trait WriteLockMethod {
5 fn write<'a, V>(&self, rwlock: &'a RwLock<V>) -> Option<RwLockWriteGuard<'a, V>>;
7}
8
9macro_rules! impl_locking_method {
10 ($policy:ty, $write:expr) => {
11 impl WriteLockMethod for $policy {
12 #[inline(always)]
13 #[allow(unused_variables)]
14 fn write<'a, V>(&self, rwlock: &'a RwLock<V>) -> Option<RwLockWriteGuard<'a, V>> {
15 #[allow(unused_macros)]
16 macro_rules! method {
17 () => {
18 self
19 };
20 }
21 #[allow(unused_macros)]
22 macro_rules! lock {
23 () => {
24 rwlock
25 };
26 }
27 $write
28 }
29 }
30 };
31}
32
33impl_locking_method!(Blocking, Some(lock!().write()));
34
35impl_locking_method!(TryLock, lock!().try_write());
36
37impl_locking_method!(Duration, lock!().try_write_for(*method!()));
38
39impl_locking_method!(Instant, lock!().try_write_until(*method!()));
40
41#[cfg(test)]
42mod test {
43 use crate::*;
44
45 #[test]
46 fn smoke() {
47 let rwlock = RwLock::new(String::from("test"));
48 assert_eq!(*WriteLockMethod::write(&Blocking, &rwlock).unwrap(), "test");
49 }
50
51 #[test]
52 fn trylocks() {
53 let rwlock = RwLock::new(String::from("test"));
54
55 assert_eq!(*WriteLockMethod::write(&TryLock, &rwlock).unwrap(), "test");
56 assert_eq!(
57 *WriteLockMethod::write(&Duration::from_millis(100), &rwlock).unwrap(),
58 "test"
59 );
60 assert_eq!(
61 *WriteLockMethod::write(&(Instant::now() + Duration::from_millis(100)), &rwlock)
62 .unwrap(),
63 "test"
64 );
65 }
66}