keepcalm 0.6.0

Simple shared types for multi-threaded programs
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
624
625
626
use std::fmt::Debug;
use std::marker::PhantomData;

use crate::{
    implementation::{LockMetadata, SharedImpl},
    synchronizer::{SynchronizerMetadata, SynchronizerReadLock, SynchronizerWriteLock},
};

/// A zero-sized marker that removes `Send` from a lock guard.
///
/// The guards are deliberately `!Send`: a blocking lock held across an `.await` makes the
/// surrounding future `!Send`, so it cannot be spawned on a work-stealing executor (e.g.
/// `tokio::spawn`). This is a compile-time guard rail against the classic footgun of holding a lock
/// across a suspend point, which blocks the executor or deadlocks. (`std::sync::MutexGuard` is
/// `!Send`, so `PhantomData` of it removes `Send`; the guards are already `!Sync` via their
/// projection variant, so `Sync` is unaffected either way.)
type Unsend = PhantomData<std::sync::MutexGuard<'static, ()>>;

/// UNSAFETY: We can implement this for all types, as T must always be Send unless it is a projection, in which case the
/// projection functions must be Send. Note that the outer [`SharedReadLock`]/[`SharedWriteLock`]
/// are still `!Send` thanks to their [`Unsend`] marker; this inner `Send` is what lets the *owned*
/// guards (the async `spawn_blocking` vehicle) re-assert `Send` soundly.
unsafe impl<'a, T: ?Sized> Send for SharedReadLockInner<'a, T> {}
unsafe impl<'a, T: ?Sized> Send for SharedWriteLockInner<'a, T> {}

pub enum SharedReadLockInner<'a, T: ?Sized> {
    /// Delegate to Synchronizer.
    Sync(SynchronizerReadLock<'a, LockMetadata, T>),
    SyncBox(SynchronizerReadLock<'a, LockMetadata, Box<T>>),
    /// A projected lock.
    Projection(Box<dyn std::ops::Deref<Target = T> + 'a>),
}

/// This holds a read lock on the underlying container's object.
///
/// The particular behaviour of the lock depends on the underlying synchronization primitive.
#[must_use = "if unused the lock will immediately unlock"]
// Waiting for stable: https://github.com/rust-lang/rust/issues/83310
// #[must_not_suspend = "holding a lock across suspend \
//                       points can cause deadlocks, delays, \
//                       and cause Futures to not implement `Send`"]
pub struct SharedReadLock<'a, T: ?Sized> {
    pub(crate) inner: SharedReadLockInner<'a, T>,
    /// Held-lock registration for deadlock detection; `None` for untracked
    /// (Arc/RCU) synchronizers and for projections (the projected root guard
    /// carries the registration).
    #[cfg(feature = "deadlock_detection")]
    pub(crate) lockdep: Option<crate::lockdep::HeldToken>,
    /// Makes the guard `!Send` so it cannot be held across an `.await`. See [`Unsend`].
    pub(crate) _unsend: Unsend,
}

impl<'a, T: ?Sized> SharedReadLock<'a, T> {
    pub(crate) fn from_projection(inner: Box<dyn std::ops::Deref<Target = T> + 'a>) -> Self {
        SharedReadLock {
            inner: SharedReadLockInner::Projection(inner),
            #[cfg(feature = "deadlock_detection")]
            lockdep: None,
            _unsend: PhantomData,
        }
    }

    /// Map this read guard to a component of the locked data.
    ///
    /// The underlying lock stays held until the returned guard is dropped. The
    /// projection function may be called more than once.
    pub fn map<P: ?Sized, F>(self, project: F) -> SharedReadLock<'a, P>
    where
        F: for<'x> Fn(&'x T) -> &'x P + Send + 'a,
    {
        struct Mapped<'a, T: ?Sized, P: ?Sized, F> {
            lock: SharedReadLock<'a, T>,
            project: F,
            _marker: std::marker::PhantomData<&'a P>,
        }

        impl<'a, T: ?Sized, P: ?Sized, F> std::ops::Deref for Mapped<'a, T, P, F>
        where
            F: for<'x> Fn(&'x T) -> &'x P,
        {
            type Target = P;
            fn deref(&self) -> &P {
                (self.project)(&*self.lock)
            }
        }

        SharedReadLock::from_projection(Box::new(Mapped {
            lock: self,
            project,
            _marker: std::marker::PhantomData,
        }))
    }
}

