9 个版本
| 0.2.1 | 2020 年 1 月 16 日 |
|---|---|
| 0.2.0 | 2020 年 1 月 16 日 |
| 0.1.6 | 2019 年 10 月 8 日 |
#2355 在 Rust 模式
每月 34 次下载
11KB
152 行
fast-map
一个小型库和自定义 derive,用于创建一个类似于 map 的结构体,它使用 match 表达式来 获取 和 插入 值。
如果在编译时知道你的键,这个库在支持的 map 操作中可能会比 HashMap 更快。
为包装结构体提供以下操作(通过 derive 宏)
MyMap::get,返回Result<Option<&V>, Error>MyMap::get_mut,返回Result<Option<&mut V>, Error>MyMap::insert,返回Result<Option<V>, Error>,其中V是如果存在则的旧值MyMap::remove,返回Result<Option<V>, Error>MyMap::values,返回一个&V迭代器
如果你知道你的操作不会失败(例如,如果你的键类型是 enum,并且你将所有变体作为键列出),你可以将 infallible = true 添加到你的 derive 属性中,这将 unwrap 映射操作的结果。
用法
fn main() {
pub enum A { A, B, C, D };
#[derive(Default, fast_map::FastMap)]
// We know this cannot fail, since we list all the `enum` variants, so we add `infallible = true`
#[fast_map(infallible = true, keys(A::A, A::B, A::C, A::D))]
struct Foo(fast_map::Map4<A, String>);
let mut foo = Foo::default();
foo.insert(A::B, "B".into());
assert_eq!(foo.get(A::B), Some(&"B".to_string()));
assert_eq!(foo.get(A::C), None);
foo.insert(A::C, "C".into());
assert_eq!(foo.values().collect::<Vec<_>>().len(), 2);
}
更新日志
0.2.1
- 将非错误操作的依赖项作为宏属性添加回来(
infallible = true)。默认值为false。
0.2.0
- 已移除
easy和strictMapLike特性。即使是对于get,显式处理未知键也是更好的做法。 - 向封装结构体添加了
get_mut操作
当前版本:0.2.1
许可证:MIT
依赖项
~1.5MB
~33K SLoC