nuclei 0.5.4

Proactive IO & runtime system
Documentation
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! An asynchronous reader-writer lock with an upgradable read; how it
//! admits waiters is described on its core, `raw_rwlock`.

#![allow(unsafe_code)]

use core::fmt;
use core::future::Future;
use core::ops::Deref;
use core::pin::Pin;
use core::task::{Context, Poll};
use std::sync::Arc;

use super::internal;
use super::mutex::TryLockError;
use super::raw_rwlock::{
    MAX_READERS, READ, RawRwLock, RwWait, UPGRADABLE_READ, UpgradeWait, WRITE,
};

/// The most readers [`RwLock::with_max_readers`] accepts: `u32::MAX >> 3`,
/// or the reader field of the lock where a word is too narrow for that.
const MAX_READS: usize = if (u32::MAX >> 3) as usize <= MAX_READERS {
    (u32::MAX >> 3) as usize
} else {
    MAX_READERS
};

/// A reader-writer lock whose waits are futures.
///
/// Uncontended, a read or write lock and its release are one atomic
/// read-modify-write each and allocate nothing. Under contention a caller
/// takes the lock whenever it is free to it, even past queued waiters, so
/// a release never waits for a suspended task; a waiter passed over
/// [`MAX_BYPASS`](super::MAX_BYPASS) times is handed the lock next, and
/// nobody gets in ahead of it meanwhile, so neither readers nor writers
/// starve.
///
/// [`upgradable_read`](Self::upgradable_read) takes a read that can later
/// become the write without letting a writer in between. One upgradable
/// read exists at a time; it shares the lock with plain readers, and plain
/// read guards cannot upgrade. A write guard becomes a read guard, with no
/// writer in between, through
/// [`RwLockWriteGuard::downgrade`].
///
/// ```
/// use nuclei::sync::RwLock;
///
/// let lock = RwLock::new(5);
/// let a = lock.try_read().expect("free");
/// let b = lock.try_read().expect("readers share");
/// assert!(lock.try_write().is_err());
/// drop((a, b));
/// *lock.try_write().expect("free again") += 1;
/// assert_eq!(lock.into_inner(), 6);
/// ```
pub struct RwLock<T: ?Sized> {
    raw: RawRwLock,
    value: core::cell::UnsafeCell<T>,
}

// SAFETY: the value moves with the lock.
unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
// SAFETY: readers share `&T` across threads and a writer gets `&mut T`
// alone, the same contract as `std::sync::RwLock`.
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}

impl<T> RwLock<T> {
    loom_const_fn! {
        /// An unlocked lock holding `value`.
        pub fn new(value: T) -> Self {
            Self { raw: RawRwLock::new(), value: core::cell::UnsafeCell::new(value) }
        }
    }

    loom_const_fn! {
        /// An unlocked lock holding `value`, usable in a `static`.
        pub fn const_new(value: T) -> Self {
            Self { raw: RawRwLock::new(), value: core::cell::UnsafeCell::new(value) }
        }
    }

    loom_const_fn! {
        /// An unlocked lock holding `value` that admits at most `max_reads`
        /// readers at once: a reader that finds them all inside waits for
        /// one to leave, as one held back by a writer does.
        ///
        /// # Panics
        ///
        /// When `max_reads` is `0` or is bigger than `u32::MAX >> 3`. (A
        /// target whose word is narrower than 64 bits caps it lower, at the
        /// readers its state word can count: `2^23 - 1` on 32 bits.)
        pub fn with_max_readers(value: T, max_reads: u32) -> Self {
            assert!(max_reads != 0, "a RwLock may not be created with 0 readers");
            assert!(
                max_reads as usize <= MAX_READS,
                "a RwLock may not be created with more than u32::MAX >> 3 readers"
            );
            Self {
                raw: RawRwLock::with_max_readers(max_reads as usize),
                value: core::cell::UnsafeCell::new(value),
            }
        }
    }