impl<'a, T: ?Sized> Drop for SharedReadLock<'a, T> {
    fn drop(&mut self) {
        #[cfg(feature = "deadlock_detection")]
        if let Some(token) = self.lockdep.take() {
            crate::lockdep::release_held(token);
        }
        if let SharedReadLockInner::Sync(lock) = &self.inner {
            let metadata = lock.metadata();
            if let Some(poison) = &metadata.poison {
                if crate::implementation::panicking_should_poison() {
                    poison.store(true, std::sync::atomic::Ordering::Release);
                }
            }
        }
        if let SharedReadLockInner::SyncBox(lock) = &self.inner {
            let metadata = lock.metadata();
            if let Some(poison) = &metadata.poison {
                if crate::implementation::panicking_should_poison() {
                    poison.store(true, std::sync::atomic::Ordering::Release);
                }
            }
        }
    }
}

impl<'a, T: ?Sized> From<SynchronizerReadLock<'a, LockMetadata, T>> for SharedReadLock<'a, T> {
    fn from(value: SynchronizerReadLock<'a, LockMetadata, T>) -> Self {
        #[cfg(feature = "deadlock_detection")]
        let lockdep = crate::lockdep::read_guard_token(&value);
        SharedReadLock {
            inner: SharedReadLockInner::Sync(value),
            #[cfg(feature = "deadlock_detection")]
            lockdep,
            _unsend: PhantomData,
        }
    }
}

impl<'a, T: ?Sized> From<SynchronizerReadLock<'a, LockMetadata, Box<T>>> for SharedReadLock<'a, T> {
    fn from(value: SynchronizerReadLock<'a, LockMetadata, Box<T>>) -> Self {
        #[cfg(feature = "deadlock_detection")]
        let lockdep = crate::lockdep::read_guard_token(&value);
        SharedReadLock {
            inner: SharedReadLockInner::SyncBox(value),
            #[cfg(feature = "deadlock_detection")]
            lockdep,
            _unsend: PhantomData,
        }
    }
}

pub struct SharedReadLockOwned<T: ?Sized + 'static> {
    // Note the ordering of the fields - we want to drop the inner lock before the container!
    pub(crate) inner: SharedReadLock<'static, T>,
    // Container is never used, but we keep it around for safety/reference purposes
    #[allow(unused)]
    pub(crate) container: SharedImpl<T>,
}

// UNSAFETY: The borrowed `SharedReadLock` is `!Send` only because of its `Unsend` marker; its real
// contents are `Send` (see the `SharedReadLockInner` impl above). The owned guard is the internal
// vehicle for the async `spawn_blocking` path, which must move it across threads, so we re-assert
// `Send` here. This is *not* exposed to users -- they never receive an owned guard directly.
unsafe impl<T: ?Sized + 'static> Send for SharedReadLockOwned<T> {}
unsafe impl<T: ?Sized + 'static> Send for SharedWriteLockOwned<T> {}

impl<T: ?Sized + 'static> SharedReadLockOwned<T> {
    /// Unsafely reattach this to a lifetime. You must call this with a lock that is the same
    /// as the original lock!
    #[cfg(feature = "async_experimental")]
    #[allow(clippy::needless_lifetimes)]
    pub(crate) unsafe fn unsafe_reattach<'a>(
        self,
        _to: &'a SharedImpl<T>,
    ) -> SharedReadLock<'a, T> {
        std::mem::transmute(self.inner)
    }
}

