collection_tools 0.4.0

Collection of general purpose tools to manipulate collections( containers like Vec/HashMap/HashSet ).
Documentation

Module :: collection_tools

experimentalrust-statusdocs.rsOpen in Gitpod discord

Collection of general purpose tools to manipulate collections( containers like Vec/HashMap/HashSet... ).

Basic Use Case :: Variadic Constructors for Collections

This module encompasses a suite of meta-tools designed to enhance Rust's collection handling, most notably through the inclusion of variadic constructors. A prime example is the hmap! macro, which facilitates the ergonomic construction of HashMap instances. These constructors allow for the intuitive and concise initialization of collections, mirroring the simplicity found in other programming languages.

Consider the following example, which demonstrates the use of the hmap! macro to effortlessly create a HashMap:

# #[ cfg( not( feature = "use_alloc" ) ) ]
# #[ 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 };
let mut std_map = std::collections::HashMap::new();
std_map.insert( 3, 13 );
assert_eq!( meta_map, std_map );

# }

Basic Use Case :: no_std HashSet / HashMap

When implementing a no_std environment with the use_alloc feature in your Rust project, you'll encounter a challenge: collections like Vec are imported differently depending on the availability of the std library. Moreover, to use data structures such as HashSet or HashMap in a no_std context, it's necessary to depend on third-party crates, as these are not provided by the alloc crate directly. This crate aims to simplify the process of designing Rust libraries or applications that require these collections in a no_std environment, offering a more streamlined approach to working with dynamic data structures without the standard library.

You can do

# #[ cfg( not( feature = "use_alloc" ) ) ]
# #[ cfg( all( feature = "enabled", feature = "collection_std" ) ) ]
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {

use collection_tools::Vec;

let mut map : Vec< i32 > = Vec::new();
map.push( 1 );
assert_eq!( map.first().unwrap().clone(), 1 );

# }

Instead of

#[ cfg( feature = "use_alloc" ) ]
extern crate alloc;
#[ cfg( feature = "use_alloc" ) ]
use alloc::vec::Vec;
#[ cfg( not( feature = "no_std" ) ) ]
use std::vec::Vec;

let mut collection : Vec< i32 > = Vec::new();
collection.push( 1 );
assert_eq!( collection.first().unwrap().clone(), 1 );

To add to your project

cargo add collection_tools

Try out from the repository

git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/container_tools_trivial
cargo run