Struct RwLock

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

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);

Implementations§

Source§

impl<T> RwLock<T>

Source

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

Creates a new reader-writer lock.

§Examples
use async_std::sync::RwLock;

let lock = RwLock::new(0);
Source§

impl<T: ?Sized> RwLock<T>

Source

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

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());
Source

pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>

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());
Source

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

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());
Source

pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>

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());
Source

pub fn into_inner(self) -> T
where T: Sized,

Consumes the lock, returning the underlying data.

§Examples
use async_std::sync::RwLock;

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

pub fn get_mut(&mut self) -> &mut T

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§

Source§

impl<T: ?Sized + Debug> Debug for RwLock<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized + Default> Default for RwLock<T>

Source§

fn default() -> RwLock<T>

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

impl<T> From<T> for RwLock<T>

Source§

fn from(val: T) -> RwLock<T>

Converts to this type from the input type.
Source§

impl<T: ?Sized + Send> Send for RwLock<T>

Source§

impl<T: ?Sized + Send + Sync> 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 + ?Sized,

§

impl<T> UnwindSafe for RwLock<T>
where T: UnwindSafe + ?Sized,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.