summaryrefslogtreecommitdiffstats
path: root/vendor/tokio/src/sync/rwlock
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/tokio/src/sync/rwlock
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tokio/src/sync/rwlock')
-rw-r--r--vendor/tokio/src/sync/rwlock/owned_read_guard.rs149
-rw-r--r--vendor/tokio/src/sync/rwlock/owned_write_guard.rs234
-rw-r--r--vendor/tokio/src/sync/rwlock/owned_write_guard_mapped.rs171
-rw-r--r--vendor/tokio/src/sync/rwlock/read_guard.rs156
-rw-r--r--vendor/tokio/src/sync/rwlock/write_guard.rs240
-rw-r--r--vendor/tokio/src/sync/rwlock/write_guard_mapped.rs176
6 files changed, 1126 insertions, 0 deletions
diff --git a/vendor/tokio/src/sync/rwlock/owned_read_guard.rs b/vendor/tokio/src/sync/rwlock/owned_read_guard.rs
new file mode 100644
index 000000000..b7f3926a4
--- /dev/null
+++ b/vendor/tokio/src/sync/rwlock/owned_read_guard.rs
@@ -0,0 +1,149 @@
+use crate::sync::rwlock::RwLock;
+use std::fmt;
+use std::marker::PhantomData;
+use std::mem;
+use std::mem::ManuallyDrop;
+use std::ops;
+use std::sync::Arc;
+
+/// Owned RAII structure used to release the shared read access of a lock when
+/// dropped.
+///
+/// This structure is created by the [`read_owned`] method on
+/// [`RwLock`].
+///
+/// [`read_owned`]: method@crate::sync::RwLock::read_owned
+/// [`RwLock`]: struct@crate::sync::RwLock
+pub struct OwnedRwLockReadGuard<T: ?Sized, U: ?Sized = T> {
+ // ManuallyDrop allows us to destructure into this field without running the destructor.
+ pub(super) lock: ManuallyDrop<Arc<RwLock<T>>>,
+ pub(super) data: *const U,
+ pub(super) _p: PhantomData<T>,
+}
+
+impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
+ /// Make a new `OwnedRwLockReadGuard` for a component of the locked data.
+ /// This operation cannot fail as the `OwnedRwLockReadGuard` passed in
+ /// already locked the data.
+ ///
+ /// This is an associated function that needs to be
+ /// used as `OwnedRwLockReadGuard::map(...)`. A method would interfere with
+ /// methods of the same name on the contents of the locked data.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::sync::Arc;
+ /// use tokio::sync::{RwLock, OwnedRwLockReadGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(Foo(1)));
+ ///
+ /// let guard = lock.read_owned().await;
+ /// let guard = OwnedRwLockReadGuard::map(guard, |f| &f.0);
+ ///
+ /// assert_eq!(1, *guard);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn map<F, V: ?Sized>(mut this: Self, f: F) -> OwnedRwLockReadGuard<T, V>
+ where
+ F: FnOnce(&U) -> &V,
+ {
+ let data = f(&*this) as *const V;
+ let lock = unsafe { ManuallyDrop::take(&mut this.lock) };
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ OwnedRwLockReadGuard {
+ lock: ManuallyDrop::new(lock),
+ data,
+ _p: PhantomData,
+ }
+ }
+
+ /// Attempts to make a new [`OwnedRwLockReadGuard`] for a component of the
+ /// locked data. The original guard is returned if the closure returns
+ /// `None`.
+ ///
+ /// This operation cannot fail as the `OwnedRwLockReadGuard` passed in
+ /// already locked the data.
+ ///
+ /// This is an associated function that needs to be used as
+ /// `OwnedRwLockReadGuard::try_map(..)`. A method would interfere with
+ /// methods of the same name on the contents of the locked data.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::sync::Arc;
+ /// use tokio::sync::{RwLock, OwnedRwLockReadGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(Foo(1)));
+ ///
+ /// let guard = lock.read_owned().await;
+ /// let guard = OwnedRwLockReadGuard::try_map(guard, |f| Some(&f.0)).expect("should not fail");
+ ///
+ /// assert_eq!(1, *guard);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn try_map<F, V: ?Sized>(mut this: Self, f: F) -> Result<OwnedRwLockReadGuard<T, V>, Self>
+ where
+ F: FnOnce(&U) -> Option<&V>,
+ {
+ let data = match f(&*this) {
+ Some(data) => data as *const V,
+ None => return Err(this),
+ };
+ let lock = unsafe { ManuallyDrop::take(&mut this.lock) };
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ Ok(OwnedRwLockReadGuard {
+ lock: ManuallyDrop::new(lock),
+ data,
+ _p: PhantomData,
+ })
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockReadGuard<T, U> {
+ type Target = U;
+
+ fn deref(&self) -> &U {
+ unsafe { &*self.data }
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> fmt::Debug for OwnedRwLockReadGuard<T, U>
+where
+ U: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> fmt::Display for OwnedRwLockReadGuard<T, U>
+where
+ U: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Display::fmt(&**self, f)
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockReadGuard<T, U> {
+ fn drop(&mut self) {
+ self.lock.s.release(1);
+ unsafe { ManuallyDrop::drop(&mut self.lock) };
+ }
+}
diff --git a/vendor/tokio/src/sync/rwlock/owned_write_guard.rs b/vendor/tokio/src/sync/rwlock/owned_write_guard.rs
new file mode 100644
index 000000000..91b659524
--- /dev/null
+++ b/vendor/tokio/src/sync/rwlock/owned_write_guard.rs
@@ -0,0 +1,234 @@
+use crate::sync::rwlock::owned_read_guard::OwnedRwLockReadGuard;
+use crate::sync::rwlock::owned_write_guard_mapped::OwnedRwLockMappedWriteGuard;
+use crate::sync::rwlock::RwLock;
+use std::fmt;
+use std::marker::PhantomData;
+use std::mem::{self, ManuallyDrop};
+use std::ops;
+use std::sync::Arc;
+
+/// Owned RAII structure used to release the exclusive write access of a lock when
+/// dropped.
+///
+/// This structure is created by the [`write_owned`] method
+/// on [`RwLock`].
+///
+/// [`write_owned`]: method@crate::sync::RwLock::write_owned
+/// [`RwLock`]: struct@crate::sync::RwLock
+pub struct OwnedRwLockWriteGuard<T: ?Sized> {
+ pub(super) permits_acquired: u32,
+ // ManuallyDrop allows us to destructure into this field without running the destructor.
+ pub(super) lock: ManuallyDrop<Arc<RwLock<T>>>,
+ pub(super) data: *mut T,
+ pub(super) _p: PhantomData<T>,
+}
+
+impl<T: ?Sized> OwnedRwLockWriteGuard<T> {
+ /// Make a new [`OwnedRwLockMappedWriteGuard`] for a component of the locked
+ /// data.
+ ///
+ /// This operation cannot fail as the `OwnedRwLockWriteGuard` passed in
+ /// already locked the data.
+ ///
+ /// This is an associated function that needs to be used as
+ /// `OwnedRwLockWriteGuard::map(..)`. A method would interfere with methods
+ /// of the same name on the contents of the locked data.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::sync::Arc;
+ /// use tokio::sync::{RwLock, OwnedRwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(Foo(1)));
+ ///
+ /// {
+ /// let lock = Arc::clone(&lock);
+ /// let mut mapped = OwnedRwLockWriteGuard::map(lock.write_owned().await, |f| &mut f.0);
+ /// *mapped = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn map<F, U: ?Sized>(mut this: Self, f: F) -> OwnedRwLockMappedWriteGuard<T, U>
+ where
+ F: FnOnce(&mut T) -> &mut U,
+ {
+ let data = f(&mut *this) as *mut U;
+ let lock = unsafe { ManuallyDrop::take(&mut this.lock) };
+ let permits_acquired = this.permits_acquired;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ OwnedRwLockMappedWriteGuard {
+ permits_acquired,
+ lock: ManuallyDrop::new(lock),
+ data,
+ _p: PhantomData,
+ }
+ }
+
+ /// Attempts to make a new [`OwnedRwLockMappedWriteGuard`] for a component
+ /// of the locked data. The original guard is returned if the closure
+ /// returns `None`.
+ ///
+ /// This operation cannot fail as the `OwnedRwLockWriteGuard` passed in
+ /// already locked the data.
+ ///
+ /// This is an associated function that needs to be
+ /// used as `OwnedRwLockWriteGuard::try_map(...)`. A method would interfere
+ /// with methods of the same name on the contents of the locked data.
+ ///
+ /// [`RwLockMappedWriteGuard`]: struct@crate::sync::RwLockMappedWriteGuard
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::sync::Arc;
+ /// use tokio::sync::{RwLock, OwnedRwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(Foo(1)));
+ ///
+ /// {
+ /// let guard = Arc::clone(&lock).write_owned().await;
+ /// let mut guard = OwnedRwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail");
+ /// *guard = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn try_map<F, U: ?Sized>(
+ mut this: Self,
+ f: F,
+ ) -> Result<OwnedRwLockMappedWriteGuard<T, U>, Self>
+ where
+ F: FnOnce(&mut T) -> Option<&mut U>,
+ {
+ let data = match f(&mut *this) {
+ Some(data) => data as *mut U,
+ None => return Err(this),
+ };
+ let permits_acquired = this.permits_acquired;
+ let lock = unsafe { ManuallyDrop::take(&mut this.lock) };
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ Ok(OwnedRwLockMappedWriteGuard {
+ permits_acquired,
+ lock: ManuallyDrop::new(lock),
+ data,
+ _p: PhantomData,
+ })
+ }
+
+ /// Converts this `OwnedRwLockWriteGuard` into an
+ /// `OwnedRwLockMappedWriteGuard`. This method can be used to store a
+ /// non-mapped guard in a struct field that expects a mapped guard.
+ ///
+ /// This is equivalent to calling `OwnedRwLockWriteGuard::map(guard, |me| me)`.
+ #[inline]
+ pub fn into_mapped(this: Self) -> OwnedRwLockMappedWriteGuard<T> {
+ Self::map(this, |me| me)
+ }
+
+ /// Atomically downgrades a write lock into a read lock without allowing
+ /// any writers to take exclusive access of the lock in the meantime.
+ ///
+ /// **Note:** This won't *necessarily* allow any additional readers to acquire
+ /// locks, since [`RwLock`] is fair and it is possible that a writer is next
+ /// in line.
+ ///
+ /// Returns an RAII guard which will drop this read access of the `RwLock`
+ /// when dropped.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use tokio::sync::RwLock;
+ /// # use std::sync::Arc;
+ /// #
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(1));
+ ///
+ /// let n = lock.clone().write_owned().await;
+ ///
+ /// let cloned_lock = lock.clone();
+ /// let handle = tokio::spawn(async move {
+ /// *cloned_lock.write_owned().await = 2;
+ /// });
+ ///
+ /// let n = n.downgrade();
+ /// assert_eq!(*n, 1, "downgrade is atomic");
+ ///
+ /// drop(n);
+ /// handle.await.unwrap();
+ /// assert_eq!(*lock.read().await, 2, "second writer obtained write lock");
+ /// # }
+ /// ```
+ pub fn downgrade(mut self) -> OwnedRwLockReadGuard<T> {
+ let lock = unsafe { ManuallyDrop::take(&mut self.lock) };
+ let data = self.data;
+
+ // Release all but one of the permits held by the write guard
+ lock.s.release((self.permits_acquired - 1) as usize);
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(self);
+ OwnedRwLockReadGuard {
+ lock: ManuallyDrop::new(lock),
+ data,
+ _p: PhantomData,
+ }
+ }
+}
+
+impl<T: ?Sized> ops::Deref for OwnedRwLockWriteGuard<T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ unsafe { &*self.data }
+ }
+}
+
+impl<T: ?Sized> ops::DerefMut for OwnedRwLockWriteGuard<T> {
+ fn deref_mut(&mut self) -> &mut T {
+ unsafe { &mut *self.data }
+ }
+}
+
+impl<T: ?Sized> fmt::Debug for OwnedRwLockWriteGuard<T>
+where
+ T: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
+impl<T: ?Sized> fmt::Display for OwnedRwLockWriteGuard<T>
+where
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Display::fmt(&**self, f)
+ }
+}
+
+impl<T: ?Sized> Drop for OwnedRwLockWriteGuard<T> {
+ fn drop(&mut self) {
+ self.lock.s.release(self.permits_acquired as usize);
+ unsafe { ManuallyDrop::drop(&mut self.lock) };
+ }
+}
diff --git a/vendor/tokio/src/sync/rwlock/owned_write_guard_mapped.rs b/vendor/tokio/src/sync/rwlock/owned_write_guard_mapped.rs
new file mode 100644
index 000000000..6453236eb
--- /dev/null
+++ b/vendor/tokio/src/sync/rwlock/owned_write_guard_mapped.rs
@@ -0,0 +1,171 @@
+use crate::sync::rwlock::RwLock;
+use std::fmt;
+use std::marker::PhantomData;
+use std::mem::{self, ManuallyDrop};
+use std::ops;
+use std::sync::Arc;
+
+/// Owned RAII structure used to release the exclusive write access of a lock when
+/// dropped.
+///
+/// This structure is created by [mapping] an [`OwnedRwLockWriteGuard`]. It is a
+/// separate type from `OwnedRwLockWriteGuard` to disallow downgrading a mapped
+/// guard, since doing so can cause undefined behavior.
+///
+/// [mapping]: method@crate::sync::OwnedRwLockWriteGuard::map
+/// [`OwnedRwLockWriteGuard`]: struct@crate::sync::OwnedRwLockWriteGuard
+pub struct OwnedRwLockMappedWriteGuard<T: ?Sized, U: ?Sized = T> {
+ pub(super) permits_acquired: u32,
+ // ManuallyDrop allows us to destructure into this field without running the destructor.
+ pub(super) lock: ManuallyDrop<Arc<RwLock<T>>>,
+ pub(super) data: *mut U,
+ pub(super) _p: PhantomData<T>,
+}
+
+impl<T: ?Sized, U: ?Sized> OwnedRwLockMappedWriteGuard<T, U> {
+ /// Make a new `OwnedRwLockMappedWriteGuard` for a component of the locked
+ /// data.
+ ///
+ /// This operation cannot fail as the `OwnedRwLockMappedWriteGuard` passed
+ /// in already locked the data.
+ ///
+ /// This is an associated function that needs to be used as
+ /// `OwnedRwLockWriteGuard::map(..)`. A method would interfere with methods
+ /// of the same name on the contents of the locked data.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::sync::Arc;
+ /// use tokio::sync::{RwLock, OwnedRwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(Foo(1)));
+ ///
+ /// {
+ /// let lock = Arc::clone(&lock);
+ /// let mut mapped = OwnedRwLockWriteGuard::map(lock.write_owned().await, |f| &mut f.0);
+ /// *mapped = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn map<F, V: ?Sized>(mut this: Self, f: F) -> OwnedRwLockMappedWriteGuard<T, V>
+ where
+ F: FnOnce(&mut U) -> &mut V,
+ {
+ let data = f(&mut *this) as *mut V;
+ let lock = unsafe { ManuallyDrop::take(&mut this.lock) };
+ let permits_acquired = this.permits_acquired;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ OwnedRwLockMappedWriteGuard {
+ permits_acquired,
+ lock: ManuallyDrop::new(lock),
+ data,
+ _p: PhantomData,
+ }
+ }
+
+ /// Attempts to make a new `OwnedRwLockMappedWriteGuard` for a component
+ /// of the locked data. The original guard is returned if the closure
+ /// returns `None`.
+ ///
+ /// This operation cannot fail as the `OwnedRwLockMappedWriteGuard` passed
+ /// in already locked the data.
+ ///
+ /// This is an associated function that needs to be
+ /// used as `OwnedRwLockMappedWriteGuard::try_map(...)`. A method would interfere with
+ /// methods of the same name on the contents of the locked data.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::sync::Arc;
+ /// use tokio::sync::{RwLock, OwnedRwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(Foo(1)));
+ ///
+ /// {
+ /// let guard = Arc::clone(&lock).write_owned().await;
+ /// let mut guard = OwnedRwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail");
+ /// *guard = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn try_map<F, V: ?Sized>(
+ mut this: Self,
+ f: F,
+ ) -> Result<OwnedRwLockMappedWriteGuard<T, V>, Self>
+ where
+ F: FnOnce(&mut U) -> Option<&mut V>,
+ {
+ let data = match f(&mut *this) {
+ Some(data) => data as *mut V,
+ None => return Err(this),
+ };
+ let lock = unsafe { ManuallyDrop::take(&mut this.lock) };
+ let permits_acquired = this.permits_acquired;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ Ok(OwnedRwLockMappedWriteGuard {
+ permits_acquired,
+ lock: ManuallyDrop::new(lock),
+ data,
+ _p: PhantomData,
+ })
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> ops::Deref for OwnedRwLockMappedWriteGuard<T, U> {
+ type Target = U;
+
+ fn deref(&self) -> &U {
+ unsafe { &*self.data }
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> ops::DerefMut for OwnedRwLockMappedWriteGuard<T, U> {
+ fn deref_mut(&mut self) -> &mut U {
+ unsafe { &mut *self.data }
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> fmt::Debug for OwnedRwLockMappedWriteGuard<T, U>
+where
+ U: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> fmt::Display for OwnedRwLockMappedWriteGuard<T, U>
+where
+ U: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Display::fmt(&**self, f)
+ }
+}
+
+impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockMappedWriteGuard<T, U> {
+ fn drop(&mut self) {
+ self.lock.s.release(self.permits_acquired as usize);
+ unsafe { ManuallyDrop::drop(&mut self.lock) };
+ }
+}
diff --git a/vendor/tokio/src/sync/rwlock/read_guard.rs b/vendor/tokio/src/sync/rwlock/read_guard.rs
new file mode 100644
index 000000000..38eec7727
--- /dev/null
+++ b/vendor/tokio/src/sync/rwlock/read_guard.rs
@@ -0,0 +1,156 @@
+use crate::sync::batch_semaphore::Semaphore;
+use std::fmt;
+use std::marker;
+use std::mem;
+use std::ops;
+
+/// RAII structure used to release the shared read access of a lock when
+/// dropped.
+///
+/// This structure is created by the [`read`] method on
+/// [`RwLock`].
+///
+/// [`read`]: method@crate::sync::RwLock::read
+/// [`RwLock`]: struct@crate::sync::RwLock
+pub struct RwLockReadGuard<'a, T: ?Sized> {
+ pub(super) s: &'a Semaphore,
+ pub(super) data: *const T,
+ pub(super) marker: marker::PhantomData<&'a T>,
+}
+
+impl<'a, T: ?Sized> RwLockReadGuard<'a, T> {
+ /// Make a new `RwLockReadGuard` for a component of the locked data.
+ ///
+ /// This operation cannot fail as the `RwLockReadGuard` passed in already
+ /// locked the data.
+ ///
+ /// This is an associated function that needs to be
+ /// used as `RwLockReadGuard::map(...)`. A method would interfere with
+ /// methods of the same name on the contents of the locked data.
+ ///
+ /// This is an asynchronous version of [`RwLockReadGuard::map`] from the
+ /// [`parking_lot` crate].
+ ///
+ /// [`RwLockReadGuard::map`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLockReadGuard.html#method.map
+ /// [`parking_lot` crate]: https://crates.io/crates/parking_lot
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::{RwLock, RwLockReadGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = RwLock::new(Foo(1));
+ ///
+ /// let guard = lock.read().await;
+ /// let guard = RwLockReadGuard::map(guard, |f| &f.0);
+ ///
+ /// assert_eq!(1, *guard);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn map<F, U: ?Sized>(this: Self, f: F) -> RwLockReadGuard<'a, U>
+ where
+ F: FnOnce(&T) -> &U,
+ {
+ let data = f(&*this) as *const U;
+ let s = this.s;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ RwLockReadGuard {
+ s,
+ data,
+ marker: marker::PhantomData,
+ }
+ }
+
+ /// Attempts to make a new [`RwLockReadGuard`] for a component of the
+ /// locked data. The original guard is returned if the closure returns
+ /// `None`.
+ ///
+ /// This operation cannot fail as the `RwLockReadGuard` passed in already
+ /// locked the data.
+ ///
+ /// This is an associated function that needs to be used as
+ /// `RwLockReadGuard::try_map(..)`. A method would interfere with methods of the
+ /// same name on the contents of the locked data.
+ ///
+ /// This is an asynchronous version of [`RwLockReadGuard::try_map`] from the
+ /// [`parking_lot` crate].
+ ///
+ /// [`RwLockReadGuard::try_map`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLockReadGuard.html#method.try_map
+ /// [`parking_lot` crate]: https://crates.io/crates/parking_lot
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::{RwLock, RwLockReadGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = RwLock::new(Foo(1));
+ ///
+ /// let guard = lock.read().await;
+ /// let guard = RwLockReadGuard::try_map(guard, |f| Some(&f.0)).expect("should not fail");
+ ///
+ /// assert_eq!(1, *guard);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn try_map<F, U: ?Sized>(this: Self, f: F) -> Result<RwLockReadGuard<'a, U>, Self>
+ where
+ F: FnOnce(&T) -> Option<&U>,
+ {
+ let data = match f(&*this) {
+ Some(data) => data as *const U,
+ None => return Err(this),
+ };
+ let s = this.s;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ Ok(RwLockReadGuard {
+ s,
+ data,
+ marker: marker::PhantomData,
+ })
+ }
+}
+
+impl<T: ?Sized> ops::Deref for RwLockReadGuard<'_, T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ unsafe { &*self.data }
+ }
+}
+
+impl<'a, T: ?Sized> fmt::Debug for RwLockReadGuard<'a, T>
+where
+ T: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
+impl<'a, T: ?Sized> fmt::Display for RwLockReadGuard<'a, T>
+where
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Display::fmt(&**self, f)
+ }
+}
+
+impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T> {
+ fn drop(&mut self) {
+ self.s.release(1);
+ }
+}
diff --git a/vendor/tokio/src/sync/rwlock/write_guard.rs b/vendor/tokio/src/sync/rwlock/write_guard.rs
new file mode 100644
index 000000000..865a121ed
--- /dev/null
+++ b/vendor/tokio/src/sync/rwlock/write_guard.rs
@@ -0,0 +1,240 @@
+use crate::sync::batch_semaphore::Semaphore;
+use crate::sync::rwlock::read_guard::RwLockReadGuard;
+use crate::sync::rwlock::write_guard_mapped::RwLockMappedWriteGuard;
+use std::fmt;
+use std::marker;
+use std::mem;
+use std::ops;
+
+/// RAII structure used to release the exclusive write access of a lock when
+/// dropped.
+///
+/// This structure is created by the [`write`] method
+/// on [`RwLock`].
+///
+/// [`write`]: method@crate::sync::RwLock::write
+/// [`RwLock`]: struct@crate::sync::RwLock
+pub struct RwLockWriteGuard<'a, T: ?Sized> {
+ pub(super) permits_acquired: u32,
+ pub(super) s: &'a Semaphore,
+ pub(super) data: *mut T,
+ pub(super) marker: marker::PhantomData<&'a mut T>,
+}
+
+impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
+ /// Make a new [`RwLockMappedWriteGuard`] for a component of the locked data.
+ ///
+ /// This operation cannot fail as the `RwLockWriteGuard` passed in already
+ /// locked the data.
+ ///
+ /// This is an associated function that needs to be used as
+ /// `RwLockWriteGuard::map(..)`. A method would interfere with methods of
+ /// the same name on the contents of the locked data.
+ ///
+ /// This is an asynchronous version of [`RwLockWriteGuard::map`] from the
+ /// [`parking_lot` crate].
+ ///
+ /// [`RwLockMappedWriteGuard`]: struct@crate::sync::RwLockMappedWriteGuard
+ /// [`RwLockWriteGuard::map`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLockWriteGuard.html#method.map
+ /// [`parking_lot` crate]: https://crates.io/crates/parking_lot
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::{RwLock, RwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = RwLock::new(Foo(1));
+ ///
+ /// {
+ /// let mut mapped = RwLockWriteGuard::map(lock.write().await, |f| &mut f.0);
+ /// *mapped = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn map<F, U: ?Sized>(mut this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
+ where
+ F: FnOnce(&mut T) -> &mut U,
+ {
+ let data = f(&mut *this) as *mut U;
+ let s = this.s;
+ let permits_acquired = this.permits_acquired;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ RwLockMappedWriteGuard {
+ permits_acquired,
+ s,
+ data,
+ marker: marker::PhantomData,
+ }
+ }
+
+ /// Attempts to make a new [`RwLockMappedWriteGuard`] for a component of
+ /// the locked data. The original guard is returned if the closure returns
+ /// `None`.
+ ///
+ /// This operation cannot fail as the `RwLockWriteGuard` passed in already
+ /// locked the data.
+ ///
+ /// This is an associated function that needs to be
+ /// used as `RwLockWriteGuard::try_map(...)`. A method would interfere with
+ /// methods of the same name on the contents of the locked data.
+ ///
+ /// This is an asynchronous version of [`RwLockWriteGuard::try_map`] from
+ /// the [`parking_lot` crate].
+ ///
+ /// [`RwLockMappedWriteGuard`]: struct@crate::sync::RwLockMappedWriteGuard
+ /// [`RwLockWriteGuard::try_map`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLockWriteGuard.html#method.try_map
+ /// [`parking_lot` crate]: https://crates.io/crates/parking_lot
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::{RwLock, RwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = RwLock::new(Foo(1));
+ ///
+ /// {
+ /// let guard = lock.write().await;
+ /// let mut guard = RwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail");
+ /// *guard = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn try_map<F, U: ?Sized>(
+ mut this: Self,
+ f: F,
+ ) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
+ where
+ F: FnOnce(&mut T) -> Option<&mut U>,
+ {
+ let data = match f(&mut *this) {
+ Some(data) => data as *mut U,
+ None => return Err(this),
+ };
+ let s = this.s;
+ let permits_acquired = this.permits_acquired;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ Ok(RwLockMappedWriteGuard {
+ permits_acquired,
+ s,
+ data,
+ marker: marker::PhantomData,
+ })
+ }
+
+ /// Converts this `RwLockWriteGuard` into an `RwLockMappedWriteGuard`. This
+ /// method can be used to store a non-mapped guard in a struct field that
+ /// expects a mapped guard.
+ ///
+ /// This is equivalent to calling `RwLockWriteGuard::map(guard, |me| me)`.
+ #[inline]
+ pub fn into_mapped(this: Self) -> RwLockMappedWriteGuard<'a, T> {
+ RwLockWriteGuard::map(this, |me| me)
+ }
+
+ /// Atomically downgrades a write lock into a read lock without allowing
+ /// any writers to take exclusive access of the lock in the meantime.
+ ///
+ /// **Note:** This won't *necessarily* allow any additional readers to acquire
+ /// locks, since [`RwLock`] is fair and it is possible that a writer is next
+ /// in line.
+ ///
+ /// Returns an RAII guard which will drop this read access of the `RwLock`
+ /// when dropped.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// # use tokio::sync::RwLock;
+ /// # use std::sync::Arc;
+ /// #
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = Arc::new(RwLock::new(1));
+ ///
+ /// let n = lock.write().await;
+ ///
+ /// let cloned_lock = lock.clone();
+ /// let handle = tokio::spawn(async move {
+ /// *cloned_lock.write().await = 2;
+ /// });
+ ///
+ /// let n = n.downgrade();
+ /// assert_eq!(*n, 1, "downgrade is atomic");
+ ///
+ /// drop(n);
+ /// handle.await.unwrap();
+ /// assert_eq!(*lock.read().await, 2, "second writer obtained write lock");
+ /// # }
+ /// ```
+ ///
+ /// [`RwLock`]: struct@crate::sync::RwLock
+ pub fn downgrade(self) -> RwLockReadGuard<'a, T> {
+ let RwLockWriteGuard { s, data, .. } = self;
+
+ // Release all but one of the permits held by the write guard
+ s.release((self.permits_acquired - 1) as usize);
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(self);
+ RwLockReadGuard {
+ s,
+ data,
+ marker: marker::PhantomData,
+ }
+ }
+}
+
+impl<T: ?Sized> ops::Deref for RwLockWriteGuard<'_, T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ unsafe { &*self.data }
+ }
+}
+
+impl<T: ?Sized> ops::DerefMut for RwLockWriteGuard<'_, T> {
+ fn deref_mut(&mut self) -> &mut T {
+ unsafe { &mut *self.data }
+ }
+}
+
+impl<'a, T: ?Sized> fmt::Debug for RwLockWriteGuard<'a, T>
+where
+ T: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
+impl<'a, T: ?Sized> fmt::Display for RwLockWriteGuard<'a, T>
+where
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Display::fmt(&**self, f)
+ }
+}
+
+impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T> {
+ fn drop(&mut self) {
+ self.s.release(self.permits_acquired as usize);
+ }
+}
diff --git a/vendor/tokio/src/sync/rwlock/write_guard_mapped.rs b/vendor/tokio/src/sync/rwlock/write_guard_mapped.rs
new file mode 100644
index 000000000..9c5b1e7c3
--- /dev/null
+++ b/vendor/tokio/src/sync/rwlock/write_guard_mapped.rs
@@ -0,0 +1,176 @@
+use crate::sync::batch_semaphore::Semaphore;
+use std::fmt;
+use std::marker;
+use std::mem;
+use std::ops;
+
+/// RAII structure used to release the exclusive write access of a lock when
+/// dropped.
+///
+/// This structure is created by [mapping] an [`RwLockWriteGuard`]. It is a
+/// separate type from `RwLockWriteGuard` to disallow downgrading a mapped
+/// guard, since doing so can cause undefined behavior.
+///
+/// [mapping]: method@crate::sync::RwLockWriteGuard::map
+/// [`RwLockWriteGuard`]: struct@crate::sync::RwLockWriteGuard
+pub struct RwLockMappedWriteGuard<'a, T: ?Sized> {
+ pub(super) permits_acquired: u32,
+ pub(super) s: &'a Semaphore,
+ pub(super) data: *mut T,
+ pub(super) marker: marker::PhantomData<&'a mut T>,
+}
+
+impl<'a, T: ?Sized> RwLockMappedWriteGuard<'a, T> {
+ /// Make a new `RwLockMappedWriteGuard` for a component of the locked data.
+ ///
+ /// This operation cannot fail as the `RwLockMappedWriteGuard` passed in already
+ /// locked the data.
+ ///
+ /// This is an associated function that needs to be used as
+ /// `RwLockMappedWriteGuard::map(..)`. A method would interfere with methods
+ /// of the same name on the contents of the locked data.
+ ///
+ /// This is an asynchronous version of [`RwLockWriteGuard::map`] from the
+ /// [`parking_lot` crate].
+ ///
+ /// [`RwLockWriteGuard::map`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLockWriteGuard.html#method.map
+ /// [`parking_lot` crate]: https://crates.io/crates/parking_lot
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::{RwLock, RwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = RwLock::new(Foo(1));
+ ///
+ /// {
+ /// let mut mapped = RwLockWriteGuard::map(lock.write().await, |f| &mut f.0);
+ /// *mapped = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn map<F, U: ?Sized>(mut this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
+ where
+ F: FnOnce(&mut T) -> &mut U,
+ {
+ let data = f(&mut *this) as *mut U;
+ let s = this.s;
+ let permits_acquired = this.permits_acquired;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ RwLockMappedWriteGuard {
+ permits_acquired,
+ s,
+ data,
+ marker: marker::PhantomData,
+ }
+ }
+
+ /// Attempts to make a new [`RwLockMappedWriteGuard`] for a component of
+ /// the locked data. The original guard is returned if the closure returns
+ /// `None`.
+ ///
+ /// This operation cannot fail as the `RwLockMappedWriteGuard` passed in already
+ /// locked the data.
+ ///
+ /// This is an associated function that needs to be
+ /// used as `RwLockMappedWriteGuard::try_map(...)`. A method would interfere
+ /// with methods of the same name on the contents of the locked data.
+ ///
+ /// This is an asynchronous version of [`RwLockWriteGuard::try_map`] from
+ /// the [`parking_lot` crate].
+ ///
+ /// [`RwLockWriteGuard::try_map`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLockWriteGuard.html#method.try_map
+ /// [`parking_lot` crate]: https://crates.io/crates/parking_lot
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::{RwLock, RwLockWriteGuard};
+ ///
+ /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+ /// struct Foo(u32);
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let lock = RwLock::new(Foo(1));
+ ///
+ /// {
+ /// let guard = lock.write().await;
+ /// let mut guard = RwLockWriteGuard::try_map(guard, |f| Some(&mut f.0)).expect("should not fail");
+ /// *guard = 2;
+ /// }
+ ///
+ /// assert_eq!(Foo(2), *lock.read().await);
+ /// # }
+ /// ```
+ #[inline]
+ pub fn try_map<F, U: ?Sized>(
+ mut this: Self,
+ f: F,
+ ) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
+ where
+ F: FnOnce(&mut T) -> Option<&mut U>,
+ {
+ let data = match f(&mut *this) {
+ Some(data) => data as *mut U,
+ None => return Err(this),
+ };
+ let s = this.s;
+ let permits_acquired = this.permits_acquired;
+ // NB: Forget to avoid drop impl from being called.
+ mem::forget(this);
+ Ok(RwLockMappedWriteGuard {
+ permits_acquired,
+ s,
+ data,
+ marker: marker::PhantomData,
+ })
+ }
+}
+
+impl<T: ?Sized> ops::Deref for RwLockMappedWriteGuard<'_, T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ unsafe { &*self.data }
+ }
+}
+
+impl<T: ?Sized> ops::DerefMut for RwLockMappedWriteGuard<'_, T> {
+ fn deref_mut(&mut self) -> &mut T {
+ unsafe { &mut *self.data }
+ }
+}
+
+impl<'a, T: ?Sized> fmt::Debug for RwLockMappedWriteGuard<'a, T>
+where
+ T: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
+impl<'a, T: ?Sized> fmt::Display for RwLockMappedWriteGuard<'a, T>
+where
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Display::fmt(&**self, f)
+ }
+}
+
+impl<'a, T: ?Sized> Drop for RwLockMappedWriteGuard<'a, T> {
+ fn drop(&mut self) {
+ self.s.release(self.permits_acquired as usize);
+ }
+}