Struct RwLock

Source
pub struct RwLock<T> { /* private fields */ }
Expand description

An async reader-writer lock.

This lock allows max_readers readers at once, or one writer. This is different from a mutex, which only allows one lock at a time, regardless of whether it is used for reading or writing.

This lock is fair, meaning that locks are provided in the order that they are requested. This is in contrast to a read-preferring lock, which would continue to allow readers to lock even if a writer is waiting. This stops the possibility of readers starving writers.

This type only implements Sync when T is Send. Values are provided through the Deref and DerefMut implementations on the ReadGuard and WriteGuard types.

§Examples

use no_std_async::RwLock;

async fn foo() {
    let lock = RwLock::new(42);

    {
        // we can have as many readers as we want.
        let reads = [
           lock.read().await,
           lock.read().await,
           lock.read().await,
           lock.read().await,
           // ...and so on
        ];
        assert!(reads.iter().all(|r| **r == 42));
    } // all readers are dropped here.

    {
        // we can only have one writer at a time.
        let mut write = lock.write().await;
        *write += 1;
        assert_eq!(*write, 43);
    } // the writer is dropped here.
}

Implementations§

Source§

impl<T> RwLock<T>

Source

pub const fn new(data: T) -> Self

Creates a new RwLock with the given data.

Source

pub const fn with_max_readers(data: T, max_readers: usize) -> Self

Creates a new RwLock with the given data and maximum number of readers.

Source

pub async fn read(&self) -> ReadGuard<'_, T>

Acquires a read lock on the data.

Source

pub async fn write(&self) -> WriteGuard<'_, T>

Acquires a write lock on the data.

Trait Implementations§

Source§

impl<T: Send> Send for RwLock<T>

Source§

impl<T: Send> Sync for RwLock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for RwLock<T>

§

impl<T> !RefUnwindSafe for RwLock<T>

§

impl<T> Unpin for RwLock<T>
where T: Unpin,

§

impl<T> !UnwindSafe for RwLock<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.