pub struct RwLock<T: ?Sized> { /* private fields */ }
Expand description

An async reader-writer lock.

This type of lock allows multiple readers or one writer at any point in time.

The locking strategy is write-preferring, which means writers are never starved. Releasing a write lock wakes the next blocked reader and the next blocked writer.

Examples

use async_lock::RwLock;

let lock = RwLock::new(5);

// Multiple read locks can be held at a time.
let r1 = lock.read().await;
let r2 = lock.read().await;
assert_eq!(*r1, 5);
assert_eq!(*r2, 5);
drop((r1, r2));

// Only one write lock can be held at a time.
let mut w = lock.write().await;
*w += 1;
assert_eq!(*w, 6);

Implementations

Creates a new reader-writer lock.

Examples
use async_lock::RwLock;

let lock = RwLock::new(0);

Unwraps the lock and returns the inner value.

Examples
use async_lock::RwLock;

let lock = RwLock::new(5);
assert_eq!(lock.into_inner(), 5);

Attempts to acquire a read lock.

If a read lock could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the lock when dropped.

Examples
use async_lock::RwLock;

let lock = RwLock::new(1);

let reader = lock.read().await;
assert_eq!(*reader, 1);

assert!(lock.try_read().is_some());

Acquires a read lock.

Returns a guard that releases the lock when dropped.

Note that attempts to acquire a read lock will block if there are also concurrent attempts to acquire a write lock.

Examples
use async_lock::RwLock;

let lock = RwLock::new(1);

let reader = lock.read().await;
assert_eq!(*reader, 1);

assert!(lock.try_read().is_some());

Attempts to acquire a read lock with the possiblity to upgrade to a write lock.

If a read lock could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the lock when dropped.

Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.

Examples
use async_lock::{RwLock, RwLockUpgradableReadGuard};

let lock = RwLock::new(1);

let reader = lock.upgradable_read().await;
assert_eq!(*reader, 1);
assert_eq!(*lock.try_read().unwrap(), 1);

let mut writer = RwLockUpgradableReadGuard::upgrade(reader).await;
*writer = 2;

Attempts to acquire a read lock with the possiblity to upgrade to a write lock.

Returns a guard that releases the lock when dropped.

Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.

Note that attempts to acquire an upgradable read lock will block if there are concurrent attempts to acquire another upgradable read lock or a write lock.

Examples
use async_lock::{RwLock, RwLockUpgradableReadGuard};

let lock = RwLock::new(1);

let reader = lock.upgradable_read().await;
assert_eq!(*reader, 1);
assert_eq!(*lock.try_read().unwrap(), 1);

let mut writer = RwLockUpgradableReadGuard::upgrade(reader).await;
*writer = 2;

Attempts to acquire a write lock.

If a write lock could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the lock when dropped.

Examples
use async_lock::RwLock;

let lock = RwLock::new(1);

assert!(lock.try_write().is_some());
let reader = lock.read().await;
assert!(lock.try_write().is_none());

Acquires a write lock.

Returns a guard that releases the lock when dropped.

Examples
use async_lock::RwLock;

let lock = RwLock::new(1);

let writer = lock.write().await;
assert!(lock.try_read().is_none());

Returns a mutable reference to the inner value.

Since this call borrows the lock mutably, no actual locking takes place. The mutable borrow statically guarantees no locks exist.

Examples
use async_lock::RwLock;

let mut lock = RwLock::new(1);

*lock.get_mut() = 2;
assert_eq!(*lock.read().await, 2);

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.