    loom_const_fn! {
        /// [`with_max_readers`](Self::with_max_readers), usable in a
        /// `static`.
        ///
        /// # Panics
        ///
        /// As for [`with_max_readers`](Self::with_max_readers).
        pub fn const_with_max_readers(value: T, max_reads: u32) -> Self {
            assert!(max_reads != 0, "a RwLock may not be created with 0 readers");
            assert!(
                max_reads as usize <= MAX_READS,
                "a RwLock may not be created with more than u32::MAX >> 3 readers"
            );
            Self {
                raw: RawRwLock::with_max_readers(max_reads as usize),
                value: core::cell::UnsafeCell::new(value),
            }
        }
    }

    loom_const_fn! {
        /// An unlocked lock holding `value` whose contended acquires never
        /// spin: they queue at once. For a lock its holders keep across long
        /// waits of their own (I/O, say), where a spin could only burn the
        /// caller's time.
        #[cfg(test)]
        pub(crate) fn new_without_spin(value: T) -> Self {
            Self { raw: RawRwLock::without_spin(), value: core::cell::UnsafeCell::new(value) }
        }
    }

    /// Consumes the lock and returns its value.
    pub fn into_inner(self) -> T {
        self.value.into_inner()
    }
}

#[cfg(all(test, not(loom)))]
impl<T: ?Sized> RwLock<T> {
    /// The rounds a contended acquire of this lock would spin now.
    pub(crate) fn spin_rounds(&self) -> u32 {
        self.raw.spin_rounds()
    }
}

impl<T: ?Sized> RwLock<T> {
    /// Shares the lock if no writer holds it, no upgrade is under way, the
    /// readers have room and no handoff is owed.
    ///
    /// # Errors
    ///
    /// [`TryLockError`] when any of those fails.
    pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>, TryLockError> {
        if self.raw.try_kind(READ) {
            Ok(RwLockReadGuard::new(self))
        } else {
            Err(TryLockError(()))
        }
    }

    /// [`try_read`](Self::try_read) through an `Arc`, so the guard can
    /// outlive the borrow.
    ///
    /// # Errors
    ///
    /// [`TryLockError`] when the lock cannot be shared now.
    pub fn try_read_owned(self: Arc<Self>) -> Result<OwnedRwLockReadGuard<T>, TryLockError> {
        if self.raw.try_kind(READ) {
            Ok(OwnedRwLockReadGuard::new(self))
        } else {
            Err(TryLockError(()))
        }
    }

