Struct RwLock
pub struct RwLock<T>where
T: ?Sized,{ /* private fields */ }
Expand description
An asynchronous reader-writer lock.
This type of lock allows a number of readers or at most one writer at any point in time. The write portion of this lock typically allows modification of the underlying data (exclusive access) and the read portion of this lock typically allows for read-only access (shared access).
The acquisition order of this lock is not guaranteed and depending on the runtime’s implementation and preference of any used polling combinators.
§Examples
let lock = RwLock::new(5);
// many reader locks can be held at once
{
let r1 = lock.read().await;
let r2 = lock.read().await;
assert_eq!(*r1, 5);
assert_eq!(*r2, 5);
} // read locks are dropped at this point
// only one write lock may be held, however
{
let mut w = lock.write().await;
*w += 1;
assert_eq!(*w, 6);
} // write lock is dropped here
Implementations§
§impl<T> RwLock<T>
impl<T> RwLock<T>
pub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the lock, returning the underlying data.
§impl<T> RwLock<T>where
T: ?Sized,
impl<T> RwLock<T>where
T: ?Sized,
pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>, TryLockError>
pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>, TryLockError>
Attempts to acquire this RwLock
with shared read access.
If the access couldn’t be acquired immediately, returns TryLockError
. Otherwise, an RAII
guard is returned which will release read access when dropped.
This function does not block.
This function does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
pub fn try_write(&self) -> Result<RwLockWriteGuard<'_, T>, TryLockError>
pub fn try_write(&self) -> Result<RwLockWriteGuard<'_, T>, TryLockError>
Attempts to lock this RwLock
with exclusive write access.
If the lock could not be acquired immediately, returns TryLockError
. Otherwise, an RAII
guard is returned which will release the lock when it is dropped.
This function does not block.
This function does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
pub async fn read(&self) -> RwLockReadGuard<'_, T>
pub async fn read(&self) -> RwLockReadGuard<'_, T>
Locks the current RwLock
with shared read access, causing the current task to yield
until the lock has been acquired.
This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
Returns an RAII guard which will release this task’s shared access once it is dropped.
pub async fn write(&self) -> RwLockWriteGuard<'_, T>
pub async fn write(&self) -> RwLockWriteGuard<'_, T>
Locks the current RwLock
with exclusive write access, causing the current task to yield
until the lock has been acquired.
This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
Returns an RAII guard which will drop the write access once it is dropped.
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for RwLock<T>
impl<T> !RefUnwindSafe for RwLock<T>
impl<T> !Send for RwLock<T>
impl<T> !Sync for RwLock<T>
impl<T> Unpin for RwLock<T>
impl<T> UnwindSafe for RwLock<T>where
T: UnwindSafe + ?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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
self
to a value of a Properties
struct.source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
self
to a value of a Properties
struct.