calf-vec

Crates.iocalf-vec
lib.rscalf-vec
version0.3.1-beta
sourcesrc
created_at2020-10-18 16:26:58.282036
updated_at2020-10-19 19:24:23.993405
descriptionSmall copy-on-write arrays, essentially combining `SmallVec` and `Cow`
homepage
repositoryhttps://github.com/timothee-haudebourg/calf-vec
max_upload_size
id302246
size41,845
Timothée Haudebourg (timothee-haudebourg)

documentation

https://docs.rs/calf-vec

README

Small copy-on-write arrays for Rust

Documentation Crate informations Repository

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.

Commit count: 25

cargo fmt