pub struct CowCell<T> { /* private fields */ }
Expand description
A conncurrently readable async cell.
This structure behaves in a similar manner to a RwLock<T>
. However unlike
a RwLock
, writes and parallel reads can be performed at the same time. This
means readers and writers do no block either other. Writers are serialised.
To achieve this a form of “copy-on-write” (or for Rust, clone on write) is used. As a write transaction begins, we clone the existing data to a new location that is capable of being mutated.
Readers are guaranteed that the content of the CowCell
will live as long
as the read transaction is open, and will be consistent for the duration
of the transaction. There can be an “unlimited” number of readers in parallel
accessing different generations of data of the CowCell
.
Writers are serialised and are guaranteed they have exclusive write access to the structure.
Implementations§
source§impl<T> CowCell<T>where
T: Clone,
impl<T> CowCell<T>where
T: Clone,
sourcepub fn new(data: T) -> Self
pub fn new(data: T) -> Self
Create a new CowCell
for storing type T
. T
must implement Clone
to enable clone-on-write.
sourcepub fn read<'x>(&'x self) -> CowCellReadTxn<T>
pub fn read<'x>(&'x self) -> CowCellReadTxn<T>
Begin a read transaction, returning a read guard. The content of the read guard is guaranteed to be consistent for the life time of the read - even if writers commit during.
sourcepub async fn write<'x>(&'x self) -> CowCellWriteTxn<'x, T>
pub async fn write<'x>(&'x self) -> CowCellWriteTxn<'x, T>
Begin a write transaction, returning a write guard. The content of the
write is only visible to this thread, and is not visible to any reader
until commit()
is called.
sourcepub async fn try_write<'x>(&'x self) -> Option<CowCellWriteTxn<'x, T>>
pub async fn try_write<'x>(&'x self) -> Option<CowCellWriteTxn<'x, T>>
Attempt to create a write transaction. If it fails, and err
is returned. On success the Ok(guard)
is returned. See also
write(&self)