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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::fmt;
use std::future::Future;
use std::mem::ManuallyDrop;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use super::raw::{RawRead, RawUpgradableRead, RawUpgrade, RawWrite};
use super::{
    RwLock, RwLockReadGuard, RwLockReadGuardArc, RwLockUpgradableReadGuard,
    RwLockUpgradableReadGuardArc, RwLockWriteGuard, RwLockWriteGuardArc,
};

/// The future returned by [`RwLock::read`].
pub struct Read<'a, T: ?Sized> {
    /// Raw read lock acquisition future, doesn't depend on `T`.
    pub(super) raw: RawRead<'a>,

    /// Pointer to the value protected by the lock. Covariant in `T`.
    pub(super) value: *const T,
}

unsafe impl<T: Sync + ?Sized> Send for Read<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for Read<'_, T> {}

impl<T: ?Sized> fmt::Debug for Read<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Read { .. }")
    }
}

impl<T: ?Sized> Unpin for Read<'_, T> {}

impl<'a, T: ?Sized> Future for Read<'a, T> {
    type Output = RwLockReadGuard<'a, T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(Pin::new(&mut self.raw).poll(cx));

        Poll::Ready(RwLockReadGuard {
            lock: self.raw.lock,
            value: self.value,
        })
    }
}

/// The future returned by [`RwLock::read_arc`].
pub struct ReadArc<'a, T> {
    /// Raw read lock acquisition future, doesn't depend on `T`.
    pub(super) raw: RawRead<'a>,

    // FIXME: Could be covariant in T
    pub(super) lock: &'a Arc<RwLock<T>>,
}

unsafe impl<T: Send + Sync> Send for ReadArc<'_, T> {}
unsafe impl<T: Send + Sync> Sync for ReadArc<'_, T> {}

impl<T> fmt::Debug for ReadArc<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("ReadArc { .. }")
    }
}

impl<T> Unpin for ReadArc<'_, T> {}

impl<'a, T> Future for ReadArc<'a, T> {
    type Output = RwLockReadGuardArc<T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(Pin::new(&mut self.raw).poll(cx));

        // SAFETY: we just acquired a read lock
        Poll::Ready(unsafe { RwLockReadGuardArc::from_arc(self.lock.clone()) })
    }
}

/// The future returned by [`RwLock::upgradable_read`].
pub struct UpgradableRead<'a, T: ?Sized> {
    /// Raw upgradable read lock acquisition future, doesn't depend on `T`.
    pub(super) raw: RawUpgradableRead<'a>,

    /// Pointer to the value protected by the lock. Invariant in `T`
    /// as the upgradable lock could provide write access.
    pub(super) value: *mut T,
}

unsafe impl<T: Send + Sync + ?Sized> Send for UpgradableRead<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for UpgradableRead<'_, T> {}

impl<T: ?Sized> fmt::Debug for UpgradableRead<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("UpgradableRead { .. }")
    }
}

impl<T: ?Sized> Unpin for UpgradableRead<'_, T> {}

impl<'a, T: ?Sized> Future for UpgradableRead<'a, T> {
    type Output = RwLockUpgradableReadGuard<'a, T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(Pin::new(&mut self.raw).poll(cx));

        Poll::Ready(RwLockUpgradableReadGuard {
            lock: self.raw.lock,
            value: self.value,
        })
    }
}

/// The future returned by [`RwLock::upgradable_read_arc`].
pub struct UpgradableReadArc<'a, T: ?Sized> {
    /// Raw upgradable read lock acquisition future, doesn't depend on `T`.
    pub(super) raw: RawUpgradableRead<'a>,

    pub(super) lock: &'a Arc<RwLock<T>>,
}

unsafe impl<T: Send + Sync + ?Sized> Send for UpgradableReadArc<'_, T> {}
unsafe impl<T: Send + Sync + ?Sized> Sync for UpgradableReadArc<'_, T> {}

impl<T: ?Sized> fmt::Debug for UpgradableReadArc<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("UpgradableReadArc { .. }")
    }
}

impl<T: ?Sized> Unpin for UpgradableReadArc<'_, T> {}

impl<'a, T: ?Sized> Future for UpgradableReadArc<'a, T> {
    type Output = RwLockUpgradableReadGuardArc<T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(Pin::new(&mut self.raw).poll(cx));
        Poll::Ready(RwLockUpgradableReadGuardArc {
            lock: self.lock.clone(),
        })
    }
}

/// The future returned by [`RwLock::write`].
pub struct Write<'a, T: ?Sized> {
    /// Raw write lock acquisition future, doesn't depend on `T`.
    pub(super) raw: RawWrite<'a>,

    /// Pointer to the value protected by the lock. Invariant in `T`.
    pub(super) value: *mut T,
}

