Skip to content

DevNulPavel/cow_arc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust

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]));

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages