4个版本 (2个破坏性更新)
0.3.1-beta | 2020年10月19日 |
---|---|
0.3.0-beta | 2020年10月19日 |
0.2.0-alpha | 2020年10月18日 |
0.1.0-alpha | 2020年10月18日 |
#2336 in 数据结构
42KB
774 行
Rust的小型copy-on-write数组
文档 | 包信息 | 仓库 |
此包提供了用于小型copy-on-write数组的CalfVec
数据结构。只要数据未被写入,它就只被借用。当拥有数据时,只要数据足够小,它就存储在栈上。作为最后手段,数据才会移动到堆上。这基本上是SmallVec
和Cow
的交集 (Small
+ Cow
= Calf
)。此外,此包还提供了基于CalfVec
的小型copy-on-write字符串CalfString
。
基本用法
CalfVec
要么借用数据,要么拥有数据。您可以首先从切片创建一个CalfVec
。只有在修改CalfVec
时才会进行复制。
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])
CalfVec
也可以直接创建来拥有其数据
let mut owned: CalfVec<'_, u8, 32> = CalfVec::owned(vec![1, 2, 3]);
在这里,由于拥有缓冲区的容量小于32(作为参数给出),它存储在栈上。只有在必要时才会将其移动到堆上
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.
许可证
在以下许可证中选择一项
- Apache许可证版本2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
由您选择。
贡献
除非您明确声明,否则您根据Apache-2.0许可证定义的任何有意提交以包含在作品中的贡献,都应如上所述双重许可,不附加任何其他条款或条件。