| Crates.io | instance-copy-on-write |
| lib.rs | instance-copy-on-write |
| version | 0.9.1 |
| created_at | 2025-09-11 21:50:36.833617+00 |
| updated_at | 2026-01-12 19:04:45.560947+00 |
| description | A MT-safe copy-on-write implementation for the data structures. |
| homepage | |
| repository | https://codeberg.org/4neko/instance-copy-on-write |
| max_upload_size | |
| id | 1834643 |
| size | 125,301 |
The purpose of this crate to attempt to introduce a CoW model of data access synchronization. The create is based on the crossbeam and atomics.
The ICoWRead provides read only which is binded to ICoW and until ICoW is valid. The data will not be updated until re-read.
The ICoWWeak provides read only until ICoW is valid.
The ICoWCopy provides a Copied instance which is modified and commmited back.
The ICoWLock provides an exclusive lock which locks reading and writing.
[!ATTENTION] By default an
RwLockbased implementation is used, because theatomicsbased implementation is still not ready. It can be activated by enabling the featureprefer_atomic.
The ICoWRead<DATA> instance with inner value DATA obtained from the ICoW<DATA> is valid even after the inner value was copied and copy was commited back. All readers which will will read the ICoW instance after the commit will receive a ICoWRead<DATA> with updated DATA.
Exclusive copying prevents readers from reading while simple non-exclusive copy allows to access the inner data for reading and further copying. But, the non-exclusive copies are not in sync, so each copy
is uniq. The sequential commit of copied data is not yet supported.
The pull requests are now supported because the repository was moved to Codeberg. The alternative way is to send patches over the email to patch[at]4neko.org.
In case if you would like to contribute the code, please use pull request. Your pull request should include:
Description of changes and why it is needed.
Test the pull request.
In case of you prefer email and patch files please consider the following:
For each feature or fix, please send patches separatly.
Please write what your patch is implementing or fixing.
I can read the code and I am able to understand it, so don't write a poem or essay in the description to the patches.
Please test your patch.
v 0.9.1 Experimental.
aquire function in ICoWWeak is now sets the flag in ICoWWeakRead which allows to determine if instance was updated. A item_updated returns the result.ICoWWeak was added which allows to store timeless reference to ICoW for reading.read operations
and slower writes.cow_refcell for sinhgle thread apps.transaction style
which in theory is thread safe.ICoWCopy from ICoWRead.Weak referencing which could be obtained from ICoWRead. This would allow to detect if
cached ICoWRead got outdated.Waker which should wakeup the thread/s which was/were parked while
waiting for the exclusive lock. Untested!ICoW::clone() and the rest functions which are related to clone_*() were renamed to clone_copy to avoid collisions with the Clone::clone().
Sources are licensed with: MIT License
There are three types of the operations:
Read-only reference
Copy-on-Write writing and storing
Exclusive copy-on-write (giant lock).
All read instances are valid all the time until dropeed, even if the CoW operation was performed.
After CoW instance commited, all new readers will see new value, all previous readers the previous value.
It was designed for the short period and small structures which can be copied. Also, where write operations can fail and the program is ok to use updated value.
An atocis-based CoW operation.
┌─────────────────┐
┌────────────────┤ ICoW<Instance> ┼───────────────┐
▼ └─────────┬───────┘ ▼
│
▼
┌──────────┐ ┌─────────────┐ ┌────────────────────┐
┌──────►│ read() │◄────────────┤ copy(), │ │ copy_exclusivly() │
│ └────┬─────┘ ^ │ clone() │ │ clone_exclusivly() │
│excl.lock │ | └─────────────┘ └─────────┬──────────┘
│ repeat │ | │
│ ┌────────▼───────────┐ V ┌────────────┐ ┌─────────▼──────────┐
└───┼ compare_exchange() │ ┌► │ ICowCopy │ │ compare_exchange() │
└────────┬───────────┘ │ └─────┬──────┘ │ self <- null_ptr │
│ success │ │ └─────────┬──────────┘
│ │ │ │
┌──────▼──────┐ │ │ ┌────────▼──────────┐ locked
│ clone() │ │ │ │ IF │already
└──────┬──────┘ │ │ │ self == null_ptr ┼───────┐
│ │ │ └───────┬───────────┘ │
│ │ │ │ │
│ │ │ │ │
┌────────▼────────────┐ │ │ ┌────────▼────────────┐ │
│ ICowRead<Instance> ├───┘ │ │ ICowLock<Instance> │ │
└────────┬────────────┘ │ └────────┬────────────┘ │
│ │ │ │
│ ▼ │ │
▼ │ │
┌──────────────┐ ┌─────────────┐ │ │
│ / ... / │ │ / ... / │◄──────────────┘ │
└──────┬───────┘ └───┬────────┬┘ │
│ │ OR │ │
│ │ │ │
┌───────▼─────┐ ┌───────▼──┐ ┌─▼────────┐ │
│ drop() │ │ drop() │ │ commit() │ ▼
└─────────────┘ └──────────┘ └──────────┘
let val =
ICoW::new(TestStruct::new(1, 2));
// read only
let read1 = val.read();
//..
drop(read1);
// ...
// copy on write NON-exclusively, read is possible
let mut transaction = val.clone_copy();
transaction.start = 5;
transaction.stop = 6;
// update, after calling this function, all reades who
// read before will still see old version.
// all reades after, new
transaction.commit();
//or
drop(transaction); // to drop changes
// exclusive lock, read is also not possible
let res = val.clone_copy_exclusivly();
// ..
// commit changes releasing lock
commit(val); //or
drop(val); // to drop changes and release lock