pub struct RwLockWriteGuard<'rwlock, T>where
T: 'rwlock + ?Sized,{ /* private fields */ }Expand description
Implementations§
Source§impl<'rwlock, T> RwLockWriteGuard<'rwlock, T>where
T: ?Sized,
impl<'rwlock, T> RwLockWriteGuard<'rwlock, T>where
T: ?Sized,
Sourcepub fn downgrade(s: RwLockWriteGuard<'rwlock, T>) -> RwLockReadGuard<'rwlock, T>
🔬This is a nightly-only experimental API. (rwlock_downgrade)
pub fn downgrade(s: RwLockWriteGuard<'rwlock, T>) -> RwLockReadGuard<'rwlock, T>
rwlock_downgrade)Downgrades a write-locked RwLockWriteGuard into a read-locked RwLockReadGuard.
Since we have the RwLockWriteGuard, the RwLock must already be locked for writing, so
this method cannot fail.
After downgrading, other readers will be allowed to read the protected data.
§Examples
downgrade takes ownership of the RwLockWriteGuard and returns a RwLockReadGuard.
#![feature(rwlock_downgrade)]
use std::sync::{RwLock, RwLockWriteGuard};
let rw = RwLock::new(0);
let mut write_guard = rw.write().unwrap();
*write_guard = 42;
let read_guard = RwLockWriteGuard::downgrade(write_guard);
assert_eq!(42, *read_guard);downgrade will atomically change the state of the RwLock from exclusive mode into
shared mode. This means that it is impossible for another writing thread to get in between a
thread calling downgrade and any reads it performs after downgrading.
#![feature(rwlock_downgrade)]
use std::sync::{Arc, RwLock, RwLockWriteGuard};
let rw = Arc::new(RwLock::new(1));
// Put the lock in write mode.
let mut main_write_guard = rw.write().unwrap();
let rw_clone = rw.clone();
let evil_handle = std::thread::spawn(move || {
// This will not return until the main thread drops the `main_read_guard`.
let mut evil_guard = rw_clone.write().unwrap();
assert_eq!(*evil_guard, 2);
*evil_guard = 3;
});
*main_write_guard = 2;
// Atomically downgrade the write guard into a read guard.
let main_read_guard = RwLockWriteGuard::downgrade(main_write_guard);
// Since `downgrade` is atomic, the writer thread cannot have changed the protected data.
assert_eq!(*main_read_guard, 2, "`downgrade` was not atomic");Sourcepub fn map<U, F>(
orig: RwLockWriteGuard<'rwlock, T>,
f: F,
) -> MappedRwLockWriteGuard<'rwlock, U>
🔬This is a nightly-only experimental API. (mapped_lock_guards)
pub fn map<U, F>( orig: RwLockWriteGuard<'rwlock, T>, f: F, ) -> MappedRwLockWriteGuard<'rwlock, U>
mapped_lock_guards)Makes a MappedRwLockWriteGuard for a component of the borrowed data, e.g.
an enum variant.
The RwLock is already locked for writing, so this cannot fail.
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 RwLockWriteGuard used through
Deref.
§Panics
If the closure panics, the guard will be dropped (unlocked) and the RwLock will be poisoned.
Sourcepub fn filter_map<U, F>(
orig: RwLockWriteGuard<'rwlock, T>,
f: F,
) -> Result<MappedRwLockWriteGuard<'rwlock, U>, RwLockWriteGuard<'rwlock, T>>
🔬This is a nightly-only experimental API. (mapped_lock_guards)
pub fn filter_map<U, F>( orig: RwLockWriteGuard<'rwlock, T>, f: F, ) -> Result<MappedRwLockWriteGuard<'rwlock, U>, RwLockWriteGuard<'rwlock, T>>
mapped_lock_guards)Makes a MappedRwLockWriteGuard for a component of the borrowed data. The
original guard is returned as an Err(...) if the closure returns
None.
The RwLock is already locked for writing, so this cannot fail.
This is an associated function that needs to be used as
RwLockWriteGuard::filter_map(...). A method would interfere with methods
of the same name on the contents of the RwLockWriteGuard used through
Deref.
§Panics
If the closure panics, the guard will be dropped (unlocked) and the RwLock will be poisoned.
Trait Implementations§
1.16.0 · Source§impl<T> Debug for RwLockWriteGuard<'_, T>
impl<T> Debug for RwLockWriteGuard<'_, T>
1.0.0 · Source§impl<T> Deref for RwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> Deref for RwLockWriteGuard<'_, T>where
T: ?Sized,
1.0.0 · Source§impl<T> DerefMut for RwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> DerefMut for RwLockWriteGuard<'_, T>where
T: ?Sized,
1.20.0 · Source§impl<T> Display for RwLockWriteGuard<'_, T>
impl<T> Display for RwLockWriteGuard<'_, T>
1.0.0 · Source§impl<T> Drop for RwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> Drop for RwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> !Send for RwLockWriteGuard<'_, T>where
T: ?Sized,
impl<T> Sync for RwLockWriteGuard<'_, T>
Auto Trait Implementations§
impl<'rwlock, T> Freeze for RwLockWriteGuard<'rwlock, T>where
T: ?Sized,
impl<'rwlock, T> RefUnwindSafe for RwLockWriteGuard<'rwlock, T>where
T: ?Sized,
impl<'rwlock, T> Unpin for RwLockWriteGuard<'rwlock, T>where
T: ?Sized,
impl<'rwlock, T> UnwindSafe for RwLockWriteGuard<'rwlock, T>where
T: ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W), ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
Source§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X), ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more