cocoon-tpm-utils-async 0.1.3

Cocoon TPM project - Rust async related utilities
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023-2025 SUSE LLC
// Author: Nicolai Stange <nstange@suse.de>

//! Implementation of [`AsyncRwLock`]

extern crate alloc;
use super::semaphore;
use crate::sync_types;
use core::ptr;
use core::{convert, future, marker, ops, pin, task};

/// Error information returned by the [`AsyncRwLock`] API.
#[derive(Clone, Copy, Debug)]
pub enum AsyncRwLockError {
    /// A pending operation's associated [`AsyncRwLock`] has been
    /// dropped.
    StaleRwLock,

    /// Memory allocation failure.
    MemoryAllocationFailure,

    /// Internal error.
    Internal,
}

impl convert::From<semaphore::AsyncSemaphoreError> for AsyncRwLockError {
    fn from(value: semaphore::AsyncSemaphoreError) -> Self {
        match value {
            semaphore::AsyncSemaphoreError::RequestExceedsSemaphoreCapacity => AsyncRwLockError::Internal,
            semaphore::AsyncSemaphoreError::StaleSemaphore => AsyncRwLockError::StaleRwLock,
            semaphore::AsyncSemaphoreError::MemoryAllocationFailure => AsyncRwLockError::MemoryAllocationFailure,
            semaphore::AsyncSemaphoreError::Internal => AsyncRwLockError::Internal,
        }
    }
}