    /// Takes the lock alone if nobody holds it and no handoff is owed.
    ///
    /// # Errors
    ///
    /// [`TryLockError`] when any reader or writer holds the lock, or a
    /// waiter is owed it.
    pub fn try_write(&self) -> Result<RwLockWriteGuard<'_, T>, TryLockError> {
        if self.raw.try_kind(WRITE) {
            Ok(RwLockWriteGuard::new(self))
        } else {
            Err(TryLockError(()))
        }
    }

    /// [`try_write`](Self::try_write) through an `Arc`, so the guard can
    /// outlive the borrow.
    ///
    /// # Errors
    ///
    /// [`TryLockError`] when the lock cannot be taken alone now.
    pub fn try_write_owned(self: Arc<Self>) -> Result<OwnedRwLockWriteGuard<T>, TryLockError> {
        if self.raw.try_kind(WRITE) {
            Ok(OwnedRwLockWriteGuard::new(self))
        } else {
            Err(TryLockError(()))
        }
    }

    /// Takes the upgradable read if no writer and no other upgradable read
    /// holds the lock and no handoff is owed.
    pub fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<'_, T>> {
        self.raw
            .try_kind(UPGRADABLE_READ)
            .then(|| RwLockUpgradableReadGuard { lock: self })
    }

    /// Waits to share the lock. Uncontended, the first poll is one atomic
    /// read-modify-write. Contended, it is lock-free, not wait-free: see
    /// [Progress](super#progress).
    ///
    /// Dropping the future withdraws the wait, and passes on a lock handed
    /// to it meanwhile.
    pub fn read(&self) -> Read<'_, T> {
        Read {
            lock: self,
            wait: RwWait::new(),
        }
    }

    /// [`read`](Self::read) through an `Arc`, so the guard can outlive the
    /// borrow.
    pub fn read_owned(self: Arc<Self>) -> ReadOwned<T> {
        ReadOwned {
            lock: self,
            wait: RwWait::new(),
        }
    }

    /// Waits to hold the lock alone. Uncontended, the first poll is one atomic
    /// read-modify-write. Contended, it is lock-free, not wait-free: see
    /// [Progress](super#progress).
    ///
    /// Dropping the future withdraws the wait, and passes on a lock handed
    /// to it meanwhile.
    pub fn write(&self) -> Write<'_, T> {
        Write {
            lock: self,
            wait: RwWait::new(),
        }
    }

    /// Waits to hold the lock alone, with a guard that keeps the lock alive
    /// itself, so the future and the guard can outlive the borrow that made
    /// them: a wait kept across calls, or a guard returned to a caller.
    pub fn write_owned(self: Arc<Self>) -> WriteOwned<T> {
        WriteOwned {
            lock: self,
            wait: RwWait::new(),
        }
    }

    /// [`read`](Self::read) for a thread that may block: it parks the
    /// calling thread until the lock can be shared. On a nuclei shard (inside one of its tasks) it does not park the thread: it runs a nested `block_on`, so the shard keeps running its other tasks meanwhile.
    /// Inside another runtime's task it blocks that task's executor thread,
    /// so do not: the task that would release may be the one that thread was
    /// running.
    ///
    /// A wait whose frame runs a task that opens another blocking frame gives
    /// up its place in line and asks again, at the back, once its frame is on
    /// top again, so nested blocking waits never wait on each other with the
    /// resource free.
    ///
    /// # Panics
    ///
    /// On `wasm32`, where no other thread can release, when the lock cannot
    /// be shared at once: the call could never end.
    pub fn blocking_read(&self) -> RwLockReadGuard<'_, T> {
        internal::blocking_in_line(self.read())
    }

    /// [`write`](Self::write) for a thread that may block: see
    /// [`blocking_read`](Self::blocking_read).
    ///
    /// # Panics
    ///
    /// On `wasm32`, where no other thread can release, when the lock is held
    /// at all: the call could never end.
    pub fn blocking_write(&self) -> RwLockWriteGuard<'_, T> {
        internal::blocking_in_line(self.write())
    }

    /// Waits for the upgradable read. It makes the progress
    /// [`read`](Self::read) does.
    pub fn upgradable_read(&self) -> UpgradableRead<'_, T> {
        UpgradableRead {
            lock: self,
            wait: RwWait::new(),
        }
    }

    /// The value, borrowed mutably; `&mut self` proves no guard exists.
    pub fn get_mut(&mut self) -> &mut T {
        self.value.get_mut()
    }
}

impl<T> From<T> for RwLock<T> {
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

impl<T: Default> Default for RwLock<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut d = f.debug_struct("RwLock");
        match self.try_read() {
            Ok(inner) => d.field("data", &&*inner),
            Err(_) => d.field("data", &format_args!("<locked>")),
        };
        d.finish()
    }
}

/// The upgradable read of a [`RwLock`]: shared access that can become
/// exclusive without a writer getting in between. Releases on drop, with
/// the progress [`RwLockReadGuard`]'s release makes.
///
/// It moves to another thread only when `T` is `Send` as well as `Sync`:
/// an upgrade turns it into `&mut T`, so moving it can move the value. A
/// value that may be shared but not moved, such as a
/// [`std::sync::MutexGuard`], keeps the guard on its thread:
///
/// ```compile_fail,E0277
/// use nuclei::sync::RwLockUpgradableReadGuard;
/// use std::sync::MutexGuard;
///
/// fn moves<T: Send>() {}
/// moves::<RwLockUpgradableReadGuard<'static, Option<MutexGuard<'static, ()>>>>();
/// ```
#[must_use = "dropping the guard releases the lock at once"]
pub struct RwLockUpgradableReadGuard<'a, T: ?Sized> {
    lock: &'a RwLock<T>,
}