pub struct SharedWriteLockOwned<T: ?Sized + 'static> {
    // Note the ordering of the fields - we want to drop the inner lock before the container!
    pub(crate) inner: SharedWriteLock<'static, T>,
    // Container is never used, but we keep it around for safety/reference purposes
    #[allow(unused)]
    pub(crate) container: SharedImpl<T>,
}

impl<T: ?Sized + 'static> SharedWriteLockOwned<T> {
    /// Unsafely reattach this to a lifetime. You must call this with a lock that is the same
    /// as the original lock!
    #[cfg(feature = "async_experimental")]
    #[allow(clippy::needless_lifetimes)]
    pub(crate) unsafe fn unsafe_reattach<'a>(
        self,
        _to: &'a SharedImpl<T>,
    ) -> SharedWriteLock<'a, T> {
        std::mem::transmute(self.inner)
    }
}

impl<T: ?Sized + 'static> std::ops::Deref for SharedReadLockOwned<T> {
    type Target = <SharedReadLock<'static, T> as std::ops::Deref>::Target;
    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl<T: ?Sized + 'static> std::ops::Deref for SharedWriteLockOwned<T> {
    type Target = <SharedWriteLock<'static, T> as std::ops::Deref>::Target;
    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl<T: ?Sized + 'static> std::ops::DerefMut for SharedWriteLockOwned<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.deref_mut()
    }
}

pub enum SharedWriteLockInner<'a, T: ?Sized> {
    /// Delegate to Synchronizer.
    Sync(SynchronizerWriteLock<'a, LockMetadata, T>),
    SyncBox(SynchronizerWriteLock<'a, LockMetadata, Box<T>>),
    Projection(Box<dyn std::ops::DerefMut<Target = T> + 'a>),
}

/// This holds a write lock on the underlying container's object.
///
/// The particular behaviour of the lock depends on the underlying synchronization primitive.
#[must_use = "if unused the lock will immediately unlock"]
// Waiting for stable: https://github.com/rust-lang/rust/issues/83310
// #[must_not_suspend = "holding a lock across suspend \
//                       points can cause deadlocks, delays, \
//                       and cause Futures to not implement `Send`"]
pub struct SharedWriteLock<'a, T: ?Sized> {
    pub(crate) inner: SharedWriteLockInner<'a, T>,
    /// Held-lock registration for deadlock detection; `None` for untracked
    /// (RCU) synchronizers and for projections (the projected root guard
    /// carries the registration).
    #[cfg(feature = "deadlock_detection")]
    pub(crate) lockdep: Option<crate::lockdep::HeldToken>,
    /// Makes the guard `!Send` so it cannot be held across an `.await`. See [`Unsend`].
    pub(crate) _unsend: Unsend,
}

impl<'a, T: ?Sized> SharedWriteLock<'a, T> {
    pub(crate) fn from_projection(inner: Box<dyn std::ops::DerefMut<Target = T> + 'a>) -> Self {
        SharedWriteLock {
            inner: SharedWriteLockInner::Projection(inner),
            #[cfg(feature = "deadlock_detection")]
            lockdep: None,
            _unsend: PhantomData,
        }
    }

    /// Map this write guard to a component of the locked data.
    ///
    /// The underlying lock stays held until the returned guard is dropped. The
    /// projection functions may be called more than once.
    pub fn map<P: ?Sized, FR, FW>(self, project: FR, project_mut: FW) -> SharedWriteLock<'a, P>
    where
        FR: for<'x> Fn(&'x T) -> &'x P + Send + 'a,
        FW: for<'x> Fn(&'x mut T) -> &'x mut P + Send + 'a,
    {
        struct Mapped<'a, T: ?Sized, P: ?Sized, FR, FW> {
            lock: SharedWriteLock<'a, T>,
            project: FR,
            project_mut: FW,
            _marker: std::marker::PhantomData<&'a P>,
        }

        impl<'a, T: ?Sized, P: ?Sized, FR, FW> std::ops::Deref for Mapped<'a, T, P, FR, FW>
        where
            FR: for<'x> Fn(&'x T) -> &'x P,
        {
            type Target = P;
            fn deref(&self) -> &P {
                (self.project)(&*self.lock)
            }
        }

        impl<'a, T: ?Sized, P: ?Sized, FR, FW> std::ops::DerefMut for Mapped<'a, T, P, FR, FW>
        where
            FR: for<'x> Fn(&'x T) -> &'x P,
            FW: for<'x> Fn(&'x mut T) -> &'x mut P,
        {
            fn deref_mut(&mut self) -> &mut P {
                (self.project_mut)(&mut *self.lock)
            }
        }

        SharedWriteLock::from_projection(Box::new(Mapped {
            lock: self,
            project,
            project_mut,
            _marker: std::marker::PhantomData,
        }))
    }
}

