calf-vec 0.3.1-beta

Small copy-on-write arrays, essentially combining `SmallVec` and `Cow`
Documentation

Small copy-on-write arrays for Rust

This crate provides the CalfVec data structure for small copy-on-write arrays. As long as the data is not written to, it is only borrowed. When owned, the data is stored on the stack as long as it is small enough. Data is only moved on the heap as a last resort. This is basically the intersection between SmallVec and Cow (Small + Cow = Calf). Additionally this crate provides a CalfString for small copy-on-write strings based on CalfVec.

Basic usage

A CalfVec either borrows or owns its data. You can start by creating a CalfVec from a slice. It will only be copied when the CalfVec is modified.

use calf_vec::CalfVec;

let slice = &[1, 2, 3];
let mut calf: CalfVec<'_, u8, 32> = CalfVec::borrowed(slice); // at this point, data is only borrowed.
calf[0]; // => 1
calf[0] = 4; // because it is modified, the data is copied here.
assert_eq!(calf, [4, 2, 3])

A CalfVec can also be directly created to own its data:

let mut owned: CalfVec<'_, u8, 32> = CalfVec::owned(vec![1, 2, 3]);

Here, since the owned buffer's capacity is smaller than 32 (given as parameter), it is stored on the stack. It will be moved on the heap only when necessary:

owned.push(4);
owned.push(5);
// ...
owned.push(31);
owned.push(32); // <- here the buffer's capacity now exceeds the given limit (32).
                //    it is hence moved on the heap, transparently.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.