// SAFETY: moving it moves `&T` access and, through `upgrade` and
// `try_upgrade`, `&mut T` access, as moving the `Upgrade` future does.
unsafe impl<T: ?Sized + Send + Sync> Send for RwLockUpgradableReadGuard<'_, T> {}
// SAFETY: `&RwLockUpgradableReadGuard` reaches `&T` only: both upgrades
// take the guard by value.
unsafe impl<T: ?Sized + Sync> Sync for RwLockUpgradableReadGuard<'_, T> {}

impl<'a, T: ?Sized> RwLockUpgradableReadGuard<'a, T> {
    /// Waits for the readers inside to leave, holding new readers back
    /// meanwhile, then holds the write.
    pub fn upgrade(self) -> Upgrade<'a, T> {
        let lock = self.lock;
        core::mem::forget(self);
        Upgrade {
            lock,
            wait: UpgradeWait::new(),
            done: false,
        }
    }

    /// Holds the write at once if no reader is inside; otherwise hands the
    /// upgradable read back.
    pub fn try_upgrade(self) -> Result<RwLockWriteGuard<'a, T>, Self> {
        if !self.lock.raw.try_upgrade() {
            return Err(self);
        }
        let lock = self.lock;
        core::mem::forget(self);
        Ok(RwLockWriteGuard::new(lock))
    }
}

impl<T: ?Sized> Deref for RwLockUpgradableReadGuard<'_, T> {
    type Target = T;

    fn deref(&self) -> &T {
        // SAFETY: the upgradable read excludes every writer.
        unsafe { &*self.lock.value.get() }
    }
}

impl<T: ?Sized> Drop for RwLockUpgradableReadGuard<'_, T> {
    fn drop(&mut self) {
        self.lock.raw.upgradable_unlock();
    }
}

impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockUpgradableReadGuard<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&**self, f)
    }
}

/// The future [`RwLock::read`] returns.
#[must_use = "futures do nothing unless polled"]
pub struct Read<'a, T: ?Sized> {
    lock: &'a RwLock<T>,
    wait: RwWait,
}

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

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<RwLockReadGuard<'a, T>> {
        let this = self.get_mut();
        this.wait
            .poll(&this.lock.raw, READ, cx.waker())
            .map(|()| RwLockReadGuard::new(this.lock))
    }
}

impl<T: ?Sized> Drop for Read<'_, T> {
    fn drop(&mut self) {
        self.wait.cancel(&self.lock.raw, READ);
    }
}

impl<T: ?Sized> internal::Withdraw for Read<'_, T> {
    fn withdraw(self: Pin<&mut Self>) {
        let this = self.get_mut();
        this.wait.cancel(&this.lock.raw, READ);
    }
}

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

/// The future [`RwLock::read_owned`] returns. It keeps its place in the
/// queue, and the bypasses it counted, for as long as it lives; dropping it
/// withdraws the wait and passes on a lock handed to it meanwhile.
#[must_use = "futures do nothing unless polled"]
pub struct ReadOwned<T: ?Sized> {
    lock: Arc<RwLock<T>>,
    wait: RwWait,
}

impl<T: ?Sized> Future for ReadOwned<T> {
    type Output = OwnedRwLockReadGuard<T>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<OwnedRwLockReadGuard<T>> {
        let this = self.get_mut();
        this.wait
            .poll(&this.lock.raw, READ, cx.waker())
            .map(|()| OwnedRwLockReadGuard::new(Arc::clone(&this.lock)))
    }
}

impl<T: ?Sized> Drop for ReadOwned<T> {
    fn drop(&mut self) {
        self.wait.cancel(&self.lock.raw, READ);
    }
}

impl<T: ?Sized> fmt::Debug for ReadOwned<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ReadOwned").finish_non_exhaustive()
    }
}

/// The future [`RwLock::write`] returns.
#[must_use = "futures do nothing unless polled"]
pub struct Write<'a, T: ?Sized> {
    lock: &'a RwLock<T>,
    wait: RwWait,
}

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

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<RwLockWriteGuard<'a, T>> {
        let this = self.get_mut();
        this.wait
            .poll(&this.lock.raw, WRITE, cx.waker())
            .map(|()| RwLockWriteGuard::new(this.lock))
    }
}

