Struct SwapLock

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

A reader-writer lock that only syncs when the sync method is called.

This is similar to a RwLock, except that writes are not instantly realized by parallel readers. Instead, writes are only realized when the sync method is called. This is useful for cases where you want to write to a value, but you don’t want to block readers while you do so.

This type only implements Sync when T is Send. Syncing and the non-const new method require T to implement Clone. Values are provided through the Deref and DerefMut implementations on the ReadGuard and Guard types.

§Examples

use no_std_async::SwapLock;

async fn foo() {
    let lock = SwapLock::new(42);
    let read = lock.read().await;
    assert_eq!(*read, 42);

    let mut write = lock.write().await;
    *write += 1;
    assert_eq!(*write, 43); // All writers will read the new value.
    assert_eq!(*read, 42); // The read value is not updated until `sync` is called.

    drop(read);
    drop(write);

    lock.sync().await;
    let read = lock.read().await;
    assert_eq!(*read, 43); // The value has now been updated.
}

Implementations§

Source§

impl<T> SwapLock<T>

Source

pub fn new(val: T) -> Self
where T: Clone,

Creates a new SwapLock with the given data.

Source

pub const fn const_new(data: T, write: T) -> Self

Creates a new SwapLock with the given data and write value. These values should be the same. In new, it is simply cloned, but this is not possible in a const context.

Source

pub async fn sync(&self)
where T: Clone,

Syncs the data with the written value. This function waits for all locks to be released. This means that holding a read or write lock will deadlock if you call this function.

Source

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

Acquires a read lock on the data.

Source

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

Acquires a write lock on the data.

Trait Implementations§

Source§

impl<T: Send> Send for SwapLock<T>

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for SwapLock<T>

§

impl<T> !RefUnwindSafe for SwapLock<T>

§

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

§

impl<T> !UnwindSafe for SwapLock<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.