embassy-sync

Crates

git

Versions

default

Flavors

Struct RwLock

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

Async read-write lock.

The read-write lock is generic over the raw mutex implementation M and the data T it protects. The raw read-write lock is used to guard access to the internal state. It is held for very short periods only, while locking and unlocking. It is not held for the entire time the async RwLock is locked.

Which implementation you select depends on the context in which you’re using the read-write lock.

Use CriticalSectionRawMutex when data can be shared between threads and interrupts.

Use NoopRawMutex when data is only shared between tasks running on the same executor.

Use ThreadModeRawMutex when data is shared between tasks running on the same executor but you want a singleton.

Implementations§

Source§

impl<M, T> RwLock<M, T>
where M: RawMutex,

Async read-write lock.

Source

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

Create a new read-write lock with the given value.

Source§

impl<M, T> RwLock<M, T>
where M: RawMutex, T: ?Sized,

Source

pub fn read(&self) -> impl Future<Output = RwLockReadGuard<'_, M, T>>

Lock the read-write lock for reading.

This will wait for the lock to be available if it’s already locked for writing.

Source

pub fn write(&self) -> impl Future<Output = RwLockWriteGuard<'_, M, T>>

Lock the read-write lock for writing.

This will wait for the lock to be available if it’s already locked for reading or writing.

Source

pub fn try_read(&self) -> Result<RwLockReadGuard<'_, M, T>, TryLockError>

Attempt to immediately lock the rwlock.

If the rwlock is already locked, this will return an error instead of waiting.

Source

pub fn try_write(&self) -> Result<RwLockWriteGuard<'_, M, T>, TryLockError>

Attempt to immediately lock the rwlock.

If the rwlock is already locked, this will return an error instead of waiting.

Source

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

Consumes this read-write lock, returning the underlying data.

Source

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

Returns a mutable reference to the underlying data.

Since this call borrows the RwLock mutably, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist.

Trait Implementations§

Source§

impl<M, T> Debug for RwLock<M, T>
where M: RawMutex, T: ?Sized + Debug,

Source§

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

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

impl<M, T> Default for RwLock<M, T>
where M: RawMutex, T: Default,

Source§

fn default() -> Self

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

impl<M: RawMutex, T> From<T> for RwLock<M, T>

Source§

fn from(from: T) -> Self

Converts to this type from the input type.
Source§

impl<M: RawMutex + Send, T: ?Sized + Send> Send for RwLock<M, T>

Source§

impl<M: RawMutex + Sync, T: ?Sized + Send> Sync for RwLock<M, T>

Auto Trait Implementations§

§

impl<M, T> !Freeze for RwLock<M, T>

§

impl<M, T> !RefUnwindSafe for RwLock<M, T>

§

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

§

impl<M, T> UnwindSafe for RwLock<M, T>
where M: UnwindSafe, 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.