singlefile/container_shared/
guards.rs

1use crate::container::Container;
2
3use std::fmt;
4use std::ops::{Deref, DerefMut};
5
6type RwLockReadGuard<'a, T> = parking_lot::lock_api::RwLockReadGuard<'a, parking_lot::RawRwLock, T>;
7type RwLockWriteGuard<'a, T> = parking_lot::lock_api::RwLockWriteGuard<'a, parking_lot::RawRwLock, T>;
8type ArcRwLockReadGuard<T> = parking_lot::lock_api::ArcRwLockReadGuard<parking_lot::RawRwLock, T>;
9type ArcRwLockWriteGuard<T> = parking_lot::lock_api::ArcRwLockWriteGuard<parking_lot::RawRwLock, T>;
10
11
12
13/// A lifetime-bound, read-only access permit into a [`ContainerShared`].
14///
15/// This structure is created by the [`access`] method on [`ContainerShared`].
16///
17/// [`ContainerShared`]: crate::container_shared::ContainerShared
18/// [`access`]: crate::container_shared::ContainerShared::access
19#[must_use = "if unused the lock will immediately unlock"]
20#[derive(Debug)]
21pub struct AccessGuard<'a, T, Manager> {
22  inner: RwLockReadGuard<'a, Container<T, Manager>>
23}
24
25impl<'a, T, Manager> AccessGuard<'a, T, Manager> {
26  #[inline]
27  pub(super) fn new(inner: RwLockReadGuard<'a, Container<T, Manager>>) -> Self {
28    AccessGuard { inner }
29  }
30
31  /// Gets a reference to the file manager in the underlying [`Container`].
32  #[inline]
33  pub fn manager(&self) -> &Manager {
34    Container::manager(&self.inner)
35  }
36
37  /// Gets a reference to the underlying [`Container`].
38  #[inline]
39  pub fn container(&self) -> &Container<T, Manager> {
40    &self.inner
41  }
42}
43
44impl<'a, T, Manager> Deref for AccessGuard<'a, T, Manager> {
45  type Target = T;
46
47  #[inline]
48  fn deref(&self) -> &Self::Target {
49    Container::get(&self.inner)
50  }
51}
52
53impl<'a, T: fmt::Display, Manager> fmt::Display for AccessGuard<'a, T, Manager> {
54  #[inline]
55  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56    <T as fmt::Display>::fmt(self, f)
57  }
58}
59
60
61
62/// A lifetime-bound, mutable access permit into a [`ContainerShared`].
63///
64/// This structure is created by the [`access_mut`] method on [`ContainerShared`].
65///
66/// [`ContainerShared`]: crate::container_shared::ContainerShared
67/// [`access_mut`]: crate::container_shared::ContainerShared::access_mut
68#[must_use = "if unused the lock will immediately unlock"]
69#[derive(Debug)]
70pub struct AccessGuardMut<'a, T, Manager> {
71  inner: RwLockWriteGuard<'a, Container<T, Manager>>
72}
73
74impl<'a, T, Manager> AccessGuardMut<'a, T, Manager> {
75  #[inline]
76  pub(super) fn new(inner: RwLockWriteGuard<'a, Container<T, Manager>>) -> Self {
77    AccessGuardMut { inner }
78  }
79
80  /// Gets a reference to the file manager in the underlying [`Container`].
81  #[inline]
82  pub fn manager(&self) -> &Manager {
83    Container::manager(&self.inner)
84  }
85
86  /// Gets an immutable reference to the underlying [`Container`].
87  #[inline]
88  pub fn container(&self) -> &Container<T, Manager> {
89    &self.inner
90  }
91
92  /// Gets a mutable reference to the underlying [`Container`].
93  #[inline]
94  pub fn container_mut(&mut self) -> &mut Container<T, Manager> {
95    &mut self.inner
96  }
97
98  /// Downgrades this guard to a read-only [`AccessGuard`], allowing multiple-access.
99  #[inline]
100  pub fn downgrade(self) -> AccessGuard<'a, T, Manager> {
101    AccessGuard { inner: RwLockWriteGuard::downgrade(self.inner) }
102  }
103}
104
105impl<'a, T, Manager> Deref for AccessGuardMut<'a, T, Manager> {
106  type Target = T;
107
108  #[inline]
109  fn deref(&self) -> &Self::Target {
110    Container::get(&self.inner)
111  }
112}
113
114impl<'a, T, Manager> DerefMut for AccessGuardMut<'a, T, Manager> {
115  #[inline]
116  fn deref_mut(&mut self) -> &mut Self::Target {
117    Container::get_mut(&mut self.inner)
118  }
119}
120
121impl<'a, T: fmt::Display, Manager> fmt::Display for AccessGuardMut<'a, T, Manager> {
122  #[inline]
123  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
124    <T as fmt::Display>::fmt(self, f)
125  }
126}
127
128
129
130/// An owned, read-only access permit into a [`ContainerShared`].
131///
132/// This structure is created by the [`access_owned`] method on [`ContainerShared`].
133///
134/// [`ContainerShared`]: crate::container_shared::ContainerShared
135/// [`access_owned`]: crate::container_shared::ContainerShared::access_owned
136#[must_use = "if unused the lock will immediately unlock"]
137#[derive(Debug)]
138pub struct OwnedAccessGuard<T, Manager> {
139  inner: ArcRwLockReadGuard<Container<T, Manager>>
140}
141
142impl<T, Manager> OwnedAccessGuard<T, Manager> {
143  #[inline]
144  pub(super) fn new(inner: ArcRwLockReadGuard<Container<T, Manager>>) -> Self {
145    OwnedAccessGuard { inner }
146  }
147
148  /// Gets a reference to the file manager in the underlying [`Container`].
149  #[inline]
150  pub fn manager(&self) -> &Manager {
151    Container::manager(&self.inner)
152  }
153
154  /// Gets a reference to the underlying [`Container`].
155  #[inline]
156  pub fn container(&self) -> &Container<T, Manager> {
157    &self.inner
158  }
159}
160
161impl<T, Manager> Deref for OwnedAccessGuard<T, Manager> {
162  type Target = T;
163
164  #[inline]
165  fn deref(&self) -> &Self::Target {
166    Container::get(&self.inner)
167  }
168}
169
170impl<T: fmt::Display, Manager> fmt::Display for OwnedAccessGuard<T, Manager> {
171  #[inline]
172  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173    <T as fmt::Display>::fmt(self, f)
174  }
175}
176
177
178
179/// An owned, mutable access permit into a [`ContainerShared`].
180///
181/// This structure is created by the [`access_owned_mut`] method on [`ContainerShared`].
182///
183/// [`ContainerShared`]: crate::container_shared::ContainerShared
184/// [`access_owned_mut`]: crate::container_shared::ContainerShared::access_owned_mut
185#[must_use = "if unused the lock will immediately unlock"]
186#[derive(Debug)]
187pub struct OwnedAccessGuardMut<T, Manager> {
188  inner: ArcRwLockWriteGuard<Container<T, Manager>>
189}
190
191impl<T, Manager> OwnedAccessGuardMut<T, Manager> {
192  #[inline]
193  pub(super) fn new(inner: ArcRwLockWriteGuard<Container<T, Manager>>) -> Self {
194    OwnedAccessGuardMut { inner }
195  }
196
197  /// Gets a reference to the file manager in the underlying [`Container`].
198  #[inline]
199  pub fn manager(&self) -> &Manager {
200    Container::manager(&self.inner)
201  }
202
203  /// Gets an immutable reference to the underlying [`Container`].
204  #[inline]
205  pub fn container(&self) -> &Container<T, Manager> {
206    &self.inner
207  }
208
209  /// Gets a mutable reference to the underlying [`Container`].
210  #[inline]
211  pub fn container_mut(&mut self) -> &mut Container<T, Manager> {
212    &mut self.inner
213  }
214
215  /// Downgrades this guard to a read-only [`OwnedAccessGuard`], allowing multiple-access.
216  #[inline]
217  pub fn downgrade(self) -> OwnedAccessGuard<T, Manager> {
218    OwnedAccessGuard { inner: ArcRwLockWriteGuard::downgrade(self.inner) }
219  }
220}
221
222impl<T, Manager> Deref for OwnedAccessGuardMut<T, Manager> {
223  type Target = T;
224
225  #[inline]
226  fn deref(&self) -> &Self::Target {
227    Container::get(&self.inner)
228  }
229}
230
231impl<T, Manager> DerefMut for OwnedAccessGuardMut<T, Manager> {
232  #[inline]
233  fn deref_mut(&mut self) -> &mut Self::Target {
234    Container::get_mut(&mut self.inner)
235  }
236}
237
238impl<T: fmt::Display, Manager> fmt::Display for OwnedAccessGuardMut<T, Manager> {
239  #[inline]
240  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
241    <T as fmt::Display>::fmt(self, f)
242  }
243}