impl<T: ?Sized> Drop for Write<'_, T> {
    fn drop(&mut self) {
        self.wait.cancel(&self.lock.raw, WRITE);
    }
}

impl<T: ?Sized> internal::Withdraw for Write<'_, T> {
    fn withdraw(self: Pin<&mut Self>) {
        let this = self.get_mut();
        this.wait.cancel(&this.lock.raw, WRITE);
    }
}

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

/// The future [`RwLock::write_owned`] returns. It keeps its place in the
/// queue, and the bypasses it counted, for as long as it lives; dropping it
/// withdraws the wait and passes on a handoff made to it meanwhile.
#[must_use = "futures do nothing unless polled"]
pub struct WriteOwned<T: ?Sized> {
    lock: Arc<RwLock<T>>,
    wait: RwWait,
}

impl<T: ?Sized> Future for WriteOwned<T> {
    type Output = OwnedRwLockWriteGuard<T>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<OwnedRwLockWriteGuard<T>> {
        let this = self.get_mut();
        this.wait
            .poll(&this.lock.raw, WRITE, cx.waker())
            .map(|()| OwnedRwLockWriteGuard::new(Arc::clone(&this.lock)))
    }
}

impl<T: ?Sized> Drop for WriteOwned<T> {
    fn drop(&mut self) {
        self.wait.cancel(&self.lock.raw, WRITE);
    }
}

impl<T: ?Sized> fmt::Debug for WriteOwned<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WriteOwned").finish_non_exhaustive()
    }
}

/// The future [`RwLock::upgradable_read`] returns.
#[must_use = "futures do nothing unless polled"]
pub struct UpgradableRead<'a, T: ?Sized> {
    lock: &'a RwLock<T>,
    wait: RwWait,
}

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

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<RwLockUpgradableReadGuard<'a, T>> {
        let this = self.get_mut();
        this.wait
            .poll(&this.lock.raw, UPGRADABLE_READ, cx.waker())
            .map(|()| RwLockUpgradableReadGuard { lock: this.lock })
    }
}

impl<T: ?Sized> Drop for UpgradableRead<'_, T> {
    fn drop(&mut self) {
        self.wait.cancel(&self.lock.raw, UPGRADABLE_READ);
    }
}

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

/// The future [`RwLockUpgradableReadGuard::upgrade`] returns. Dropping it
/// before it completes releases the upgradable read.
#[must_use = "futures do nothing unless polled"]
pub struct Upgrade<'a, T: ?Sized> {
    lock: &'a RwLock<T>,
    wait: UpgradeWait,
    done: bool,
}

// SAFETY: it holds the upgradable read and yields the write: moving it
// moves `&mut T` access, sharing it shares nothing.
unsafe impl<T: ?Sized + Send + Sync> Send for Upgrade<'_, T> {}
// SAFETY: `&Upgrade` exposes nothing.
unsafe impl<T: ?Sized + Send + Sync> Sync for Upgrade<'_, T> {}

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

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<RwLockWriteGuard<'a, T>> {
        let this = self.get_mut();
        assert!(
            !this.done,
            "an Upgrade future was polled after it completed"
        );
        this.wait.poll(&this.lock.raw, cx.waker()).map(|()| {
            this.done = true;
            RwLockWriteGuard::new(this.lock)
        })
    }
}

impl<T: ?Sized> Drop for Upgrade<'_, T> {
    fn drop(&mut self) {
        if !self.done {
            self.wait.cancel(&self.lock.raw);
        }
    }
}

impl<T: ?Sized> fmt::Debug for Upgrade<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Upgrade")
            .field("done", &self.done)
            .finish_non_exhaustive()
    }
}

mod guards;
pub use guards::{
    OwnedRwLockMappedWriteGuard, OwnedRwLockReadGuard, OwnedRwLockWriteGuard,
    RwLockMappedWriteGuard, RwLockReadGuard, RwLockWriteGuard,
};

#[cfg(all(test, not(loom)))]
mod tests;