[][src]Struct async_std::sync::RwLock

pub struct RwLock<T> { /* fields omitted */ }

A reader-writer lock for protecting shared data.

This type is an async version of std::sync::RwLock.

Examples

use async_std::sync::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 locks can be held at a time.
let mut w = lock.write().await;
*w += 1;
assert_eq!(*w, 6);

Methods

impl<T> RwLock<T>[src]

pub fn new(t: T) -> RwLock<T>[src]

Creates a new reader-writer lock.

Examples

use async_std::sync::RwLock;

let lock = RwLock::new(0);

pub async fn read<'_, '_>(&'_ self) -> RwLockReadGuard<'_, T>[src]

Acquires a read lock.

Returns a guard that releases the lock when dropped.

Examples

use async_std::sync::RwLock;

let lock = RwLock::new(1);

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

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

pub fn try_read(&self) -> Option<RwLockReadGuard<T>>[src]

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_std::sync::RwLock;

let lock = RwLock::new(1);

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

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

pub async fn write<'_, '_>(&'_ self) -> RwLockWriteGuard<'_, T>[src]

Acquires a write lock.

Returns a guard that releases the lock when dropped.

Examples

use async_std::sync::RwLock;

let lock = RwLock::new(1);

let mut n = lock.write().await;
*n = 2;

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

pub fn try_write(&self) -> Option<RwLockWriteGuard<T>>[src]

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_std::sync::RwLock;

let lock = RwLock::new(1);

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

assert!(lock.try_write().is_none());

pub fn into_inner(self) -> T[src]

Consumes the lock, returning the underlying data.

Examples

use async_std::sync::RwLock;

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

pub fn get_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the underlying data.

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

Examples

use async_std::sync::RwLock;

let mut lock = RwLock::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.write().await, 10);

Trait Implementations

impl<T: Send> Send for RwLock<T>[src]

impl<T: Send> Sync for RwLock<T>[src]

impl<T> From<T> for RwLock<T>[src]

impl<T: Default> Default for RwLock<T>[src]

impl<T: Debug> Debug for RwLock<T>[src]

Auto Trait Implementations

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

impl<T> UnwindSafe for RwLock<T> where
    T: UnwindSafe

impl<T> !RefUnwindSafe for RwLock<T>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]