read_write_api/impls/
parking_lot.rs

1use {
2    crate::{
3        DowngradableWriteApi,
4        DowngradableWriteGuard,
5        GuardedTarget,
6        ReadApi,
7        UpgradableReadApi,
8        UpgradableReadGuard,
9        WriteApi,
10    },
11    parking_lot::{RwLock, RwLockReadGuard, RwLockUpgradableReadGuard, RwLockWriteGuard},
12};
13
14impl<T> GuardedTarget for RwLock<T> {
15    type Target = T;
16}
17
18impl<T> GuardedTarget for &RwLock<T> {
19    type Target = T;
20}
21
22impl<T> GuardedTarget for &mut RwLock<T> {
23    type Target = T;
24}
25
26impl<T> ReadApi for RwLock<T>
27{
28    type ReadGuard<'a> = RwLockReadGuard<'a, T>
29        where Self: 'a;
30
31    #[inline]
32    fn read(&self) -> RwLockReadGuard<'_, T> {
33        RwLock::read(self)
34    }
35}
36
37impl<T> WriteApi for RwLock<T>
38{
39    type WriteGuard<'a> = &'a mut T
40        where Self: 'a;
41
42    #[inline]
43    fn write(&mut self) -> &mut T {
44        self.get_mut()
45    }
46}
47
48impl<T> UpgradableReadApi for RwLock<T>
49{
50    type UpgradableReadGuard<'a> = &'a mut T
51        where Self: 'a;
52
53    #[inline]
54    fn upgradable_read(&mut self) -> &mut T {
55        self.get_mut()
56    }
57}
58
59impl<T> DowngradableWriteApi for RwLock<T>
60{
61    type DowngradableWriteGuard<'a> = &'a mut T
62        where Self: 'a;
63
64    #[inline]
65    fn downgradable_write(&mut self) -> &mut T {
66        self.get_mut()
67    }
68}
69
70impl<T> ReadApi for &RwLock<T>
71{
72    type ReadGuard<'a> = RwLockReadGuard<'a, T>
73        where Self: 'a;
74
75    #[inline]
76    fn read(&self) -> RwLockReadGuard<'_, T> {
77        RwLock::read(self)
78    }
79}
80
81impl<T> WriteApi for &RwLock<T>
82{
83    type WriteGuard<'a> = RwLockWriteGuard<'a, T>
84        where Self: 'a;
85
86    #[inline]
87    fn write(&mut self) -> RwLockWriteGuard<'_, T> {
88        RwLock::write(self)
89    }
90}
91
92impl<T> UpgradableReadApi for &RwLock<T>
93{
94    type UpgradableReadGuard<'a> = RwLockUpgradableReadGuard<'a, T>
95        where Self: 'a;
96
97    #[inline]
98    fn upgradable_read(&mut self) -> RwLockUpgradableReadGuard<'_, T> {
99        RwLock::upgradable_read(self)
100    }
101}
102
103impl<T> DowngradableWriteApi for &RwLock<T>
104{
105    type DowngradableWriteGuard<'a> = RwLockWriteGuard<'a, T>
106        where Self: 'a;
107
108    #[inline]
109    fn downgradable_write(&mut self) -> RwLockWriteGuard<'_, T> {
110        RwLock::write(self)
111    }
112}
113
114impl<T> ReadApi for &mut RwLock<T>
115{
116    type ReadGuard<'a> = RwLockReadGuard<'a, T>
117        where Self: 'a;
118
119    #[inline]
120    fn read(&self) -> RwLockReadGuard<'_, T> {
121        RwLock::read(self)
122    }
123}
124
125impl<T> WriteApi for &mut RwLock<T>
126{
127    type WriteGuard<'a> = &'a mut T
128        where Self: 'a;
129
130    #[inline]
131    fn write(&mut self) -> &mut T {
132        self.get_mut()
133    }
134}
135
136impl<T> UpgradableReadApi for &mut RwLock<T>
137{
138    type UpgradableReadGuard<'a> = RwLockUpgradableReadGuard<'a, T>
139        where Self: 'a;
140
141    #[inline]
142    fn upgradable_read(&mut self) -> RwLockUpgradableReadGuard<'_, T> {
143        RwLock::upgradable_read(self)
144    }
145}
146
147impl<T> DowngradableWriteApi for &mut RwLock<T>
148{
149    type DowngradableWriteGuard<'a> = &'a mut T
150        where Self: 'a;
151
152    #[inline]
153    fn downgradable_write(&mut self) -> &mut T {
154        self.get_mut()
155    }
156}
157
158impl<'a, T> UpgradableReadGuard for RwLockUpgradableReadGuard<'a, T>
159{
160    type UpgradeResult = RwLockWriteGuard<'a, T>;
161    type UpgradeToDowngradableResult = RwLockWriteGuard<'a, T>;
162
163    #[inline]
164    fn upgrade(self) -> RwLockWriteGuard<'a, T> {
165        RwLockUpgradableReadGuard::upgrade(self)
166    }
167
168    #[inline]
169    fn upgrade_to_downgradable(self) -> RwLockWriteGuard<'a, T> {
170        RwLockUpgradableReadGuard::upgrade(self)
171    }
172}
173
174impl<'a, T> DowngradableWriteGuard for RwLockWriteGuard<'a, T>
175{
176    type DowngradeResult = RwLockReadGuard<'a, T>;
177    type DowngradeToUpgradableResult = RwLockUpgradableReadGuard<'a, T>;
178
179    #[inline]
180    fn downgrade(self) -> RwLockReadGuard<'a, T> {
181        RwLockWriteGuard::downgrade(self)
182    }
183
184    #[inline]
185    fn downgrade_to_upgradable(self) -> RwLockUpgradableReadGuard<'a, T> {
186        RwLockWriteGuard::downgrade_to_upgradable(self)
187    }
188}
189
190impl<'a, T: ?Sized> UpgradableReadGuard for &'a mut T
191{
192    type UpgradeResult = Self;
193    type UpgradeToDowngradableResult = Self;
194
195    #[inline(always)]
196    fn upgrade(self) -> Self {
197        self
198    }
199
200    #[inline(always)]
201    fn upgrade_to_downgradable(self) -> Self {
202        self
203    }
204}
205
206impl<'a, T: ?Sized> DowngradableWriteGuard for &'a mut T
207{
208    type DowngradeResult = Self;
209    type DowngradeToUpgradableResult = Self;
210
211    #[inline(always)]
212    fn downgrade(self) -> Self {
213        self
214    }
215
216    #[inline(always)]
217    fn downgrade_to_upgradable(self) -> Self {
218        self
219    }
220}