pub struct RwLock<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

Creates a new RwLock containing value T

Consumes the lock, returning the underlying data.

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.

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.

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.

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.

Returns a mutable reference to the underlying data.

This call borrows RwLock mutably (at compile-time) so there is no need for dynamic checks.

Trait Implementations

Formats the value using the given formatter. Read more

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

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.