10 次重大发布
| 0.11.0 | 2024 年 7 月 13 日 |
|---|---|
| 0.9.0 | 2024 年 6 月 10 日 |
| 0.4.0 | 2024 年 3 月 22 日 |
#1886 在 开发工具
500 次每月下载
用于 20 个包(直接使用 4 个)
58KB
401 行
模块 :: collection_tools
通用工具集合,用于操作集合(如 Vec/HashMap/HashSet...)。
基本用例 :: 集合的可变参数构造函数
本模块包含一套元工具,旨在增强 Rust 的集合处理能力,最显著的是通过包含可变参数构造函数。一个主要例子是 hmap! 宏,它简化了 HashMap 实例的创建。这些构造函数允许直观且简洁地初始化集合,类似于其他编程语言的简单性。
以下是一个示例,展示了如何使用 hmap! 宏轻松地创建一个 HashMap
# #[ cfg( all( feature = "enabled", feature = "collection_constructors" ) ) ]
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {
use collection_tools::*;
let meta_map = hmap! { 3 => 13 };
// it is identical to `hashbrown::HashMap` if `use_alloc` feature is on, otherwise `std::collections::HashMap`
let mut std_map = collection_tools::HashMap::new();
std_map.insert( 3, 13 );
assert_eq!( meta_map, std_map );
# }
另一个示例,这次是 into_bset!,为你提供一个 BTreeSet
# #[ cfg( all( feature = "enabled", feature = "collection_constructors" ) ) ]
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {
use collection_tools::*;
let meta_set = bset! { 3, 13 };
// this `BTreeSet` is just a reexport from `alloc`,
// so it can be used in the same places as `alloc/std::BTreeSet`
let mut std_set = collection_tools::BTreeSet::new();
std_set.insert( 13 );
std_set.insert( 3 );
assert_eq!( meta_set, std_set );
# }
另一个使用 list! 的示例
# #[ cfg( all( feature = "enabled", feature = "collection_constructors" ) ) ]
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {
use collection_tools::*;
let meta_list : LinkedList< i32 > = list! { 3, 13 };
// this `LinkedList` is just a reexport from `alloc`,
// so it can be used in the same places as `alloc/std::LinkedList`
let mut meta_list = collection_tools::LinkedList::new();
meta_list.push_front( 13 );
meta_list.push_front( 3 );
assert_eq!( meta_list, meta_list );
# }
基本用例 :: no_std HashSet / HashMap
在Rust项目中实现带有use_alloc特性的no_std环境时,你将面临挑战:根据std库的可用性,集合如Vec的导入方式不同。此外,要在no_std环境中使用HashSet或HashMap等数据结构,需要依赖于第三方crate,因为这些不是由alloc crate直接提供的。这个crate旨在简化设计Rust库或应用程序的过程,这些库或应用程序需要在no_std环境中使用这些集合,提供一个更简洁的方法来处理动态数据结构,而无需使用标准库。
你可以这样做
# #[ cfg( feature = "enabled" ) ]
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {
use collection_tools::HashSet;
let mut vec : HashSet< i32 > = HashSet::new();
vec.insert( 1 );
assert_eq!( vec.contains( &1 ), true );
# }
而不是
点击查看
# #[ cfg( all( feature = "enabled", feature = "collection_std" ) ) ]
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {
#[ cfg( feature = "use_alloc" ) ]
use hashbrown::HashSet; // a `no_std` replacement for `HashSet`
#[ cfg( not( feature = "no_std" ) ) ]
use std::collections::HashSet;
let mut vec : HashSet< i32 > = HashSet::new();
vec.insert( 1 );
assert_eq!( vec.contains( &1 ), true );
# }
基本用例 :: no_std HashSet / HashMap
该crate有两种类型的宏:严格的宏(我们已讨论过),它要求所有集合成员类型相同;以及更“宽松”的宏,它使用底层的Into trait将类型转换为特定的类型。可以通过在宏名称前加上into_来访问它们(如into_vec、into_bmap等)。
虽然严格的宏要求所有成员类型相同,但更宽松的宏通常要求你指定所需的类型。因此,没有明确的赢家。根据每种情况分别选择正确的一个。
例如
# #[ cfg( all( feature = "enabled", feature = "collection_into_constructors", any( not( feature = "no_std" ), feature = "use_alloc" ) ) ) ]
# {
use std::borrow::Cow;
let vec : Vec< String > = collection_tools::into_vec!( "&str", "String".to_string(), Cow::from( "Cow" ) );
# }
每个严格的宏都有一个宽松的对应宏。
正在使用的集合
那么关于collection_tools::<collection>是什么情况呢?
并没有什么特别之处。我们只是重用了来自alloc(与std相同)的集合。
但是并非所有集合都可在alloc crate中找到。目前,例外的是HashMap和HashSet。这导致我们无法在no_std环境中使用它们。我们是如何解决的?通过在启用no_std功能时使用来自hashbrown crate的这些集合。你可以在其文档页面上找到有关集合来源的更多详细信息。
更多示例
如果你对应该使用宏的语法感到困惑,可以访问其文档。它充满了不同的示例,所以希望你不至于被困住。
将其添加到你的项目中
cargo add collection_tools
从存储库中尝试
git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/container_tools_trivial
cargo run
依赖关系
~0–415KB