impl<'a, T: ?Sized> From<SynchronizerWriteLock<'a, LockMetadata, T>> for SharedWriteLock<'a, T> {
    fn from(value: SynchronizerWriteLock<'a, LockMetadata, T>) -> Self {
        #[cfg(feature = "deadlock_detection")]
        let lockdep = crate::lockdep::write_guard_token(&value);
        SharedWriteLock {
            inner: SharedWriteLockInner::Sync(value),
            #[cfg(feature = "deadlock_detection")]
            lockdep,
            _unsend: PhantomData,
        }
    }
}

impl<'a, T: ?Sized> From<SynchronizerWriteLock<'a, LockMetadata, Box<T>>>
    for SharedWriteLock<'a, T>
{
    fn from(value: SynchronizerWriteLock<'a, LockMetadata, Box<T>>) -> Self {
        #[cfg(feature = "deadlock_detection")]
        let lockdep = crate::lockdep::write_guard_token(&value);
        SharedWriteLock {
            inner: SharedWriteLockInner::SyncBox(value),
            #[cfg(feature = "deadlock_detection")]
            lockdep,
            _unsend: PhantomData,
        }
    }
}

impl<'a, T: ?Sized> std::ops::Deref for SharedReadLock<'a, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        use SharedReadLockInner::*;
        match &self.inner {
            Sync(x) => x,
            SyncBox(x) => x,
            Projection(x) => x,
        }
    }
}

impl<'a, T: ?Sized> std::ops::Deref for SharedWriteLock<'a, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        use SharedWriteLockInner::*;
        match &self.inner {
            Sync(x) => x,
            SyncBox(x) => x,
            Projection(x) => x,
        }
    }
}

impl<'a, T: ?Sized> std::ops::DerefMut for SharedWriteLock<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        use SharedWriteLockInner::*;
        match &mut self.inner {
            Sync(x) => &mut *x,
            SyncBox(x) => &mut *x,
            Projection(x) => &mut *x,
        }
    }
}

impl<'a, T: ?Sized> Drop for SharedWriteLock<'a, T> {
    fn drop(&mut self) {
        #[cfg(feature = "deadlock_detection")]
        if let Some(token) = self.lockdep.take() {
            crate::lockdep::release_held(token);
        }
        if let SharedWriteLockInner::Sync(lock) = &self.inner {
            let metadata = lock.metadata();
            if let Some(poison) = &metadata.poison {
                if crate::implementation::panicking_should_poison() {
                    poison.store(true, std::sync::atomic::Ordering::Release);
                }
            }
        }
        if let SharedWriteLockInner::SyncBox(lock) = &self.inner {
            let metadata = lock.metadata();
            if let Some(poison) = &metadata.poison {
                if crate::implementation::panicking_should_poison() {
                    poison.store(true, std::sync::atomic::Ordering::Release);
                }
            }
        }
    }
}

