maybe_fut/api/sync/rwlock/
read_guard.rs

1use std::fmt::Display;
2use std::ops::Deref;
3
4/// RAII structure used to release the shared read access of a lock when dropped.
5///
6/// This structure is created by the [`super::RwLock::read`] and [`super::RwLock::try_read`] methods on [`super::RwLock`].
7#[derive(Debug)]
8pub struct RwLockReadGuard<'a, T: ?Sized + 'a>(InnerRwLockReadGuard<'a, T>);
9
10#[derive(Debug)]
11enum InnerRwLockReadGuard<'a, T: ?Sized + 'a> {
12    Std(std::sync::RwLockReadGuard<'a, T>),
13    #[cfg(tokio_sync)]
14    #[cfg_attr(docsrs, doc(cfg(feature = "tokio-sync")))]
15    Tokio(tokio::sync::RwLockReadGuard<'a, T>),
16}
17
18impl<'a, T> From<std::sync::RwLockReadGuard<'a, T>> for RwLockReadGuard<'a, T> {
19    fn from(guard: std::sync::RwLockReadGuard<'a, T>) -> Self {
20        RwLockReadGuard(InnerRwLockReadGuard::Std(guard))
21    }
22}
23
24#[cfg(tokio_sync)]
25#[cfg_attr(docsrs, doc(cfg(feature = "tokio-sync")))]
26impl<'a, T> From<tokio::sync::RwLockReadGuard<'a, T>> for RwLockReadGuard<'a, T> {
27    fn from(guard: tokio::sync::RwLockReadGuard<'a, T>) -> Self {
28        RwLockReadGuard(InnerRwLockReadGuard::Tokio(guard))
29    }
30}
31
32impl<'a, T> Deref for RwLockReadGuard<'a, T>
33where
34    T: ?Sized,
35{
36    type Target = T;
37
38    fn deref(&self) -> &Self::Target {
39        match &self.0 {
40            InnerRwLockReadGuard::Std(guard) => guard.deref(),
41            #[cfg(tokio_sync)]
42            InnerRwLockReadGuard::Tokio(guard) => guard.deref(),
43        }
44    }
45}
46
47impl Display for RwLockReadGuard<'_, str> {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        match &self.0 {
50            InnerRwLockReadGuard::Std(guard) => guard.fmt(f),
51            #[cfg(tokio_sync)]
52            InnerRwLockReadGuard::Tokio(guard) => guard.fmt(f),
53        }
54    }
55}