/// A Read-Write Lock which can be waited asynchronously for.
///
/// The locking operations [`read()`](Self::read) and [`write()`](Self::write)
/// return [`Future`]s which can subsequently get polled to eventually obtain
/// the lock.
///
/// [`AsyncRwLock`] follows the common Read-Write Lock semantics: locking for
/// writes is mutually exclusive, with either locking type whereas any number of
/// read lockings can be granted at a time.
///
/// `AsyncRwlock` is **not** robust against threads "loosing interest", e.g.
/// because the execution environment somehow abandoned them: any lock waiting
/// [`Future`] acquired from [`read()`](Self::read) or [`write()`](Self::write)
/// must either get polled to completion or dropped, otherwise it might block
/// other waiters for forever. Similar reasoning applies to the lock grant
/// guards themselves: [`AsyncRwLockReadGuard`] and [`AsyncRwLockWriteGuard`]
/// instances may always block other waiters, so it must be made sure that
/// progress is always driven forward for a lock holder until the respective
/// locking guard gets eventually dropped again.
pub struct AsyncRwLock<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync> {
    sem: semaphore::AsyncSemaphore<ST, T>,
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync> AsyncRwLock<ST, T> {
    /// Instantiate a new [`AsyncRwLock`]
    ///
    /// Note that the caller is supposed to move the returned `AsyncSemaphore`
    /// into a [`SyncRcPtr`](sync_types::SyncRcPtr) -- the other parts of
    /// the API expect that.
    ///
    /// # Arguments:
    ///
    /// * `data` - the data to wrap in the the lock.
    pub fn new(data: T) -> Self {
        Self {
            sem: semaphore::AsyncSemaphore::new(0, data),
        }
    }

    /// Asynchronous, non-exclusive locking for read semantics.
    ///
    /// Instantiate a [`AsyncRwLockReadFuture`] for taking the lock
    /// asynchronously for read semantics.
    ///
    /// The returned future will not become ready as long as an exlusive write
    /// locking, i.e. an [`AsyncRwLockWriteGuard`] is active, or some waiter
    /// for an exclusive write locking is ahead in line.
    ///
    /// Note that the mere existence of a [`AsyncRwLockReadFuture`]
    /// returned from this function may block other waiters -- it **must**
    /// always get either polled to completion or dropped again.
    ///
    /// # Errors:
    ///
    /// * [`AsyncRwLockError::MemoryAllocationFailure`] - Memory allocation
    ///   failure.
    pub fn read<'a, LP: 'a + sync_types::SyncRcPtr<Self>, LR: 'a + sync_types::SyncRcPtrRef<'a, Self, LP>>(
        this: &LR,
    ) -> Result<AsyncRwLockReadFuture<ST, T, LP>, AsyncRwLockError>
    where
        Self: 'a,
    {
        let this_sem = sync_types::SyncRcPtrRefForInner::<'_, _, _, _, AsyncRwLockIndexInnerSemTag>::new(this);
        let sem_trivial_lease_fut = semaphore::AsyncSemaphore::acquire_leases(&this_sem, 0)?;
        Ok(AsyncRwLockReadFuture { sem_trivial_lease_fut })
    }

    /// Asynchronous, exclusive locking for write semantics.
    ///
    /// Instantiate a [`AsyncRwLockWriteFuture`] for taking the lock
    /// asynchronously for write semantics.
    ///
    /// The returned future will not become ready as long as some locking of any
    /// type, i.e. an [`AsyncRwLockWriteGuard`] or [`AsyncRwLockReadGuard`]
    /// is active, or some other waiter is ahead in line.
    ///
    /// Note that the mere existence of a [`AsyncRwLockWriteFuture`]
    /// returned from this function may block other waiters -- it **must**
    /// always get either polled to completion or dropped again.
    ///
    /// # Errors:
    ///
    /// * [`AsyncRwLockError::MemoryAllocationFailure`] - Memory allocation
    ///   failure.
    pub fn write<'a, LP: 'a + sync_types::SyncRcPtr<Self>, LR: 'a + sync_types::SyncRcPtrRef<'a, Self, LP>>(
        this: &LR,
    ) -> Result<AsyncRwLockWriteFuture<ST, T, LP>, AsyncRwLockError>
    where
        Self: 'a,
    {
        let this_sem = sync_types::SyncRcPtrRefForInner::<'_, _, _, _, AsyncRwLockIndexInnerSemTag>::new(this);
        let sem_exclusive_all_fut = semaphore::AsyncSemaphore::acquire_exclusive_all(&this_sem)?;
        Ok(AsyncRwLockWriteFuture { sem_exclusive_all_fut })
    }

    /// Try to synchronously acquire lock non-exclusively for read semantics.
    ///
    /// The operation will only succeed and return a [`AsyncRwLockReadGuard`] if
    /// no exclusive locking is active or waited for to become available.
    /// Otherwise [`None`] will get returned.
    pub fn try_read<'a, LP: 'a + sync_types::SyncRcPtr<Self>, LR: 'a + sync_types::SyncRcPtrRef<'a, Self, LP>>(
        this: &LR,
    ) -> Option<AsyncRwLockReadGuard<ST, T, LP>>
    where
        Self: 'a,
    {
        let this_sem = sync_types::SyncRcPtrRefForInner::<'_, _, _, _, AsyncRwLockIndexInnerSemTag>::new(this);
        semaphore::AsyncSemaphore::try_acquire_leases(&this_sem, 0)
            .unwrap()
            .map(|sem_trivial_lease_guard| AsyncRwLockReadGuard {
                sem_trivial_lease_guard,
            })
    }

    /// Try to synchronously acquire lock exclusively for write semantics.
    ///
    /// The operation will only succeed and return a [`AsyncRwLockWriteGuard`]
    /// if no locking is active or waited for to become available.
    /// Otherwise [`None`] will get returned.
    pub fn try_write<'a, LP: 'a + sync_types::SyncRcPtr<Self>, LR: 'a + sync_types::SyncRcPtrRef<'a, Self, LP>>(
        this: &LR,
    ) -> Option<AsyncRwLockWriteGuard<ST, T, LP>>
    where
        Self: 'a,
    {
        let this_sem = sync_types::SyncRcPtrRefForInner::<'_, _, _, _, AsyncRwLockIndexInnerSemTag>::new(this);
        semaphore::AsyncSemaphore::try_acquire_exclusive_all(&this_sem).map(|sem_exclusive_all_guard| {
            AsyncRwLockWriteGuard {
                sem_exclusive_all_guard,
            }
        })
    }
}

/// [`DerefInnerByTag`](sync_types::DerefInnerByTag) `TAG` for dereferencing
/// [`AsyncRwLock::sem`].
///
/// Enables presenting the [`SyncRcPtr`](sync_types::SyncRcPtr) of the
/// [`AsyncRwLock`] as one for its [`AsyncRwLock::sem`] member
/// via the [`SyncRcPtrForInner`](sync_types::SyncRcPtrForInner) mechanism.
struct AsyncRwLockIndexInnerSemTag;

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync> sync_types::DerefInnerByTag<AsyncRwLockIndexInnerSemTag>
    for AsyncRwLock<ST, T>
{
    crate::impl_deref_inner_by_tag!(sem, semaphore::AsyncSemaphore<ST, T>);
}

/// Asynchronous wait for non-exclusive locking of an [`AsyncRwLock`].
///
/// To be obtained through [`AsyncRwLock::read()`].
///
/// # Note on lifetime management
///
/// An [`AsyncRwLockReadFuture`] instance will only maintain a weak reference
/// (i.e. a [`WeakSyncRcPtr`](sync_types::WeakSyncRcPtr)) to the associated
/// [`AsyncRwLock`] instance and thus, would not hinder its deallocation. In
/// case the lock gets dropped before the future had a chance to acquire it, its
/// `poll()` would return [`AsyncRwLockError::StaleRwLock`].
pub struct AsyncRwLockReadFuture<
    ST: sync_types::SyncTypes,
    T: marker::Send + marker::Sync,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>,
> {
    sem_trivial_lease_fut: semaphore::AsyncSemaphoreLeasesFuture<
        ST,
        T,
        sync_types::SyncRcPtrForInner<AsyncRwLock<ST, T>, LP, AsyncRwLockIndexInnerSemTag>,
    >,
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    AsyncRwLockReadFuture<ST, T, LP>
{
    /// Obtain the associated [`AsyncRwLock`].
    ///
    /// Return the associated [`AsyncRwLock`] wrapped in `Some` if still
    /// alive, `None` otherwise.
    pub fn get_rwlock(&self) -> Option<LP> {
        self.sem_trivial_lease_fut
            .get_semaphore()
            .map(|sem| sem.into_container())
    }
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    marker::Unpin for AsyncRwLockReadFuture<ST, T, LP>
{
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    future::Future for AsyncRwLockReadFuture<ST, T, LP>
{
    type Output = Result<AsyncRwLockReadGuard<ST, T, LP>, AsyncRwLockError>;

    /// Poll for a locking grant of the associated [`AsyncRwLock`].
    ///
    /// Upon future completion, either a [`AsyncRwLockReadGuard`] is returned
    /// or, if the associated [`AsyncRwLock`] had been dropped in the
    /// meanwhile, an error of [`AsyncRwLockError::StaleRwLock`].
    ///
    /// The future must not get polled any further once completed.
    fn poll(self: pin::Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        future::Future::poll(pin::Pin::new(&mut self.get_mut().sem_trivial_lease_fut), cx)
            .map_ok(|sem_trivial_lease_guard| AsyncRwLockReadGuard {
                sem_trivial_lease_guard,
            })
            .map_err(AsyncRwLockError::from)
    }
}

/// Non-exclusive locking grant on an [`AsyncRwLock`].
///
/// Besides the common functionality one would expect from a locking guard, it's
/// noteworthy that a `AsyncRwLockReadGuard` instance can get presented as a
/// [`SyncRcPtr`](sync_types) for the [`AsyncRwLock`]'s inner value (or any of
/// its members) via [`AsyncRwLockReadGuardForInner`] to APIs that expect such
/// one.
pub struct AsyncRwLockReadGuard<
    ST: sync_types::SyncTypes,
    T: marker::Send + marker::Sync,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>,
> {
    sem_trivial_lease_guard: semaphore::AsyncSemaphoreLeasesGuard<
        ST,
        T,
        sync_types::SyncRcPtrForInner<AsyncRwLock<ST, T>, LP, AsyncRwLockIndexInnerSemTag>,
    >,
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    AsyncRwLockReadGuard<ST, T, LP>
{
    /// Obtain the associated [`AsyncRwLock`].
    pub fn get_rwlock(&self) -> LP::SyncRcPtrRef<'_> {
        self.sem_trivial_lease_guard.get_semaphore().get_container().clone()
    }

    /// Release the lock and return the associated [`AsyncRwLock`].
    pub fn into_rwlock(self) -> LP {
        self.sem_trivial_lease_guard.into_semaphore().into_container()
    }

    /// Convert into an [`AsyncRwLockReadWeakGuard`] not hindering
    /// destruction of the associated [`AsyncRwLock`].
    pub fn into_weak(self) -> AsyncRwLockReadWeakGuard<ST, T, LP> {
        AsyncRwLockReadWeakGuard {
            sem_trivial_lease_guard: Some(self.sem_trivial_lease_guard.into_weak()),
        }
    }

    /// Convert into a raw pointer to the protected inner value.
    ///
    /// Must eventually get converted back with [`from_raw()`](Self::from_raw)
    /// or the locking grant will be leaked forever.
    fn into_raw(this: Self) -> *const T {
        let (ptr, leases_granted) = this.sem_trivial_lease_guard.into_raw();
        debug_assert_eq!(leases_granted, 0);
        ptr
    }

    /// Convert back from a raw pointer to the protected value obtained from
    /// [`from_raw()`](Self::from_raw).
    ///
    /// # Safety
    ///
    /// The raw pointer to the protected value must have been previously
    /// obtained from [`from_raw()`](Self::from_raw).
    unsafe fn from_raw(ptr: *const T) -> Self {
        let sem_trivial_lease_guard = unsafe { semaphore::AsyncSemaphoreLeasesGuard::from_raw(ptr, 0) };
        Self {
            sem_trivial_lease_guard,
        }
    }
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    ops::Deref for AsyncRwLockReadGuard<ST, T, LP>
{
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.sem_trivial_lease_guard
    }
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>> Clone
    for AsyncRwLockReadGuard<ST, T, LP>
{
    fn clone(&self) -> Self {
        let sem_trivial_lease_guard = self.sem_trivial_lease_guard.spawn_trivial_lease();
        Self {
            sem_trivial_lease_guard,
        }
    }
}

/// Weak variant of [`AsyncRwLockReadGuard`] not hindering destruction of the
/// associated [`AsyncRwLock`].
///
/// In cases were it's desired that long-living locking grants don't prevent a
/// destruction of the associated [`AsyncRwLock`], or to break cycles, a
/// `AsyncRwLockReadWeakGuard` may be used. To be obtained via
/// [`AsyncRwLockReadGuard::into_weak()`].
///
/// An `AsyncRwLockReadWeakGuard` may get converted back into a full
/// [`AsyncRwLockReadGuard`] via [`upgrade()`](Self::upgrade).
pub struct AsyncRwLockReadWeakGuard<
    ST: sync_types::SyncTypes,
    T: marker::Send + marker::Sync,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>,
> {
    #[allow(clippy::type_complexity)]
    sem_trivial_lease_guard: Option<
        semaphore::AsyncSemaphoreLeasesWeakGuard<
            ST,
            T,
            sync_types::SyncRcPtrForInner<AsyncRwLock<ST, T>, LP, AsyncRwLockIndexInnerSemTag>,
        >,
    >,
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    AsyncRwLockReadWeakGuard<ST, T, LP>
{
    /// Attempt to convert back into an [`AsyncRwLockReadGuard`].
    ///
    /// Returns the [`AsyncRwLockReadGuard`] wrapped in `Some` if the
    /// associated [`AsyncRwLock`] is still alive, `None otherwise.
    pub fn upgrade(self) -> Option<AsyncRwLockReadGuard<ST, T, LP>> {
        self.sem_trivial_lease_guard
            .and_then(|sem_trivial_lease_guard| sem_trivial_lease_guard.upgrade())
            .map(|sem_trivial_lease_guard| AsyncRwLockReadGuard {
                sem_trivial_lease_guard,
            })
    }

    /// Attempt to convert back into an [`AsyncRwLockReadGuard`] by reference.
    ///
    /// Returns the [`AsyncRwLockReadGuard`] wrapped in `Some` if the
    /// associated [`AsyncRwLock`] is still alive, `None otherwise.
    fn upgrade_by_ref(&self) -> Option<AsyncRwLockReadGuard<ST, T, LP>> {
        self.sem_trivial_lease_guard
            .as_ref()
            .and_then(|sem_trivial_lease_guard| sem_trivial_lease_guard.try_spawn_trivial_lease())
            .map(|sem_trivial_lease_guard| AsyncRwLockReadGuard {
                sem_trivial_lease_guard,
            })
    }

    /// Convert into a raw pointer to the protected inner value.
    ///
    /// Must eventually get converted back with [`from_raw()`](Self::from_raw)
    /// or the locking grant will be leaked forever.
    ///
    /// The returned raw pointer **must not** be used for anything except for
    /// passing it back to [`from_raw()`](Self::from_raw). In particular it
    /// must not get dereferenced, as the associated [`AsyncRwLock`], and
    /// hence its wrapped value, might have been dropped already.
    fn into_raw(mut this: Self) -> *const T {
        this.sem_trivial_lease_guard
            .take()
            .map(|sem_trivial_lease_guard| {
                let (ptr, leases_granted) = sem_trivial_lease_guard.into_raw();
                debug_assert_eq!(leases_granted, 0);
                ptr
            })
            .unwrap_or(ptr::null())
    }

    /// Convert back from a raw pointer to the protected value obtained from
    /// [`from_raw()`](Self::from_raw).
    ///
    /// # Safety
    ///
    /// The raw pointer to the protected value must have been previously
    /// obtained from [`from_raw()`](Self::from_raw).
    unsafe fn from_raw(ptr: *const T) -> Self {
        if !ptr.is_null() {
            let sem_trivial_lease_guard = unsafe { semaphore::AsyncSemaphoreLeasesWeakGuard::from_raw(ptr, 0) };
            Self {
                sem_trivial_lease_guard: Some(sem_trivial_lease_guard),
            }
        } else {
            Self {
                sem_trivial_lease_guard: None,
            }
        }
    }
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>> Clone
    for AsyncRwLockReadWeakGuard<ST, T, LP>
{
    fn clone(&self) -> Self {
        let sem_trivial_lease_guard = self
            .sem_trivial_lease_guard
            .as_ref()
            .and_then(|sem_trivial_lease_guard| sem_trivial_lease_guard.try_spawn_trivial_lease())
            .map(|sem_trivial_lease_guard| sem_trivial_lease_guard.into_weak());
        Self {
            sem_trivial_lease_guard,
        }
    }
}

/// Present a `AsyncRwLockReadGuard` instance as a [`SyncRcPtr`](sync_types) for
/// the [`AsyncRwLock`]'s inner value (or any of its members).
///
/// Useful for presenting APIs with a [`SyncRcPtr`](sync_types::SyncRcPtr) to
/// the [`AsyncRwLock`]'s wrapped value or any of its members if needed.
///
/// Users are supposed to implement
/// [`DerefInnerByTag`](sync_types::DerefInnerByTag) the [`AsyncRwLock`]'s
/// wrapped value's type, for a `TAG` routing either to the [`AsyncRwLock`]'s
/// wrapped value itself or to one of its members.
///
/// Note that the [`AsyncRwLockReadGuardForInner`] (as well its cloning spawns)
/// own a read lock on the associated [`AsyncRwLock`] for its whole lifetime. In
/// particular it blocks other tasks waiting to acquire a write lock.
pub struct AsyncRwLockReadGuardForInner<
    ST: sync_types::SyncTypes,
    OT,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>,
    TAG,
> where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    guard_for_outer: AsyncRwLockReadGuard<ST, OT, LP>,
    _phantom: marker::PhantomData<fn() -> TAG>,
}

impl<ST: sync_types::SyncTypes, OT, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>, TAG> Clone
    for AsyncRwLockReadGuardForInner<ST, OT, LP, TAG>
where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    fn clone(&self) -> Self {
        Self {
            guard_for_outer: self.guard_for_outer.clone(),
            _phantom: marker::PhantomData,
        }
    }
}

impl<ST: sync_types::SyncTypes, OT, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>, TAG>
    convert::From<AsyncRwLockReadGuard<ST, OT, LP>> for AsyncRwLockReadGuardForInner<ST, OT, LP, TAG>
where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    fn from(value: AsyncRwLockReadGuard<ST, OT, LP>) -> Self {
        Self {
            guard_for_outer: value,
            _phantom: marker::PhantomData,
        }
    }
}

impl<ST: sync_types::SyncTypes, OT, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>, TAG> ops::Deref
    for AsyncRwLockReadGuardForInner<ST, OT, LP, TAG>
where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    type Target = <OT as sync_types::DerefInnerByTag<TAG>>::Output;

    fn deref(&self) -> &Self::Target {
        <OT as sync_types::DerefInnerByTag<TAG>>::deref_inner(&self.guard_for_outer)
    }
}

impl<ST: sync_types::SyncTypes, OT, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>, TAG>
    sync_types::SyncRcPtr<<OT as sync_types::DerefInnerByTag<TAG>>::Output>
    for AsyncRwLockReadGuardForInner<ST, OT, LP, TAG>
where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    type WeakSyncRcPtr = AsyncRwLockReadWeakGuardForInner<ST, OT, LP, TAG>;

    type SyncRcPtrRef<'a>
        = sync_types::GenericSyncRcPtrRef<'a, <OT as sync_types::DerefInnerByTag<TAG>>::Output, Self>
    where
        Self: 'a;

    fn downgrade(&self) -> Self::WeakSyncRcPtr {
        Self::WeakSyncRcPtr::from(self.guard_for_outer.clone().into_weak())
    }

    fn into_raw(this: Self) -> *const <OT as sync_types::DerefInnerByTag<TAG>>::Output {
        let ptr_to_outer = AsyncRwLockReadGuard::into_raw(this.guard_for_outer);
        <OT as sync_types::DerefInnerByTag<TAG>>::to_inner_ptr(ptr_to_outer)
    }

    unsafe fn from_raw(ptr: *const <OT as sync_types::DerefInnerByTag<TAG>>::Output) -> Self {
        let ptr_to_outer = unsafe { <OT as sync_types::DerefInnerByTag<TAG>>::container_of(ptr) };
        let guard_for_outer = unsafe { AsyncRwLockReadGuard::from_raw(ptr_to_outer) };
        Self {
            guard_for_outer,
            _phantom: marker::PhantomData,
        }
    }
}

/// Weak variant of [`AsyncRwLockReadGuardForInner`] not hindering destruction
/// of the associated [`AsyncRwLock`].
///
/// Implements [`WeakSyncRcPtr`](sync_types::WeakSyncRcPtr) and is to be
/// obtained from an [`AsyncRwLockReadGuardForInner`] through the
/// [`SyncRcPtr`](sync_types::SyncRcPtr) API,
/// i.e. via [`SyncRcPtr::downgrade`](sync_types::SyncRcPtr::downgrade).
pub struct AsyncRwLockReadWeakGuardForInner<
    ST: sync_types::SyncTypes,
    OT,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>,
    TAG,
> where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    guard_for_outer: AsyncRwLockReadWeakGuard<ST, OT, LP>,
    _phantom: marker::PhantomData<fn() -> TAG>,
}

impl<ST: sync_types::SyncTypes, OT, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>, TAG> Clone
    for AsyncRwLockReadWeakGuardForInner<ST, OT, LP, TAG>
where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    fn clone(&self) -> Self {
        Self {
            guard_for_outer: self.guard_for_outer.clone(),
            _phantom: marker::PhantomData,
        }
    }
}

