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,
1.92.0 · Sourcepub fn downgrade(s: RwLockWriteGuard<'rwlock, T>) -> RwLockReadGuard<'rwlock, T>
pub fn downgrade(s: RwLockWriteGuard<'rwlock, T>) -> RwLockReadGuard<'rwlock, T>
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.
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.
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<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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, A> DynAccess<T> for A
impl<T, A> DynAccess<T> for A
Source§fn load(&self) -> DynGuard<T>
fn load(&self) -> DynGuard<T>
Access::load.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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 moreSource§impl<T> Source for T
impl<T> Source for T
Source§type Slice<'a> = <<T as Deref>::Target as Source>::Slice<'a>
where
T: 'a
type Slice<'a> = <<T as Deref>::Target as Source>::Slice<'a> where T: 'a
Source can be sliced into.Source§fn read<'a, Chunk>(&'a self, offset: usize) -> Option<Chunk>where
Chunk: Chunk<'a>,
fn read<'a, Chunk>(&'a self, offset: usize) -> Option<Chunk>where
Chunk: Chunk<'a>,
None when reading
out of bounds would occur. Read moreSource§unsafe fn read_byte_unchecked(&self, offset: usize) -> u8
unsafe fn read_byte_unchecked(&self, offset: usize) -> u8
Source§fn slice(&self, range: Range<usize>) -> Option<<T as Source>::Slice<'_>>
fn slice(&self, range: Range<usize>) -> Option<<T as Source>::Slice<'_>>
slice::get(range). Read moreSource§unsafe fn slice_unchecked(
&self,
range: Range<usize>,
) -> <T as Source>::Slice<'_>
unsafe fn slice_unchecked( &self, range: Range<usize>, ) -> <T as Source>::Slice<'_>
slice::get_unchecked(range). Read more