1use crate::sync::rwlock::RwLock;
2use std::marker::PhantomData;
3use std::sync::Arc;
4use std::{fmt, mem, ops, ptr};
5
6/// Owned RAII structure used to release the shared read access of a lock when
7/// dropped.
8///
9/// This structure is created by the [`read_owned`] method on
10/// [`RwLock`].
11///
12/// [`read_owned`]: method@crate::sync::RwLock::read_owned
13/// [`RwLock`]: struct@crate::sync::RwLock
14#[clippy::has_significant_drop]
15pub struct OwnedRwLockReadGuard<T: ?Sized, U: ?Sized = T> {
16 // When changing the fields in this struct, make sure to update the
17 // `skip_drop` method.
18 #[cfg(all(tokio_unstable, feature = "tracing"))]
19 pub(super) resource_span: tracing::Span,
20 pub(super) lock: Arc<RwLock<T>>,
21 pub(super) data: *const U,
22 pub(super) _p: PhantomData<T>,
23}
24
25#[allow(dead_code)] // Unused fields are still used in Drop.
26struct Inner<T: ?Sized, U: ?Sized> {
27 #[cfg(all(tokio_unstable, feature = "tracing"))]
28 resource_span: tracing::Span,
29 lock: Arc<RwLock<T>>,
30 data: *const U,
31}
32
33impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
34 fn skip_drop(self) -> Inner<T, U> {
35 let me = mem::ManuallyDrop::new(self);
36 // SAFETY: This duplicates the values in every field of the guard, then
37 // forgets the originals, so in the end no value is duplicated.
38 unsafe {
39 Inner {
40 #[cfg(all(tokio_unstable, feature = "tracing"))]
41 resource_span: ptr::read(&me.resource_span),
42 lock: ptr::read(&me.lock),
43 data: me.data,
44 }
45 }
46 }
47
48 /// Makes a new `OwnedRwLockReadGuard` for a component of the locked data.
49 /// This operation cannot fail as the `OwnedRwLockReadGuard` passed in
50 /// already locked the data.
51 ///
52 /// This is an associated function that needs to be
53 /// used as `OwnedRwLockReadGuard::map(...)`. A method would interfere with
54 /// methods of the same name on the contents of the locked data.
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// use std::sync::Arc;
60 /// use tokio::sync::{RwLock, OwnedRwLockReadGuard};
61 ///
62 /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
63 /// struct Foo(u32);
64 ///
65 /// # #[tokio::main]
66 /// # async fn main() {
67 /// let lock = Arc::new(RwLock::new(Foo(1)));
68 ///
69 /// let guard = lock.read_owned().await;
70 /// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0);
71 ///
72 /// assert_eq!(1, *guard);
73 /// # }
74 /// ```
75 #[inline]
76 pub fn map<F, V: ?Sized>(this: Self, f: F) -> OwnedRwLockReadGuard<T, V>
77 where
78 F: FnOnce(&U) -> &V,
79 {
80 let data = f(&*this) as *const V;
81 let this = this.skip_drop();
82
83 OwnedRwLockReadGuard {
84 lock: this.lock,
85 data,
86 _p: PhantomData,
87 #[cfg(all(tokio_unstable, feature = "tracing"))]
88 resource_span: this.resource_span,
89 }
90 }
91
92 /// Attempts to make a new [`OwnedRwLockReadGuard`] for a component of the
93 /// locked data. The original guard is returned if the closure returns
94 /// `None`.
95 ///
96 /// This operation cannot fail as the `OwnedRwLockReadGuard` passed in
97 /// already locked the data.
98 ///
99 /// This is an associated function that needs to be used as
100 /// `OwnedRwLockReadGuard::try_map(..)`. A method would interfere with
101 /// methods of the same name on the contents of the locked data.
102 ///
103 /// # Examples
104 ///
105 /// ```
106 /// use std::sync::Arc;
107 /// use tokio::sync::{RwLock, OwnedRwLockReadGuard};
108 ///
109 /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
110 /// struct Foo(u32);
111 ///
112 /// # #[tokio::main]
113 /// # async fn main() {
114 /// let lock = Arc::new(RwLock::new(Foo(1)));
115 ///
116 /// let guard = lock.read_owned().await;
117 /// let guard = OwnedRwLockReadGuard::try_map(guard, |f| Some(&f.0)).expect("should not fail");
118 ///
119 /// assert_eq!(1, *guard);
120 /// # }
121 /// ```
122 #[inline]
123 pub fn try_map<F, V: ?Sized>(this: Self, f: F) -> Result<OwnedRwLockReadGuard<T, V>, Self>
124 where
125 F: FnOnce(&U) -> Option<&V>,
126 {
127 let data = match f(&*this) {
128 Some(data) => data as *const V,
129 None => return Err(this),
130 };
131 let this = this.skip_drop();
132
133 Ok(OwnedRwLockReadGuard {
134 lock: this.lock,
135 data,
136 _p: PhantomData,
137 #[cfg(all(tokio_unstable, feature = "tracing"))]
138 resource_span: this.resource_span,
139 })
140 }
141}
142
143impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockReadGuard<T, U> {
144 type Target = U;
145
146 fn deref(&self) -> &U {
147 unsafe { &*self.data }
148 }
149}
150
151impl<T: ?Sized, U: ?Sized> fmt::Debug for OwnedRwLockReadGuard<T, U>
152where
153 U: fmt::Debug,
154{
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 fmt::Debug::fmt(&**self, f)
157 }
158}
159
160impl<T: ?Sized, U: ?Sized> fmt::Display for OwnedRwLockReadGuard<T, U>
161where
162 U: fmt::Display,
163{
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165 fmt::Display::fmt(&**self, f)
166 }
167}
168
169impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockReadGuard<T, U> {
170 fn drop(&mut self) {
171 self.lock.s.release(added:1);
172
173 #[cfg(all(tokio_unstable, feature = "tracing"))]
174 self.resource_span.in_scope(|| {
175 tracing::trace!(
176 target: "runtime::resource::state_update",
177 current_readers = 1,
178 current_readers.op = "sub",
179 )
180 });
181 }
182}
183