Crate collection_tools

source ·
Expand description

§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:


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


use collection_tools::Vec;

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

Instead of

The code above will be expanded to this
#[ 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

Modules§

  • A priority queue implemented with a binary heap.
  • An ordered map based on a B-Tree.
  • An ordered set based on a B-Tree.
  • Namespace with dependencies.
  • Exposed namespace of the module.
  • A hash map implemented with quadratic probing and SIMD lookup.
  • A hash set implemented as a HashMap where the value is ().
  • A doubly-linked list with owned nodes.
  • Parented namespace of the module.
  • Prelude to use essentials: use my_module::prelude::*.
  • Protected namespace of the module.
  • A contiguous growable array type with heap-allocated contents, written Vec<T>.
  • A double-ended queue (deque) implemented with a growable ring buffer.

Macros§

  • Literally just a BTreeMap literal with keys and values into’d.
  • Literally just a BTreeSet literal with values into’d.
  • Literally just a BinaryHeap literal with values into’d.
  • Literally just a HashMap literal with keys and values into’d.
  • Literally just a HashSet literal with values into’d.
  • Literally just a LinkedList literal with values into’d.
  • Creates a Vec containing the arguments.
  • Literally just a VecDeque literal with values into’d.

Structs§

  • An ordered map based on a B-Tree.
  • An ordered set based on a B-Tree.
  • A priority queue implemented with a binary heap.
  • A hash map implemented with quadratic probing and SIMD lookup.
  • A hash set implemented as a HashMap where the value is ().
  • A doubly-linked list with owned nodes.
  • The error type for try_reserve methods.
  • A contiguous growable array type, written as Vec<T>, short for ‘vector’.
  • A double-ended queue implemented with a growable ring buffer.

Enums§

  • An endpoint of a range of keys.
  • Details of the allocation that caused a TryReserveError