/// A `Send` lock guard, produced by the opt-in `*_send` accessors ([`crate::SharedMut::read_send`],
/// [`crate::SharedMut::write_send`], [`crate::Shared::read_send`]).
///
/// The ordinary guards returned by `read`/`write` are deliberately `!Send`, so that a lock held
/// across an `.await` makes the surrounding future `!Send` and therefore unspawnable on a
/// work-stealing executor — a compile-time guard rail against blocking or deadlocking the executor.
/// A `SendGuard` opts *out* of that protection.
///
/// **Only reach for this if you understand the consequences.** A `Send` guard can be moved to
/// another thread and, in `async` code, held across a suspend point. Holding a *blocking* lock
/// across `.await` can stall the executor thread or deadlock it; moving a live guard between threads
/// changes which thread ultimately releases the lock. You are responsible for ensuring neither is a
/// problem. When in doubt, use `read`/`write` instead and keep the guard on one thread, released
/// before the next `.await`.
#[must_use = "if unused the lock will immediately unlock"]
pub struct SendGuard<G> {
    inner: G,
}

// UNSAFETY: The wrapped guard is `!Send` only because of its `Unsend` marker; its actual contents
// are `Send` (the underlying parking_lot guard uses the `send_guard` feature and any projection
// closures are `Send + Sync` by construction). `SendGuard::new` is `pub(crate)` and only ever wraps
// our own guards, so re-asserting `Send` here is sound. The user opts in explicitly via `*_send`.
unsafe impl<G> Send for SendGuard<G> {}

impl<G> SendGuard<G> {
    pub(crate) fn new(inner: G) -> Self {
        SendGuard { inner }
    }

    /// Unwrap back into the underlying (`!Send`) guard.
    pub fn into_inner(self) -> G {
        self.inner
    }
}

impl<G: std::ops::Deref> std::ops::Deref for SendGuard<G> {
    type Target = G::Target;
    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl<G: std::ops::DerefMut> std::ops::DerefMut for SendGuard<G> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.deref_mut()
    }
}

impl<G: Debug> Debug for SendGuard<G> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner.fmt(f)
    }
}

impl<G: std::fmt::Display> std::fmt::Display for SendGuard<G> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner.fmt(f)
    }
}

/// Defines some common delegated operations on the underlying values, allowing the consumer to avoid having to dereference the
/// lock directly. This could likely be made generic rather than using macros.
macro_rules! implement_lock_delegates {
    ($for:ty) => {
        /// Implement Debug where T: Debug.
        impl<'a, T: ?Sized> std::fmt::Debug for $for
        where
            T: Debug,
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                (**self).fmt(f)
            }
        }

        /// Implement Display where T: Display.
        impl<'a, T: ?Sized> std::fmt::Display for $for
        where
            T: std::fmt::Display,
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                (**self).fmt(f)
            }
        }

        /// Implement Error where T: Error
        impl<'a, T: ?Sized> std::error::Error for $for
        where
            T: std::error::Error,
        {
            #[allow(deprecated)]
            fn cause(&self) -> Option<&dyn std::error::Error> {
                (**self).cause()
            }

            #[allow(deprecated)]
            fn description(&self) -> &str {
                (**self).description()
            }

            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                (**self).source()
            }
        }

        /// Implement Borrow
        impl<'a, T: ?Sized> std::borrow::Borrow<T> for $for {
            fn borrow(&self) -> &T {
                &**self
            }
        }

        /// Implement AsRef where T: AsRef
        impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for $for
        where
            T: AsRef<U>,
        {
            fn as_ref(&self) -> &U {
                (**self).as_ref()
            }
        }

        /// Implement AsMut where T: AsMut
        impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for $for
        where
            T: AsMut<U>,
            Self: std::ops::DerefMut<Target = T>,
        {
            fn as_mut(&mut self) -> &mut U {
                (**self).as_mut()
            }
        }

        /// Implement PartialEq, but only for raw types.
        impl<'a, T: ?Sized, Rhs: ?Sized> PartialEq<Rhs> for $for
        where
            T: PartialEq<Rhs>,
        {
            fn eq(&self, other: &Rhs) -> bool {
                (**self).eq(other)
            }
        }

        /// Implement PartialOrd, but only for raw types.
        impl<'a, T: ?Sized, Rhs: ?Sized> PartialOrd<Rhs> for $for
        where
            T: PartialOrd<Rhs>,
        {
            fn partial_cmp(&self, other: &Rhs) -> Option<std::cmp::Ordering> {
                (**self).partial_cmp(other)
            }
        }

        impl<'a, T: ?Sized> Unpin for $for {}
    };
}

