1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::{ops, sync};
#[derive(Default, Debug)]
pub struct Mutex<T>(sync::Mutex<T>);
impl<T> Mutex<T> {
pub fn new(value: T) -> Self {
Self(sync::Mutex::new(value))
}
pub fn lock(&self) -> MutexGuard<'_, T> {
let guard = self.0.lock().unwrap();
MutexGuard(guard)
}
pub fn into_inner(self) -> T {
self.0.into_inner().unwrap()
}
}
pub struct MutexGuard<'a, T>(sync::MutexGuard<'a, T>);
impl<'a, T> ops::Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> ops::DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Default, Debug)]
pub struct RwLock<T>(sync::RwLock<T>);
impl<T> RwLock<T> {
pub fn new(value: T) -> Self {
Self(sync::RwLock::new(value))
}
pub fn read(&self) -> RwLockReadGuard<'_, T> {
let guard = self.0.read().unwrap();
RwLockReadGuard(guard)
}
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
let guard = self.0.write().unwrap();
RwLockWriteGuard(guard)
}
}
pub struct RwLockReadGuard<'a, T>(sync::RwLockReadGuard<'a, T>);
impl<'a, T> ops::Deref for RwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct RwLockWriteGuard<'a, T>(sync::RwLockWriteGuard<'a, T>);
impl<'a, T> ops::Deref for RwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> ops::DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}