cow_arc

Crates.iocow_arc
lib.rscow_arc
version0.2.2
sourcesrc
created_at2021-02-01 07:25:03.454142
updated_at2021-03-17 06:55:25.091362
descriptionCowArc can be useful for decreasing memory allocations by sharing immutable memory.
homepagehttps://github.com/DevNulPavel/cow_arc
repositoryhttps://github.com/DevNulPavel/cow_arc
max_upload_size
id349165
size7,277
(DevNulPavel)

documentation

https://docs.rs/crate/cow_arc/

README

Purposes

CowArc can be useful for decreasing memory allocations by sharing immutable memory.

It saves some RAM by sharing immutable values between CowArc clones. Memory allocates only in case of changing value.

CowArc can be usefull for creating builders.

Examples

let source = CowArc::new(vec![1, 2, 3]);

// Still shared memory
let mut changed = cloned.clone();
assert!(std::ptr::eq(source.deref(), changed.deref()) == true);
assert!(changed.eq(&vec![1, 2, 3]));

// New memory allocation
changed.set_val(vec![1, 2, 3, 4]);
assert!(std::ptr::eq(source.deref(), changed.deref()) == false);
assert!(changed.eq(&vec![1, 2, 3, 4]));
let source = CowArc::new(vec![1, 2, 3]);

// Still shared memory
let mut updated = source.clone();
assert!(std::ptr::eq(source.deref(), updated.deref()) == true);
assert!(changed.eq(&vec![1, 2, 3]));

// New memory allocation
updated.update_val(|val|{
        val.push(4);
});
assert!(std::ptr::eq(source.deref(), updated.deref()) == false);
assert!(updated.eq(&vec![1, 2, 3, 4]));
Commit count: 8

cargo fmt