[−][src]Struct concread::cowcell::CowCell
A conncurrently readable 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.
Examples
use concread::cowcell::CowCell; let data: i64 = 0; let cowcell = CowCell::new(data); // Begin a read transaction let read_txn = cowcell.read(); assert_eq!(*read_txn, 0); { // Now create a write, and commit it. let mut write_txn = cowcell.write(); *write_txn = 1; // Commit the change write_txn.commit(); } // Show the previous generation still reads '0' assert_eq!(*read_txn, 0); let new_read_txn = cowcell.read(); // And a new read transaction has '1' assert_eq!(*new_read_txn, 1);
Implementations
impl<T> CowCell<T> where
T: Clone,
[src]
T: Clone,
pub fn new(data: T) -> Self
[src]
Create a new CowCell
for storing type T
. T
must implement Clone
to enable clone-on-write.
pub fn read(&self) -> CowCellReadTxn<T>
[src]
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.
pub fn write(&self) -> CowCellWriteTxn<'_, T>
[src]
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.
pub fn try_write(&self) -> Option<CowCellWriteTxn<'_, T>>
[src]
Attempt to create a write transaction. If it fails, and err
is returned. On success the Ok(guard)
is returned. See also
write(&self)
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for CowCell<T>
[src]
impl<T> Send for CowCell<T> where
T: Send + Sync,
[src]
T: Send + Sync,
impl<T> Sync for CowCell<T> where
T: Send + Sync,
[src]
T: Send + Sync,
impl<T> Unpin for CowCell<T>
[src]
impl<T> UnwindSafe for CowCell<T> where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pointable for T
[src]
pub const ALIGN: usize
[src]
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
[src]
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
[src]
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
[src]
pub unsafe fn drop(ptr: usize)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,