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(MAX_READS - 1);
        // NB: Forget to avoid drop impl from being called.
        mem::forget(self);
        RwLockReadGuard {
            s,
            data,
            marker: marker::PhantomData,
        }
    }
/// 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@RwLock
Atomically downgrades a write lock into a read lock without allowing
any writers to take exclusive access of the lock in the meantime.

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.
Atomically downgrades a write lock into a read lock without allowing any writers to take exclusive access of the lock in the meantime . 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 .
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(MAX_READS - 1);
        // NB: Forget to avoid drop impl from being called.
        mem::forget(self);
        RwLockReadGuard {
            s,
            data,
            marker: marker::PhantomData,
        }
    }
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 ( MAX_READS - 1 ) ; // NB: Forget to avoid drop impl from being called. mem :: forget ( self ) ; RwLockReadGuard { s , data , marker : marker :: PhantomData , } }
Atomically downgrades a write lock into a read lock without allowing
any writers to take exclusive access of the lock in the meantime.
Atomically downgrades a write lock into a read lock without allowing any writers to take exclusive access of the lock in the meantime .
self
examples
None
use tokio::sync::rwlock;
None
use std::sync::arc;
None
#[tokio::main]
None
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;
}).



drop(n);
handle.await.unwrap();
assert_eq!(*lock.read().await, 2, "second writer obtained write lock").
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 ; } ) . drop ( n ) ; handle . await . unwrap () ; assert_eq! ( * lock . read () . await 2 " second writer obtained write lock " ) .
}
None