unsafe impl<T: Send + ?Sized> Send for Write<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for Write<'_, T> {}

impl<T: ?Sized> fmt::Debug for Write<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Write { .. }")
    }
}

impl<T: ?Sized> Unpin for Write<'_, T> {}

impl<'a, T: ?Sized> Future for Write<'a, T> {
    type Output = RwLockWriteGuard<'a, T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(Pin::new(&mut self.raw).poll(cx));

        Poll::Ready(RwLockWriteGuard {
            lock: self.raw.lock,
            value: self.value,
        })
    }
}

/// The future returned by [`RwLock::write_arc`].
pub struct WriteArc<'a, T: ?Sized> {
    /// Raw write lock acquisition future, doesn't depend on `T`.
    pub(super) raw: RawWrite<'a>,

    pub(super) lock: &'a Arc<RwLock<T>>,
}

unsafe impl<T: Send + Sync + ?Sized> Send for WriteArc<'_, T> {}
unsafe impl<T: Send + Sync + ?Sized> Sync for WriteArc<'_, T> {}

impl<T: ?Sized> fmt::Debug for WriteArc<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("WriteArc { .. }")
    }
}

impl<T: ?Sized> Unpin for WriteArc<'_, T> {}

impl<'a, T: ?Sized> Future for WriteArc<'a, T> {
    type Output = RwLockWriteGuardArc<T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(Pin::new(&mut self.raw).poll(cx));

        Poll::Ready(RwLockWriteGuardArc {
            lock: self.lock.clone(),
        })
    }
}

/// The future returned by [`RwLockUpgradableReadGuard::upgrade`].
pub struct Upgrade<'a, T: ?Sized> {
    /// Raw read lock upgrade future, doesn't depend on `T`.
    pub(super) raw: RawUpgrade<'a>,

    /// Pointer to the value protected by the lock. Invariant in `T`.
    pub(super) value: *mut T,
}

unsafe impl<T: Send + ?Sized> Send for Upgrade<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for Upgrade<'_, T> {}

impl<T: ?Sized> fmt::Debug for Upgrade<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Upgrade").finish()
    }
}

impl<T: ?Sized> Unpin for Upgrade<'_, T> {}

impl<'a, T: ?Sized> Future for Upgrade<'a, T> {
    type Output = RwLockWriteGuard<'a, T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let lock = ready!(Pin::new(&mut self.raw).poll(cx));

        Poll::Ready(RwLockWriteGuard {
            lock,
            value: self.value,
        })
    }
}

/// The future returned by [`RwLockUpgradableReadGuardArc::upgrade`].
pub struct UpgradeArc<T: ?Sized> {
    /// Raw read lock upgrade future, doesn't depend on `T`.
    /// `'static` is a lie, this field is actually referencing the
    /// `Arc` data. But since this struct also stores said `Arc`, we know
    /// this value will be alive as long as the struct is.
    ///
    /// Yes, one field of the `ArcUpgrade` struct is referencing another.
    /// Such self-references are usually not sound without pinning.
    /// However, in this case, there is an indirection via the heap;
    /// moving the `ArcUpgrade` won't move the heap allocation of the `Arc`,
    /// so the reference inside `RawUpgrade` isn't invalidated.
    pub(super) raw: ManuallyDrop<RawUpgrade<'static>>,

    /// Pointer to the value protected by the lock. Invariant in `T`.
    pub(super) lock: ManuallyDrop<Arc<RwLock<T>>>,
}

impl<T: ?Sized> fmt::Debug for UpgradeArc<T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ArcUpgrade").finish()
    }
}

impl<T: ?Sized> Unpin for UpgradeArc<T> {}

impl<T: ?Sized> Future for UpgradeArc<T> {
    type Output = RwLockWriteGuardArc<T>;

    #[inline]
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        ready!(Pin::new(&mut *self.raw).poll(cx));

        Poll::Ready(RwLockWriteGuardArc {
            lock: unsafe { ManuallyDrop::take(&mut self.lock) },
        })
    }
}

impl<T: ?Sized> Drop for UpgradeArc<T> {
    #[inline]
    fn drop(&mut self) {
        if !self.raw.is_ready() {
            // SAFETY: we drop the `Arc` (decrementing the reference count)
            // only if this future was cancelled before returning an
            // upgraded lock.
            unsafe {
                ManuallyDrop::drop(&mut self.raw);
                ManuallyDrop::drop(&mut self.lock);
            };
        }
    }
}