Skip to main content

imago/sync_primitives/
mod.rs

1//! Feature-agnostic synchronization primitives
2//!
3//! In async mode, re-exports `tokio::sync` types.  In sync mode, re-exports `parking_lot` types.
4
5#[cfg(feature = "async")]
6mod async_impl;
7#[cfg(feature = "sync")]
8mod sync_impl;
9
10#[cfg(feature = "async")]
11#[allow(unused_imports)]
12pub(crate) use tokio::sync::{
13    Mutex, MutexGuard, OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock, RwLockReadGuard,
14    RwLockWriteGuard,
15};
16
17#[cfg(feature = "async")]
18#[allow(unused_imports)]
19pub(crate) use async_impl::OwnedRwLock;
20
21#[cfg(feature = "sync")]
22#[allow(unused_imports)]
23pub(crate) use parking_lot::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
24
25#[cfg(feature = "sync")]
26#[allow(unused_imports)]
27pub(crate) use sync_impl::{OwnedRwLock, OwnedRwLockReadGuard, OwnedRwLockWriteGuard};
28
29/// Either an owned read guard or a borrowed write guard on an [`OwnedRwLock`].
30///
31/// The guards are only held, never accessed.
32#[derive(Debug)]
33pub(crate) enum RwLockEitherGuard<'a, T> {
34    /// Owned read guard.
35    Read(#[allow(dead_code)] OwnedRwLockReadGuard<T>),
36
37    /// Borrowed write guard.
38    Write(#[allow(dead_code)] &'a OwnedRwLockWriteGuard<T>),
39}
40
41impl<T> From<OwnedRwLockReadGuard<T>> for RwLockEitherGuard<'_, T> {
42    fn from(guard: OwnedRwLockReadGuard<T>) -> Self {
43        RwLockEitherGuard::Read(guard)
44    }
45}
46
47impl<'a, T> From<&'a OwnedRwLockWriteGuard<T>> for RwLockEitherGuard<'a, T> {
48    fn from(guard: &'a OwnedRwLockWriteGuard<T>) -> Self {
49        RwLockEitherGuard::Write(guard)
50    }
51}