impl<ST: sync_types::SyncTypes, OT, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>, TAG>
    convert::From<AsyncRwLockReadWeakGuard<ST, OT, LP>> for AsyncRwLockReadWeakGuardForInner<ST, OT, LP, TAG>
where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    fn from(value: AsyncRwLockReadWeakGuard<ST, OT, LP>) -> Self {
        Self {
            guard_for_outer: value,
            _phantom: marker::PhantomData,
        }
    }
}

impl<ST: sync_types::SyncTypes, OT, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, OT>>, TAG>
    sync_types::WeakSyncRcPtr<
        <OT as sync_types::DerefInnerByTag<TAG>>::Output,
        AsyncRwLockReadGuardForInner<ST, OT, LP, TAG>,
    > for AsyncRwLockReadWeakGuardForInner<ST, OT, LP, TAG>
where
    OT: sync_types::DerefInnerByTag<TAG> + marker::Send + marker::Sync,
    <OT as sync_types::DerefInnerByTag<TAG>>::Output: marker::Send + marker::Sync,
{
    fn upgrade(&self) -> Option<AsyncRwLockReadGuardForInner<ST, OT, LP, TAG>> {
        self.guard_for_outer
            .upgrade_by_ref()
            .map(|guard_for_outer| AsyncRwLockReadGuardForInner {
                guard_for_outer,
                _phantom: marker::PhantomData,
            })
    }

    fn into_raw(this: Self) -> *const <OT as sync_types::DerefInnerByTag<TAG>>::Output {
        let ptr_to_outer = AsyncRwLockReadWeakGuard::into_raw(this.guard_for_outer);
        <OT as sync_types::DerefInnerByTag<TAG>>::to_inner_ptr(ptr_to_outer)
    }

    unsafe fn from_raw(ptr: *const <OT as sync_types::DerefInnerByTag<TAG>>::Output) -> Self {
        let ptr_to_outer = unsafe { <OT as sync_types::DerefInnerByTag<TAG>>::container_of(ptr) };
        let guard_for_outer = unsafe { AsyncRwLockReadWeakGuard::from_raw(ptr_to_outer) };
        Self {
            guard_for_outer,
            _phantom: marker::PhantomData,
        }
    }
}

