pub struct ICoW<ITEM: Sized> { /* private fields */ }Expand description
A main structure which implements CoW approach based on [RwLock]. The object
stored inside can be read directly, but modifying the inner value is
performed using CoW copy-on-write approach.
The read operations are faster than write, because in order to write a
more operations needs to be performed.
This is for Mutlithreading environment only. It will be usedless when using
in single-thread programs. For the single thread, use a SICoW which would
improve the performance.
The inner value must be either Copy, Clone, Default, or provide new value manually. The copied value is modified and stored back either shared or exclusive method. The exclusive lock prevents other CoW operations guaranteeing the uniq write access.
#[derive(Debug, Clone)]
struct TestStruct { s: u32 }
let cow_val = ICoW::new(TestStruct{ s: 2 });
// read
let read0 = cow_val.read().unwrap();
// ...
drop(read0);
// write new non-exclusivly
let mut write0 = cow_val.try_clone_copy().unwrap();
write0.s = 3;
write0.commit().unwrap();
// write new exclusivly
let mut write0 = cow_val.try_clone_copy_exclusivly().unwrap();
write0.s = 3;
write0.commit().unwrap();
Implementations§
Source§impl<ITEM> ICoW<ITEM>
impl<ITEM> ICoW<ITEM>
Sourcepub fn read(&self) -> ICoWRead<'_, ITEM>
pub fn read(&self) -> ICoWRead<'_, ITEM>
Attempts to read the inner value returning the guard. This function blocks the current thread until the value becomes available. This can happen if exclusive copy-on-write is in progress.
§Returns
An instance with clonned reference is returned.
Sourcepub fn new_inplace(&self, new_item: ITEM)
pub fn new_inplace(&self, new_item: ITEM)
Updates old value to new value for the inner.