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>
impl<T> SwapLock<T>
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more