11个不稳定版本
使用旧的Rust 2015
0.7.6 | 2019年10月14日 |
---|---|
0.7.5 | 2018年8月4日 |
0.7.4 | 2017年10月9日 |
0.7.1 | 2017年5月3日 |
0.6.0 | 2016年9月12日 |
#330 在 内存管理 中
26 每月下载量
66KB
1.5K SLoC
idcontain
一个围绕'generational id-s'构建的Rust库:标记ID,可以防止ID重复使用、ID混合(在不同容器之间)并启用'悬挂ID'检测(使用已删除后的ID)。
目前唯一实现的类型是IdSlab
,它是一个无序集合,有时被称为Slab
分配器。它与@carllerche的出色Slab
相比,在内存和速度上略有牺牲,但支持上述特性。
用于防止ID重复使用和混合的特定实现有一些注意事项(即导致成员测试中出现假阳性的病态情况),这意味着您不应依赖它进行内存安全或安全性。这些情况相当极端,所以您当然可以使用它来制作视频游戏等。有关更多详细信息,请参阅文档。
关于此技术的旧但相关的博客文章。
入门指南
将依赖关系添加到您的Cargo.toml
清单中。
[dependencies]
idcontain = "0.6"
示例
extern crate idcontain;
use idcontain::{IdSlab, Id};
fn main() {
let mut id_vec_int1 = IdSlab::new();
let mut id_vec_int2 = IdSlab::with_capacity(3);
let mut id_vec_str = IdSlab::with_capacity(1);
// Inserting an element returns its `Id`.
let id1: Id<i32> = id_vec_int1.insert(1);
assert_eq!(id_vec_int1[id1], 1); // Panicking lookup.
assert_eq!(id_vec_int1.get(id1), Some(&1)); // Non-panicking lookup.
id_vec_int1[id1] = 10; // In-place mutation.
assert_eq!(id_vec_int1.remove(id1), Some(10)); // Removal.
// Id-s are not reused.
let id2 = id_vec_int1.insert(20);
assert!(id2 != id1);
// Id-s from different `IdSlab`-s do not collide.
let id3 = id_vec_int2.insert(20);
assert!(id3 != id2);
assert!(id3 != id1);
// Id-s from `IdSlab`-s of different types cannot mixed with compile-time
// checks.
let str_id: Id<&'static str> = id_vec_str.insert("hello");
assert_eq!(id_vec_str[str_id], "hello")
// Compile-time errors:
// str_id == id1
// id_vec_int1.get(str_id)
// id_vec_str[id1]
}
迭代和其他功能也受到支持,请参阅文档!
依赖关系
~385KB