Crates.io | calf-vec |
lib.rs | calf-vec |
version | 0.3.1-beta |
source | src |
created_at | 2020-10-18 16:26:58.282036 |
updated_at | 2020-10-19 19:24:23.993405 |
description | Small copy-on-write arrays, essentially combining `SmallVec` and `Cow` |
homepage | |
repository | https://github.com/timothee-haudebourg/calf-vec |
max_upload_size | |
id | 302246 |
size | 41,845 |
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
.
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.
Licensed under either of
at your option.
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.