/// Asynchronous wait for exclusive locking of an [`AsyncRwLock`].
///
/// To be obtained through [`AsyncRwLock::write()`].
///
/// # Note on lifetime management
///
/// An [`AsyncRwLockWriteFuture`] instance will only maintain a weak reference
/// (i.e. a [`WeakSyncRcPtr`](sync_types::WeakSyncRcPtr)) to the associated
/// [`AsyncRwLock`] instance and thus, would not hinder its deallocation. In
/// case the lock gets dropped before the future had a chance to acquire it, its
/// `poll()` would return [`AsyncRwLockError::StaleRwLock`].
pub struct AsyncRwLockWriteFuture<
    ST: sync_types::SyncTypes,
    T: marker::Send + marker::Sync,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>,
> {
    sem_exclusive_all_fut: semaphore::AsyncSemaphoreExclusiveAllFuture<
        ST,
        T,
        sync_types::SyncRcPtrForInner<AsyncRwLock<ST, T>, LP, AsyncRwLockIndexInnerSemTag>,
    >,
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    AsyncRwLockWriteFuture<ST, T, LP>
{
    pub fn get_rwlock(&self) -> Option<LP> {
        self.sem_exclusive_all_fut
            .get_semaphore()
            .map(|sem| sem.into_container())
    }
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    marker::Unpin for AsyncRwLockWriteFuture<ST, T, LP>
{
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    future::Future for AsyncRwLockWriteFuture<ST, T, LP>
{
    type Output = Result<AsyncRwLockWriteGuard<ST, T, LP>, AsyncRwLockError>;

    /// Poll for a locking grant of the associated [`AsyncRwLock`].
    ///
    /// Upon future completion, either a [`AsyncRwLockWriteGuard`] is returned
    /// or, if the associated [`AsyncRwLock`] had been dropped in the
    /// meanwhile, an error of [`AsyncRwLockError::StaleRwLock`].
    ///
    /// The future must not get polled any further once completed.
    fn poll(self: pin::Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        future::Future::poll(pin::Pin::new(&mut self.get_mut().sem_exclusive_all_fut), cx)
            .map_ok(|sem_exclusive_all_guard| AsyncRwLockWriteGuard {
                sem_exclusive_all_guard,
            })
            .map_err(AsyncRwLockError::from)
    }
}

/// Exclusive locking grant on an [`AsyncRwLock`].
pub struct AsyncRwLockWriteGuard<
    ST: sync_types::SyncTypes,
    T: marker::Send + marker::Sync,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>,
> {
    sem_exclusive_all_guard: semaphore::AsyncSemaphoreExclusiveAllGuard<
        ST,
        T,
        sync_types::SyncRcPtrForInner<AsyncRwLock<ST, T>, LP, AsyncRwLockIndexInnerSemTag>,
    >,
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    AsyncRwLockWriteGuard<ST, T, LP>
{
    /// Obtain the associated [`AsyncRwLock`].
    pub fn get_rwlock(&self) -> LP::SyncRcPtrRef<'_> {
        self.sem_exclusive_all_guard.get_semaphore().get_container().clone()
    }

    /// Release the lock and return the associated [`AsyncRwLock`].
    pub fn into_rwlock(self) -> LP {
        self.sem_exclusive_all_guard.into_semaphore().into_container()
    }

    /// Convert into an [`AsyncRwLockWriteWeakGuard`] not hindering
    /// destruction of the associated [`AsyncRwLock`].
    pub fn into_weak(self) -> AsyncRwLockWriteWeakGuard<ST, T, LP> {
        AsyncRwLockWriteWeakGuard {
            sem_exclusive_all_guard: self.sem_exclusive_all_guard.into_weak(),
        }
    }

    /// Simultaneously obtain an immutable reference to the associated
    /// [`AsyncRwLock`] as well as a mutable one to its wrapped inner value.
    ///
    /// Mutably dereferencing the [`AsyncRwLockWriteGuard`] to obtain a `mut`
    /// reference on the protected value would result in a borrow on `self`
    /// and thus, prohibits any use of [`get_rwlock()`](Self::get_rwlock)
    /// over the course of the reference's lifetime.
    ///
    /// However, due to the [`AsyncRwLockWriteGuard`]'s locking semantics, there
    /// isn't anything problematic about immutably referencing the
    /// associated [`AsyncRwLock`] at the same time. It's useful in e.g.
    /// situations where the lock lives in some zero-overhead
    /// [`SyncRcPtrForInner`](sync_types::SyncRcPtrForInner) and one seeks to
    /// obtain a reference to the container starting out from the lock
    /// guard.
    pub fn borrow_outer_inner_mut<'a>(&'a mut self) -> (LP::SyncRcPtrRef<'a>, &'a mut T) {
        let (sem, v) = self.sem_exclusive_all_guard.borrow_outer_inner_mut();
        (sem.get_container().clone(), v)
    }
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    ops::Deref for AsyncRwLockWriteGuard<ST, T, LP>
{
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.sem_exclusive_all_guard
    }
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    ops::DerefMut for AsyncRwLockWriteGuard<ST, T, LP>
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.sem_exclusive_all_guard
    }
}

/// Weak variant of [`AsyncRwLockWriteGuard`] not hindering destruction of the
/// associated [`AsyncRwLock`].
///
/// In cases were it's desired that long-living locking grants don't prevent a
/// destruction of the associated [`AsyncRwLock`], or to break cycles, a
/// `AsyncRwLockWriteWeakGuard` may be used. To be obtained via
/// [`AsyncRwLockWriteGuard::into_weak()`].
///
/// An `AsyncRwLockWriteWeakGuard` may get converted back into a full
/// [`AsyncRwLockWriteGuard`] via [`upgrade()`](Self::upgrade).
pub struct AsyncRwLockWriteWeakGuard<
    ST: sync_types::SyncTypes,
    T: marker::Send + marker::Sync,
    LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>,
> {
    sem_exclusive_all_guard: semaphore::AsyncSemaphoreExclusiveAllWeakGuard<
        ST,
        T,
        sync_types::SyncRcPtrForInner<AsyncRwLock<ST, T>, LP, AsyncRwLockIndexInnerSemTag>,
    >,
}

impl<ST: sync_types::SyncTypes, T: marker::Send + marker::Sync, LP: sync_types::SyncRcPtr<AsyncRwLock<ST, T>>>
    AsyncRwLockWriteWeakGuard<ST, T, LP>
{
    /// Attempt to convert back into an [`AsyncRwLockWriteGuard`].
    ///
    /// Returns the [`AsyncRwLockWriteGuard`] wrapped in `Some` if the
    /// associated [`AsyncRwLock`] is still alive, `None otherwise.
    pub fn upgrade(self) -> Option<AsyncRwLockWriteGuard<ST, T, LP>> {
        self.sem_exclusive_all_guard
            .upgrade()
            .map(|sem_exclusive_all_guard| AsyncRwLockWriteGuard {
                sem_exclusive_all_guard,
            })
    }
}