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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use core::fmt;
use core::mem::ManuallyDrop;
use core::pin::Pin;
use core::task::Poll;

use alloc::sync::Arc;

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

use event_listener_strategy::{easy_wrapper, EventListenerFuture, Strategy};

easy_wrapper! {
    /// The future returned by [`RwLock::read`].
    pub struct Read<'a, T: ?Sized>(ReadInner<'a, T> => RwLockReadGuard<'a, T>);
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

pin_project_lite::pin_project! {
    /// The future returned by [`RwLock::read`].
    struct ReadInner<'a, T: ?Sized> {
        // Raw read lock acquisition future, doesn't depend on `T`.
        #[pin]
        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 ReadInner<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for ReadInner<'_, T> {}

impl<'x, T: ?Sized> Read<'x, T> {
    #[inline]
    pub(super) fn new(raw: RawRead<'x>, value: *const T) -> Self {
        Self::_new(ReadInner { raw, value })
    }
}

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

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

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let mut this = self.project();
        ready!(this.raw.as_mut().poll_with_strategy(strategy, cx));

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

easy_wrapper! {
    /// The future returned by [`RwLock::read_arc`].
    pub struct ReadArc<'a, T>(ReadArcInner<'a, T> => RwLockReadGuardArc<T>);
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

pin_project_lite::pin_project! {
    /// The future returned by [`RwLock::read_arc`].
    struct ReadArcInner<'a, T> {
        // Raw read lock acquisition future, doesn't depend on `T`.
        #[pin]
        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 ReadArcInner<'_, T> {}
unsafe impl<T: Send + Sync> Sync for ReadArcInner<'_, T> {}

impl<'x, T> ReadArc<'x, T> {
    #[inline]
    pub(super) fn new(raw: RawRead<'x>, lock: &'x Arc<RwLock<T>>) -> Self {
        Self::_new(ReadArcInner { raw, lock })
    }
}

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

impl<'a, T> EventListenerFuture for ReadArcInner<'a, T> {
    type Output = RwLockReadGuardArc<T>;

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let mut this = self.project();
        ready!(this.raw.as_mut().poll_with_strategy(strategy, cx));

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

easy_wrapper! {
    /// The future returned by [`RwLock::upgradable_read`].
    pub struct UpgradableRead<'a, T: ?Sized>(
        UpgradableReadInner<'a, T> => RwLockUpgradableReadGuard<'a, T>
    );
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

pin_project_lite::pin_project! {
    /// The future returned by [`RwLock::upgradable_read`].
    struct UpgradableReadInner<'a, T: ?Sized> {
        // Raw upgradable read lock acquisition future, doesn't depend on `T`.
        #[pin]
        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 UpgradableReadInner<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for UpgradableReadInner<'_, T> {}

impl<'x, T: ?Sized> UpgradableRead<'x, T> {
    #[inline]
    pub(super) fn new(raw: RawUpgradableRead<'x>, value: *mut T) -> Self {
        Self::_new(UpgradableReadInner { raw, value })
    }
}

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

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

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let mut this = self.project();
        ready!(this.raw.as_mut().poll_with_strategy(strategy, cx));

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

easy_wrapper! {
    /// The future returned by [`RwLock::upgradable_read_arc`].
    pub struct UpgradableReadArc<'a, T: ?Sized>(
        UpgradableReadArcInner<'a, T> => RwLockUpgradableReadGuardArc<T>
    );
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

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

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

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

impl<'x, T: ?Sized> UpgradableReadArc<'x, T> {
    #[inline]
    pub(super) fn new(raw: RawUpgradableRead<'x>, lock: &'x Arc<RwLock<T>>) -> Self {
        Self::_new(UpgradableReadArcInner { raw, lock })
    }
}

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

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

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let mut this = self.project();
        ready!(this.raw.as_mut().poll_with_strategy(strategy, cx));
        Poll::Ready(RwLockUpgradableReadGuardArc {
            lock: this.lock.clone(),
        })
    }
}

easy_wrapper! {
    /// The future returned by [`RwLock::write`].
    pub struct Write<'a, T: ?Sized>(WriteInner<'a, T> => RwLockWriteGuard<'a, T>);
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

pin_project_lite::pin_project! {
    /// The future returned by [`RwLock::write`].
    struct WriteInner<'a, T: ?Sized> {
        // Raw write lock acquisition future, doesn't depend on `T`.
        #[pin]
        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 WriteInner<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for WriteInner<'_, T> {}

impl<'x, T: ?Sized> Write<'x, T> {
    #[inline]
    pub(super) fn new(raw: RawWrite<'x>, value: *mut T) -> Self {
        Self::_new(WriteInner { raw, value })
    }
}

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

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

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let mut this = self.project();
        ready!(this.raw.as_mut().poll_with_strategy(strategy, cx));

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

easy_wrapper! {
    /// The future returned by [`RwLock::write_arc`].
    pub struct WriteArc<'a, T: ?Sized>(WriteArcInner<'a, T> => RwLockWriteGuardArc<T>);
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

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

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

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

impl<'x, T: ?Sized> WriteArc<'x, T> {
    #[inline]
    pub(super) fn new(raw: RawWrite<'x>, lock: &'x Arc<RwLock<T>>) -> Self {
        Self::_new(WriteArcInner { raw, lock })
    }
}

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

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

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let mut this = self.project();
        ready!(this.raw.as_mut().poll_with_strategy(strategy, cx));

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

easy_wrapper! {
    /// The future returned by [`RwLockUpgradableReadGuard::upgrade`].
    pub struct Upgrade<'a, T: ?Sized>(UpgradeInner<'a, T> => RwLockWriteGuard<'a, T>);
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

pin_project_lite::pin_project! {
    /// The future returned by [`RwLockUpgradableReadGuard::upgrade`].
    struct UpgradeInner<'a, T: ?Sized> {
        // Raw read lock upgrade future, doesn't depend on `T`.
        #[pin]
        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 UpgradeInner<'_, T> {}
unsafe impl<T: Sync + ?Sized> Sync for UpgradeInner<'_, T> {}

impl<'x, T: ?Sized> Upgrade<'x, T> {
    #[inline]
    pub(super) fn new(raw: RawUpgrade<'x>, value: *mut T) -> Self {
        Self::_new(UpgradeInner { raw, value })
    }
}

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

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

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let mut this = self.project();
        let lock = ready!(this.raw.as_mut().poll_with_strategy(strategy, cx));

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

easy_wrapper! {
    /// The future returned by [`RwLockUpgradableReadGuardArc::upgrade`].
    pub struct UpgradeArc<T: ?Sized>(UpgradeArcInner<T> => RwLockWriteGuardArc<T>);
    #[cfg(all(feature = "std", not(target_family = "wasm")))]
    pub(crate) wait();
}

pin_project_lite::pin_project! {
    /// The future returned by [`RwLockUpgradableReadGuardArc::upgrade`].
    struct UpgradeArcInner<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.
        #[pin]
        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> PinnedDrop for UpgradeArcInner<T> {
        fn drop(this: Pin<&mut Self>) {
            let this = this.project();
            let is_ready = this.raw.is_ready();

            // SAFETY: The drop impl for raw assumes that it is pinned.
            unsafe {
                ManuallyDrop::drop(this.raw.get_unchecked_mut());
            }

            if !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(this.lock);
                };
            }
        }
    }
}

impl<T: ?Sized> UpgradeArc<T> {
    #[inline]
    pub(super) unsafe fn new(
        raw: ManuallyDrop<RawUpgrade<'static>>,
        lock: ManuallyDrop<Arc<RwLock<T>>>,
    ) -> Self {
        Self::_new(UpgradeArcInner { raw, lock })
    }
}

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> EventListenerFuture for UpgradeArcInner<T> {
    type Output = RwLockWriteGuardArc<T>;

    #[inline]
    fn poll_with_strategy<'x, S: Strategy<'x>>(
        self: Pin<&mut Self>,
        strategy: &mut S,
        cx: &mut S::Context,
    ) -> Poll<Self::Output> {
        let this = self.project();
        unsafe {
            // SAFETY: Practically, this is a pin projection.
            ready!(Pin::new_unchecked(&mut **this.raw.get_unchecked_mut())
                .poll_with_strategy(strategy, cx));
        }

        Poll::Ready(RwLockWriteGuardArc {
            lock: unsafe { ManuallyDrop::take(this.lock) },
        })
    }
}