implement_lock_delegates!(SharedReadLock<'a, T>);
implement_lock_delegates!(SharedReadLockOwned<T>);
implement_lock_delegates!(SharedWriteLock<'a, T>);
implement_lock_delegates!(SharedWriteLockOwned<T>);

/// The read/write guards are deliberately `!Send` so they cannot be held across an `.await`. These
/// assertions must fail to compile:
///
/// ```compile_fail
/// fn ensure_send<T: Send + ?Sized>() {}
/// use keepcalm::SharedReadLock;
/// ensure_send::<SharedReadLock<'static, i32>>();
/// ```
///
/// ```compile_fail
/// fn ensure_send<T: Send + ?Sized>() {}
/// use keepcalm::SharedWriteLock;
/// ensure_send::<SharedWriteLock<'static, i32>>();
/// ```
///
/// A future that holds a guard across an `.await` is therefore `!Send` and cannot be spawned:
///
/// ```compile_fail
/// use keepcalm::SharedMut;
/// fn ensure_send<T: Send>(t: T) -> T { t }
/// let shared = SharedMut::new(1);
/// let fut = async move {
///     let guard = shared.write();
///     std::future::ready(()).await; // holding the guard across a suspend point
///     *guard += 1;
/// };
/// ensure_send(fut); // fails: `fut` is `!Send` because it holds a `!Send` guard across `.await`
/// ```
///
/// The opt-in [`keepcalm::SendGuard`] (via `read_send`/`write_send`) *is* `Send`:
///
/// ```rust
/// use keepcalm::{SendGuard, SharedReadLock, SharedMut};
/// fn ensure_send<T: Send>() {}
/// ensure_send::<SendGuard<SharedReadLock<'static, i32>>>();
/// let shared = SharedMut::new(1);
/// let g = shared.read_send();
/// assert_eq!(*g, 1);
/// ```
#[cfg(doctest)]
mod send_test {}

#[cfg(test)]
mod test {
    use super::*;

    /// The borrowed guards are `!Send` (asserted via the `send_test` compile_fail doctests). The
    /// opt-in `SendGuard` wrapper re-enables `Send`.
    #[allow(unused)]
    fn ensure_guard_auto_traits() {
        fn ensure_send<T: Send>() {}
        ensure_send::<SendGuard<SharedReadLock<'static, ()>>>();
        ensure_send::<SendGuard<SharedWriteLock<'static, ()>>>();
    }

    #[test]
    fn test_guard_map() {
        let shared = crate::SharedMut::new((1u32, "x".to_string()));

        let read = shared.read().map(|t| &t.0);
        assert_eq!(*read, 1);
        // The root lock is still held by the mapped guard.
        assert!(shared.try_write().is_none());
        drop(read);

        let mut write = shared.write().map(|t| &t.1, |t| &mut t.1);
        *write += "y";
        drop(write);
        assert_eq!(shared.read().1, "xy");
    }

    #[allow(unused)]
    fn ensure_locks_coerce_deref(read: SharedReadLock<String>) {
        fn takes_as_ref(_: &String) {}
        fn takes_as_ref_str(_: &impl AsRef<str>) {}

        takes_as_ref(&read);
        takes_as_ref_str(&read);
        assert!(read == *"123");
    }
}