From dae28cc2a5de937593913c9e29dea826f2deebd3 Mon Sep 17 00:00:00 2001 From: Anton Parfonov Date: Mon, 13 May 2024 12:17:16 +0300 Subject: [PATCH] Separate file for each collection, tests reorganization, remove collection_std feature --- module/core/collection_tools/Cargo.toml | 6 +- module/core/collection_tools/Readme.md | 2 +- .../core/collection_tools/src/collections.rs | 32 + .../collection_tools/src/collections/bmap.rs | 168 ++++ .../collection_tools/src/collections/bset.rs | 154 ++++ .../collection_tools/src/collections/heap.rs | 151 ++++ .../collection_tools/src/collections/hmap.rs | 172 ++++ .../collection_tools/src/collections/hset.rs | 173 ++++ .../collection_tools/src/collections/list.rs | 169 ++++ .../collection_tools/src/collections/vec.rs | 169 ++++ .../collection_tools/src/collections/vecd.rs | 173 ++++ .../core/collection_tools/src/constructors.rs | 582 -------------- .../collection_tools/src/into_constructors.rs | 738 ------------------ module/core/collection_tools/src/lib.rs | 96 +-- module/core/collection_tools/src/vec.rs | 2 - .../core/collection_tools/tests/inc/bmap.rs | 52 ++ .../core/collection_tools/tests/inc/bset.rs | 51 ++ .../tests/inc/constructors.rs | 171 ---- .../core/collection_tools/tests/inc/heap.rs | 52 ++ .../core/collection_tools/tests/inc/hmap.rs | 62 ++ .../core/collection_tools/tests/inc/hset.rs | 58 ++ .../tests/inc/into_constructors.rs | 173 ---- .../core/collection_tools/tests/inc/list.rs | 51 ++ module/core/collection_tools/tests/inc/mod.rs | 21 +- .../collection_tools/tests/inc/reexport.rs | 105 --- module/core/collection_tools/tests/inc/vec.rs | 64 ++ .../core/collection_tools/tests/inc/vecd.rs | 51 ++ module/core/former/Cargo.toml | 2 +- 28 files changed, 1846 insertions(+), 1854 deletions(-) create mode 100644 module/core/collection_tools/src/collections.rs create mode 100644 module/core/collection_tools/src/collections/bmap.rs create mode 100644 module/core/collection_tools/src/collections/bset.rs create mode 100644 module/core/collection_tools/src/collections/heap.rs create mode 100644 module/core/collection_tools/src/collections/hmap.rs create mode 100644 module/core/collection_tools/src/collections/hset.rs create mode 100644 module/core/collection_tools/src/collections/list.rs create mode 100644 module/core/collection_tools/src/collections/vec.rs create mode 100644 module/core/collection_tools/src/collections/vecd.rs delete mode 100644 module/core/collection_tools/src/constructors.rs delete mode 100644 module/core/collection_tools/src/into_constructors.rs delete mode 100644 module/core/collection_tools/src/vec.rs create mode 100644 module/core/collection_tools/tests/inc/bmap.rs create mode 100644 module/core/collection_tools/tests/inc/bset.rs delete mode 100644 module/core/collection_tools/tests/inc/constructors.rs create mode 100644 module/core/collection_tools/tests/inc/heap.rs create mode 100644 module/core/collection_tools/tests/inc/hmap.rs create mode 100644 module/core/collection_tools/tests/inc/hset.rs delete mode 100644 module/core/collection_tools/tests/inc/into_constructors.rs create mode 100644 module/core/collection_tools/tests/inc/list.rs delete mode 100644 module/core/collection_tools/tests/inc/reexport.rs create mode 100644 module/core/collection_tools/tests/inc/vec.rs create mode 100644 module/core/collection_tools/tests/inc/vecd.rs diff --git a/module/core/collection_tools/Cargo.toml b/module/core/collection_tools/Cargo.toml index 1c9f60b9b5..58aca1b1b3 100644 --- a/module/core/collection_tools/Cargo.toml +++ b/module/core/collection_tools/Cargo.toml @@ -42,7 +42,6 @@ default = [ "prelude", "collection_constructors", "collection_into_constructors", - "collection_std", ] full = [ @@ -50,7 +49,6 @@ full = [ "prelude", "collection_constructors", "collection_into_constructors", - "collection_std", ] enabled = [] @@ -60,9 +58,7 @@ prelude = [] collection_constructors = [] # Collection constructors, using `into()` under the hood, like `into_hmap!( "key" => "val" )` collection_into_constructors = [] -# STD collection for no_std. -collection_std = [] -# qqq : is this feature used? seems not. if yes, what is it responsible for? discuss +# qqq : is this feature used? seems not. if yes, what is it responsible for? discuss -- not needed, removed [dependencies] diff --git a/module/core/collection_tools/Readme.md b/module/core/collection_tools/Readme.md index 07893f301d..1eae122a75 100644 --- a/module/core/collection_tools/Readme.md +++ b/module/core/collection_tools/Readme.md @@ -77,7 +77,7 @@ You can do ```rust -# #[ cfg( all( feature = "enabled", feature = "collection_std" ) ) ] +# #[ cfg( feature = "enabled" ) ] # #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] # { use collection_tools::HashSet; diff --git a/module/core/collection_tools/src/collections.rs b/module/core/collection_tools/src/collections.rs new file mode 100644 index 0000000000..090cf82267 --- /dev/null +++ b/module/core/collection_tools/src/collections.rs @@ -0,0 +1,32 @@ +/// Not meant to be called directly. +#[ doc( hidden ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! count +{ + ( @single $( $x : tt )* ) => ( () ); + + ( + @count $( $rest : expr ),* + ) + => + ( + < [ () ] >::len( &[ $( count!( @single $rest ) ),* ] ) + ); +} + +/// [BTreeMap] macros +pub mod bmap; +/// [BTreeSet] macros +pub mod bset; +/// [BinaryHeap] macros +pub mod heap; +/// [HashMap] macros +pub mod hmap; +/// [HashSet] macros +pub mod hset; +/// [LinkedList] macros +pub mod list; +/// [Vec] macros +pub mod vec; +/// [VecDeque] macros +pub mod vecd; diff --git a/module/core/collection_tools/src/collections/bmap.rs b/module/core/collection_tools/src/collections/bmap.rs new file mode 100644 index 0000000000..513e5f2753 --- /dev/null +++ b/module/core/collection_tools/src/collections/bmap.rs @@ -0,0 +1,168 @@ +/// Creates a `BTreeMap` from a list of key-value pairs. +/// +/// The `bmap` macro facilitates the convenient creation of a `BTreeMap` with initial elements. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ BTreeMap, bmap }; +/// // BTreeMap of &str to i32 +/// let map1 = bmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// +/// // BTreeMap of &str to &str +/// let map2 = bmap!{ "name" => "value" }; +/// +/// // With trailing comma +/// let map3 = bmap!( 1 => "one", 2 => "two", 3 => "three", ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `BTreeMap`. +/// Each key and value can be of any type that implements the `Into< K >` and `Into< V >` traits, where `K` and `V` are the +/// types stored in the `BTreeMap` as keys and values, respectively. +/// +/// # Returns +/// +/// Returns a `BTreeMap` containing all the specified key-value pairs. The map's capacity is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices and integer values: +/// +/// ```rust +/// # use collection_tools::{ BTreeMap, bmap }; +/// let map = bmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// assert_eq!( map.get( "one" ), Some( &1 ) ); +/// assert_eq!( map.get( "two" ), Some( &2 ) ); +/// assert_eq!( map.get( "three" ), Some( &3 ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `BTreeMap` of integers to string slices from literals: +/// +/// ```rust +/// # use collection_tools::{ BTreeMap, bmap }; +/// let numbers = bmap!( 1 => "one", 2 => "two", 3 => "three" ); +/// assert_eq!( numbers.get( &1 ), Some( &"one" ) ); +/// assert_eq!( numbers.get( &2 ), Some( &"two" ) ); +/// assert_eq!( numbers.get( &3 ), Some( &"three" ) ); +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! bmap +{ + ( + $( $key : expr => $value : expr ),* $( , )? + ) + => + {{ + let mut _map = collection_tools::BTreeMap::new(); + $( + let _ = _map.insert( $key , $value ); + )* + _map + }}; +} + +/// Creates a `BTreeMap` from a list of key-value pairs. +/// +/// The `into_bmap` macro facilitates the convenient creation of a `BTreeMap` with initial elements. +/// Keys and values passed to the macro are automatically converted into the map's key and value types +/// using `.into()`, enabling the use of literals or values of different, but convertible types. +/// +/// Note: The `into_bmap` macro relies on the `.into()` method to convert each key and value into the target types +/// of the `BTreeMap`. This means that the keys and values must be compatible with the `Into< K >` and `Into< V >` traits +/// for the key type `K` and value type `V` used in the `BTreeMap`. Also, this means that sometimes you must specify the type of collection's items. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ BTreeMap, into_bmap }; +/// // BTreeMap of &str to i32 +/// let map1 : BTreeMap< &str, i32 > = into_bmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// +/// // BTreeMap of String to String +/// let map2 : BTreeMap< String, String > = into_bmap!{ "name" => "value" }; +/// +/// // With trailing comma +/// let map3 : BTreeMap< i32, &str > = into_bmap!( 1 => "one", 2 => "two", 3 => "three", ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `BTreeMap`. +/// Each key and value can be of any type that implements the `Into< K >` and `Into< V >` traits, where `K` and `V` are the +/// types stored in the `BTreeMap` as keys and values, respectively. +/// +/// # Returns +/// +/// Returns a `BTreeMap` containing all the specified key-value pairs. The map's capacity is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices and integer values: +/// +/// ```rust +/// # use collection_tools::{ BTreeMap, into_bmap }; +/// let map : BTreeMap< &str, i32 > = into_bmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// assert_eq!( map.get( "one" ), Some( &1 ) ); +/// assert_eq!( map.get( "two" ), Some( &2 ) ); +/// assert_eq!( map.get( "three" ), Some( &3 ) ); +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into< K >` and `Into< V >`: +/// +/// ```rust +/// # use collection_tools::{ BTreeMap, into_bmap }; +/// let months : BTreeMap< String, i32 > = into_bmap!( "January" => 1, "February" => 2, "March" => 3 ); +/// assert_eq!( months.get( &"January".to_string() ), Some( &1 ) ); +/// assert_eq!( months.get( &"February".to_string() ), Some( &2 ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `BTreeMap` of integers to strings from literals: +/// +/// ```rust +/// # use collection_tools::{ BTreeMap, into_bmap }; +/// let numbers : BTreeMap< i32, String > = into_bmap!( 1 => "one", 2 => "two", 3 => "three" ); +/// assert_eq!( numbers.get( &1 ), Some( &"one".to_string() ) ); +/// assert_eq!( numbers.get( &2 ), Some( &"two".to_string() ) ); +/// assert_eq!( numbers.get( &3 ), Some( &"three".to_string() ) ); +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_bmap +{ + ( + $( $key : expr => $value : expr ),* $( , )? + ) + => + {{ + let mut _map = collection_tools::BTreeMap::new(); + $( + let _ = _map.insert( Into::into( $key ), Into::into( $value ) ); + )* + _map + }}; +} diff --git a/module/core/collection_tools/src/collections/bset.rs b/module/core/collection_tools/src/collections/bset.rs new file mode 100644 index 0000000000..6b5dfc4226 --- /dev/null +++ b/module/core/collection_tools/src/collections/bset.rs @@ -0,0 +1,154 @@ +/// Creates a `BTreeSet` from a list of elements. +/// +/// The `bset` macro allows for convenient creation of a `BTreeSet` with initial elements. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ BTreeSet, bset }; +/// // BTreeSet of &str +/// let set1 = bset!( "a", "b", "c" ); +/// +/// // With trailing comma +/// let set3 = bset!( 1, 2, 3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BTreeSet`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `BTreeSet`. +/// +/// # Returns +/// +/// Returns a `BTreeSet` containing all the specified elements. The capacity of the set is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices: +/// +/// ```rust +/// # use collection_tools::{ BTreeSet, bset }; +/// let set = bset!( "one", "two", "three" ); +/// assert!( set.contains( "one" ) ); +/// assert!( set.contains( "two" ) ); +/// assert!( set.contains( "three" ) ); +/// assert_eq!( set.len(), 3 ); +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! bset +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let mut _set = collection_tools::BTreeSet::new(); + $( + _set.insert( $key ); + )* + _set + }}; +} + +/// Creates a `BTreeSet` from a list of elements. +/// +/// The `into_bset` macro allows for convenient creation of a `BTreeSet` with initial elements. +/// Elements passed to the macro are automatically converted into the set's element type +/// using `.into()`, facilitating the use of literals or values of different, but convertible types. +/// +/// Note: The `into_bset` macro relies on the `.into()` method to convert each element into the target type +/// of the `BTreeSet`. This means that the elements must be compatible with the `Into` trait for the +/// type `T` used in the `BTreeSet`. Also, this means that sometimes you must specify the type of collection's items. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ BTreeSet, into_bset }; +/// // BTreeSet of &str +/// let set1 : BTreeSet< &str > = into_bset!( "a", "b", "c" ); +/// +/// // BTreeSet of String +/// let set2 : BTreeSet< String > = into_bset!{ "a".to_string(), "b", "c" }; +/// +/// // With trailing comma +/// let set3 : BTreeSet< i32 > = into_bset!( 1, 2, 3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BTreeSet`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `BTreeSet`. +/// +/// # Returns +/// +/// Returns a `BTreeSet` containing all the specified elements. The capacity of the set is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices: +/// +/// ```rust +/// # use collection_tools::{ BTreeSet, into_bset }; +/// let set : BTreeSet< &str > = into_bset!( "one", "two", "three" ); +/// assert!( set.contains( "one" ) ); +/// assert!( set.contains( "two" ) ); +/// assert!( set.contains( "three" ) ); +/// assert_eq!( set.len(), 3 ); +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into`: +/// +/// ```rust +/// # use collection_tools::{ BTreeSet, into_bset }; +/// let numbers : BTreeSet< i32 > = into_bset!( 1, 2, 3 ); +/// assert!( numbers.contains( &1 ) ); +/// assert!( numbers.contains( &2 ) ); +/// assert!( numbers.contains( &3 ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `BTreeSet` of `String` from string literals: +/// +/// ```rust +/// # use collection_tools::{ BTreeSet, into_bset }; +/// let s : BTreeSet< String > = into_bset!{ "value" }; +/// assert!( s.contains( "value" ) ); +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_bset +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let mut _set = collection_tools::BTreeSet::new(); + $( + _set.insert( Into::into( $key ) ); + )* + _set + }}; +} diff --git a/module/core/collection_tools/src/collections/heap.rs b/module/core/collection_tools/src/collections/heap.rs new file mode 100644 index 0000000000..f76a302933 --- /dev/null +++ b/module/core/collection_tools/src/collections/heap.rs @@ -0,0 +1,151 @@ +/// Creates a `BinaryHeap` from a list of elements. +/// +/// The `into_heap` macro simplifies the creation of a `BinaryHeap` with initial elements. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ BinaryHeap, heap }; +/// // BinaryHeap of i32 +/// let heap1 = heap!( 3, 1, 4, 1, 5, 9 ); +/// +/// // BinaryHeap of &str +/// let heap2 = heap!{ "pear", "apple", "banana" }; +/// +/// // With trailing comma +/// let heap3 = heap!( 2, 7, 1, 8, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BinaryHeap`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `BinaryHeap`. +/// +/// # Returns +/// +/// Returns a `BinaryHeap` containing all the specified elements. The capacity of the heap is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{ BinaryHeap, heap }; +/// let heap = heap!( 5, 3, 7, 1 ); +/// assert_eq!( heap.peek(), Some( &7 ) ); // The largest value is at the top of the heap +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! heap +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _heap = collection_tools::BinaryHeap::with_capacity( _cap ); + $( + _heap.push( $key ); + )* + _heap + }}; +} + +/// Creates a `BinaryHeap` from a list of elements. +/// +/// The `into_heap` macro simplifies the creation of a `BinaryHeap` with initial elements. +/// Elements passed to the macro are automatically converted into the heap's element type +/// using `.into()`, allowing for the use of literals or values of different, but convertible types. +/// +/// Note: The `into_heap` macro utilizes the `.into()` method to convert each element into the target type +/// of the `BinaryHeap`. This means that the elements must be compatible with the `Into` trait for the +/// type `T` used in the `BinaryHeap`. Also, this means that sometimes you must specify the type of collection's items. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ BinaryHeap, into_heap }; +/// // BinaryHeap of i32 +/// let heap1 : BinaryHeap< i32 > = into_heap!( 3, 1, 4, 1, 5, 9 ); +/// +/// // BinaryHeap of String +/// let heap2 : BinaryHeap< String > = into_heap!{ "pear".to_string(), "apple", "banana" }; +/// +/// // With trailing comma +/// let heap3 : BinaryHeap< i32 > = into_heap!( 2, 7, 1, 8, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BinaryHeap`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `BinaryHeap`. +/// +/// # Returns +/// +/// Returns a `BinaryHeap` containing all the specified elements. The capacity of the heap is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{ BinaryHeap, into_heap }; +/// let heap : BinaryHeap< i32 > = into_heap!( 5, 3, 7, 1 ); +/// assert_eq!( heap.peek(), Some( &7 ) ); // The largest value is at the top of the heap +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into`: +/// +/// ```rust +/// # use collection_tools::{ BinaryHeap, into_heap }; +/// let chars : BinaryHeap< char > = into_heap!( 'a', 'b', 'c' ); +/// assert_eq!( chars.peek(), Some( &'c' ) ); // Characters are ordered by their ASCII value +/// ``` +/// +/// # Example +/// +/// Creating a `BinaryHeap` of `String` from string literals: +/// +/// ```rust +/// # use collection_tools::{ BinaryHeap, into_heap }; +/// let fruits : BinaryHeap< String > = into_heap!{ "cherry", "apple", "banana" }; +/// assert_eq!( fruits.peek(), Some( &"cherry".to_string() ) ); // The lexicographically largest value is at the top +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_heap +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _heap = collection_tools::BinaryHeap::with_capacity( _cap ); + $( + _heap.push( Into::into( $key ) ); + )* + _heap + }}; +} diff --git a/module/core/collection_tools/src/collections/hmap.rs b/module/core/collection_tools/src/collections/hmap.rs new file mode 100644 index 0000000000..47b7014aac --- /dev/null +++ b/module/core/collection_tools/src/collections/hmap.rs @@ -0,0 +1,172 @@ +/// Creates a `HashMap` from a list of key-value pairs. +/// +/// The `hmap` macro allows for convenient creation of a `HashMap` with initial elements. +/// +/// # Origin +/// +/// This collection can be reexported from different crates: +/// - from `std`, if `no_std` flag if off +/// - from `hashbrown`, if `use_alloc` flag if on +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ HashMap, hmap }; +/// // HashMap of &str to i32 +/// let map1 = hmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// +/// // HashMap of &str to &str +/// let map2 = hmap!{ "name" => "value", "type" => "example" }; +/// +/// // With trailing comma +/// let map3 = hmap!( 1 => "one", 2 => "two", 3 => "three", ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `HashMap`. +/// Each key and value can be of any type that implements the `Into` and `Into` traits, where `K` and `V` are the +/// types stored in the `HashMap` as keys and values, respectively. +/// +/// # Returns +/// +/// Returns a `HashMap` containing all the specified key-value pairs. The capacity of the map is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices and integer values: +/// +/// ```rust +/// # use collection_tools::{ HashMap, hmap }; +/// let map : HashMap< &str, i32 > = hmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// assert_eq!( map.get( "one" ), Some( &1 ) ); +/// assert_eq!( map.get( "two" ), Some( &2 ) ); +/// assert_eq!( map.get( "three" ), Some( &3 ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `HashMap` of integers to strings from literals: +/// +/// ```rust +/// # use collection_tools::{ HashMap, hmap }; +/// let pairs = hmap!( 1 => "apple", 2 => "banana" ); +/// assert_eq!( pairs.get( &1 ), Some( &"apple" ) ); +/// assert_eq!( pairs.get( &2 ), Some( &"banana" ) ); +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! hmap +{ + ( + $( $key : expr => $value : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _map = collection_tools::HashMap::with_capacity( _cap ); + $( + let _ = _map.insert( $key, $value ); + )* + _map + }}; +} + +/// Creates a `HashMap` from a list of key-value pairs. +/// +/// The `into_hmap` macro allows for convenient creation of a `HashMap` with initial elements. +/// Keys and values passed to the macro are automatically converted into the map's key and value types +/// using `.into()`, enabling the use of literals or values of different, but convertible types. +/// +/// Note: The `into_hmap` macro relies on the `.into()` method to convert each key and value into the target types +/// of the `HashMap`. This means that the keys and values must be compatible with the `Into` and `Into` traits +/// for the key type `K` and value type `V` used in the `HashMap`. Also, this means that sometimes you must specify the type of collection's items. +/// +/// # Origin +/// +/// This collection can be reexported from different crates: +/// - from `std`, if `no_std` flag if off +/// - from `hashbrown`, if `use_alloc` flag if on +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ HashMap, into_hmap }; +/// // HashMap of &str to i32 +/// let map1 : HashMap< &str, i32 > = into_hmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// +/// // HashMap of String to String +/// let map2 : HashMap< String, String > = into_hmap!{ "name".to_string() => "value".to_string(), "type" => "example" }; +/// +/// // With trailing comma +/// let map3 : HashMap< i32, &str > = into_hmap!( 1 => "one", 2 => "two", 3 => "three", ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `HashMap`. +/// Each key and value can be of any type that implements the `Into` and `Into` traits, where `K` and `V` are the +/// types stored in the `HashMap` as keys and values, respectively. +/// +/// # Returns +/// +/// Returns a `HashMap` containing all the specified key-value pairs. The capacity of the map is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices and integer values: +/// +/// ```rust +/// # use collection_tools::{ HashMap, into_hmap }; +/// let map : HashMap< &str, i32 > = into_hmap!( "one" => 1, "two" => 2, "three" => 3 ); +/// assert_eq!( map.get( "one" ), Some( &1 ) ); +/// assert_eq!( map.get( "two" ), Some( &2 ) ); +/// assert_eq!( map.get( "three" ), Some( &3 ) ); +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into` and `Into`: +/// +/// ```rust +/// # use collection_tools::{ HashMap, into_hmap }; +/// let items : HashMap< String, i32 > = into_hmap!( "pen" => 10, "book" => 45, "eraser" => 5 ); +/// assert_eq!( items.get( &"pen".to_string() ), Some(&10 ) ); +/// assert_eq!( items.get( &"book".to_string() ), Some(&45 ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `HashMap` of integers to strings from literals: +/// +/// ```rust +/// # use collection_tools::{ HashMap, into_hmap }; +/// let pairs : HashMap< i32, String > = into_hmap!( 1 => "apple", 2 => "banana" ); +/// assert_eq!( pairs.get( &1 ), Some( &"apple".to_string() ) ); +/// assert_eq!( pairs.get( &2 ), Some( &"banana".to_string() ) ); +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_hmap +{ + ( + $( $key : expr => $value : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _map = collection_tools::HashMap::with_capacity( _cap ); + $( + let _ = _map.insert( Into::into( $key ), Into::into( $value ) ); + )* + _map + }}; +} diff --git a/module/core/collection_tools/src/collections/hset.rs b/module/core/collection_tools/src/collections/hset.rs new file mode 100644 index 0000000000..b23c535010 --- /dev/null +++ b/module/core/collection_tools/src/collections/hset.rs @@ -0,0 +1,173 @@ +/// Creates a `HashSet` from a list of elements. +/// +/// The `hset` macro allows for convenient creation of a `HashSet` with initial elements. +/// +/// # Origin +/// +/// This collection can be reexported from different crates: +/// - from `std`, if `no_std` flag if off +/// - from `hashbrown`, if `use_alloc` flag if on +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ HashSet, hset }; +/// // HashSet of &str +/// let set1 = hset!( "a", "b", "c" ); +/// +/// // HashSet of &str +/// let set2 = hset!{ "a", "b", "c" }; +/// +/// // With trailing comma +/// let set3 = hset!( 1, 2, 3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `HashSet`. +/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the +/// type stored in the `HashSet`. +/// +/// # Returns +/// +/// Returns a `HashSet` containing all the specified elements. The capacity of the set is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices: +/// +/// ```rust +/// # use collection_tools::{ HashSet, hset }; +/// let set = hset!( "one", "two", "three" ); +/// assert!( set.contains( "one" ) ); +/// assert!( set.contains( "two" ) ); +/// assert!( set.contains( "three" ) ); +/// assert_eq!( set.len(), 3 ); +/// ``` +/// +/// # Example +/// +/// Creating a `HashSet` of `&str` from string literals: +/// +/// ```rust +/// # use collection_tools::{ HashSet, hset }; +/// let s = hset!{ "value" }; +/// assert_eq!( s.get( "value" ), Some( &"value" ) ); +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! hset +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _set = collection_tools::HashSet::with_capacity( _cap ); + $( + let _ = _set.insert( $key ); + )* + _set + }}; +} + +/// Creates a `HashSet` from a list of elements. +/// +/// The `into_hset` macro allows for convenient creation of a `HashSet` with initial elements. +/// Elements passed to the macro are automatically converted into the set's element type +/// using `.into()`, facilitating the use of literals or values of different, but convertible types. +/// +/// Note: The `into_hset` macro relies on the `.into()` method to convert each element into the target type +/// of the `HashSet`. This means that the elements must be compatible with the `Into< T >` trait for the +/// type `T` used in the `HashSet`. Also, this means that sometimes you must specify the type of collection's items. +/// +/// # Origin +/// +/// This collection can be reexported from different crates: +/// - from `std`, if `no_std` flag if off +/// - from `hashbrown`, if `use_alloc` flag if on +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ HashSet, into_hset }; +/// // HashSet of &str +/// let set1 : HashSet< &str > = into_hset!( "a", "b", "c" ); +/// +/// // HashSet of String +/// let set2 : HashSet< String > = into_hset!{ "a".to_string(), "b", "c" }; +/// +/// // With trailing comma +/// let set3 : HashSet< i32 > = into_hset!( 1, 2, 3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `HashSet`. +/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the +/// type stored in the `HashSet`. +/// +/// # Returns +/// +/// Returns a `HashSet` containing all the specified elements. The capacity of the set is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with string slices: +/// +/// ```rust +/// # use collection_tools::{ HashSet, into_hset }; +/// let set : HashSet< &str > = into_hset!( "one", "two", "three" ); +/// assert!( set.contains( "one" ) ); +/// assert!( set.contains( "two" ) ); +/// assert!( set.contains( "three" ) ); +/// assert_eq!( set.len(), 3 ); +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into< T >`: +/// +/// ```rust +/// # use collection_tools::{ HashSet, into_hset }; +/// let numbers : HashSet< i32 > = into_hset!( 1, 2, 3 ); +/// assert!( numbers.contains( &1 ) ); +/// assert!( numbers.contains( &2 ) ); +/// assert!( numbers.contains( &3 ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `HashSet` of `String` from string literals: +/// +/// ```rust +/// # use collection_tools::{ HashSet, into_hset }; +/// let s : HashSet< String > = into_hset!{ "value" }; +/// assert_eq!( s.get( "value" ), Some( &"value".to_string() ) ); +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_hset +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _set = collection_tools::HashSet::with_capacity( _cap ); + $( + let _ = _set.insert( Into::into( $key ) ); + )* + _set + }}; +} diff --git a/module/core/collection_tools/src/collections/list.rs b/module/core/collection_tools/src/collections/list.rs new file mode 100644 index 0000000000..e01c4a4217 --- /dev/null +++ b/module/core/collection_tools/src/collections/list.rs @@ -0,0 +1,169 @@ +/// Creates a `LinkedList` from a list of elements. +/// +/// The `list` macro facilitates the creation of a `LinkedList` with initial elements. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ LinkedList, list }; +/// // LinkedList of i32 +/// let lst1 = list!( 1, 2, 3, 4, 5 ); +/// +/// // LinkedList of &str +/// let lst2 = list!{ "hello", "world", "rust" }; +/// +/// // With trailing comma +/// let lst3 = list!( 1.1, 2.2, 3.3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `LinkedList`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `LinkedList`. +/// +/// # Returns +/// +/// Returns a `LinkedList` containing all the specified elements. The capacity of the list is +/// dynamically adjusted based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{ LinkedList, list }; +/// let lst = list!( 1, 2, 3 ); +/// assert_eq!( lst.front(), Some( &1 ) ); // The first element is 1 +/// assert_eq!( lst.back(), Some( &3 ) ); // The last element is 3 +/// ``` +/// +/// # Example +/// +/// Creating a `LinkedList` of `&str` from string literals: +/// +/// ```rust +/// # use collection_tools::{ LinkedList, list }; +/// let fruits = list!{ "apple", "banana", "cherry" }; +/// assert_eq!( fruits.front(), Some( &"apple" ) ); // The first element +/// assert_eq!( fruits.back(), Some( &"cherry" ) ); // The last element +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! list +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + // "The LinkedList allows pushing and popping elements at either end in constant time." + // So no `with_capacity` + let mut _lst = collection_tools::LinkedList::new(); + $( + _lst.push_back( $key ); + )* + _lst + }}; +} + +/// Creates a `LinkedList` from a list of elements. +/// +/// The `into_list` macro facilitates the creation of a `LinkedList` with initial elements. +/// Elements passed to the macro are automatically converted into the list's element type +/// using `.into()`, making it convenient to use literals or values of different, but convertible types. +/// +/// Note: The `into_list` macro leverages the `.into()` method to convert each element into the target type +/// of the `LinkedList`. Therefore, the elements must be compatible with the `Into` trait for the +/// type `T` used in the `LinkedList`. Also, this means that sometimes you must specify the type of collection's items. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ LinkedList, into_list }; +/// // LinkedList of i32 +/// let lst1 : LinkedList< i32 > = into_list!( 1, 2, 3, 4, 5 ); +/// +/// // LinkedList of String +/// let lst2 : LinkedList< String > = into_list!{ "hello".to_string(), "world", "rust" }; +/// +/// // With trailing comma +/// let lst3 : LinkedList< f64 > = into_list!( 1.1, 2.2, 3.3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `LinkedList`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `LinkedList`. +/// +/// # Returns +/// +/// Returns a `LinkedList` containing all the specified elements. The capacity of the list is +/// dynamically adjusted based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{ LinkedList, into_list }; +/// let lst: LinkedList< i32 > = into_list!( 1, 2, 3 ); +/// assert_eq!( lst.front(), Some( &1 ) ); // The first element is 1 +/// assert_eq!( lst.back(), Some( &3 ) ); // The last element is 3 +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into`: +/// +/// ```rust +/// # use collection_tools::{ LinkedList, into_list }; +/// let chars : LinkedList< String > = into_list!( "a", "b", "c" ); +/// assert!( chars.contains( &"a".to_string() ) ); +/// assert!( chars.contains( &"b".to_string() ) ); +/// assert!( chars.contains( &"c".to_string() ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `LinkedList` of `String` from string literals: +/// +/// ```rust +/// # use collection_tools::{ LinkedList, into_list }; +/// let fruits : LinkedList< String > = into_list!{ "apple", "banana", "cherry" }; +/// assert_eq!( fruits.front(), Some( &"apple".to_string() ) ); // The first element +/// assert_eq!( fruits.back(), Some( &"cherry".to_string() ) ); // The last element +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_list +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + // "The LinkedList allows pushing and popping elements at either end in constant time." + // So no `with_capacity` + let mut _lst = collection_tools::LinkedList::new(); + $( + _lst.push_back( Into::into( $key ) ); + )* + _lst + }}; +} diff --git a/module/core/collection_tools/src/collections/vec.rs b/module/core/collection_tools/src/collections/vec.rs new file mode 100644 index 0000000000..d9a5a4521c --- /dev/null +++ b/module/core/collection_tools/src/collections/vec.rs @@ -0,0 +1,169 @@ +/// Creates a `Vec` from a list of elements. +/// +/// The `vec` macro simplifies the creation of a `Vec` with initial elements. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{Vec, vec}; +/// // Vec of i32 +/// let vec1 = vec!( 1, 2, 3, 4, 5 ); +/// +/// // Vec of &str +/// let vec2 = vec!{ "hello", "world", "rust" }; +/// +/// // With trailing comma +/// let vec3 = vec!( 1.1, 2.2, 3.3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key : expr ),* $( , )?`: A comma-separated list of elements to insert into the `Vec`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `Vec`. +/// +/// # Returns +/// +/// Returns a `Vec` containing all the specified elements. The capacity of the vector is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{Vec, vec}; +/// let vec = vec!( 1, 2, 3 ); +/// assert_eq!( vec[ 0 ], 1 ); +/// assert_eq!( vec[ 1 ], 2 ); +/// assert_eq!( vec[ 2 ], 3 ); +/// ``` +/// +/// # Example +/// +/// Creating a `Vec` of `&str` from string literals: +/// +/// ```rust +/// # use collection_tools::{Vec, vec}; +/// let mixed = vec!{ "value", "another value" }; +/// assert_eq!( mixed[ 0 ], "value" ); +/// assert_eq!( mixed[ 1 ], "another value" ); +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! vec +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _vec = collection_tools::Vec::with_capacity( _cap ); + $( + _vec.push( $key ); + )* + _vec + }}; +} + +/// Creates a `Vec` from a list of elements. +/// +/// The `into_vec!` macro simplifies the creation of a `Vec` with initial elements. +/// Elements passed to the macro are automatically converted into the vector's element type +/// using `.into()`, making it convenient to use literals or values of different, but convertible types. +/// +/// Note: The `into_vec!` macro utilizes the `.into()` method to convert each element into the target type +/// of the `Vec`. Therefore, the elements must be compatible with the `Into` trait for the +/// type `T` used in the `Vec`. Also, this means that sometimes you must specify the type of collection's items. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{Vec, into_vec}; +/// // Vec of i32 +/// let vec1 : Vec< i32 > = into_vec!( 1, 2, 3, 4, 5 ); +/// +/// // Vec of String +/// let vec2 : Vec< String > = into_vec!{ "hello", "world", "rust" }; +/// +/// // With trailing comma +/// let vec3 : Vec< f64 > = into_vec!( 1.1, 2.2, 3.3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key : expr ),* $( , )?`: A comma-separated list of elements to insert into the `Vec`. +/// Each element can be of any type that implements the `Into` trait, where `T` is the +/// type stored in the `Vec`. +/// +/// # Returns +/// +/// Returns a `Vec` containing all the specified elements. The capacity of the vector is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{Vec, into_vec}; +/// let vec : Vec< i32 > = into_vec!( 1, 2, 3 ); +/// assert_eq!( vec[ 0 ], 1 ); +/// assert_eq!( vec[ 1 ], 2 ); +/// assert_eq!( vec[ 2 ], 3 ); +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into`: +/// +/// ```rust +/// # use collection_tools::{Vec, into_vec}; +/// let words : Vec< String > = into_vec!( "alpha", "beta", "gamma" ); +/// assert_eq!( words[ 0 ], "alpha" ); +/// assert_eq!( words[ 1 ], "beta" ); +/// assert_eq!( words[ 2 ], "gamma" ); +/// ``` +/// +/// # Example +/// +/// Creating a `Vec` of `String` from string literals and String objects: +/// +/// ```rust +/// # use collection_tools::{Vec, into_vec}; +/// let mixed : Vec< String > = into_vec!{ "value", "another value".to_string() }; +/// assert_eq!( mixed[ 0 ], "value" ); +/// assert_eq!( mixed[ 1 ], "another value" ); +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_vec +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _vec = collection_tools::Vec::with_capacity( _cap ); + $( + _vec.push( Into::into( $key ) ); + )* + _vec + }}; +} diff --git a/module/core/collection_tools/src/collections/vecd.rs b/module/core/collection_tools/src/collections/vecd.rs new file mode 100644 index 0000000000..eb9369ea6f --- /dev/null +++ b/module/core/collection_tools/src/collections/vecd.rs @@ -0,0 +1,173 @@ +/// Creates a `VecDeque` from a list of elements. +/// +/// The `vecd` macro allows for the convenient creation of a `VecDeque` with initial elements. +/// Elements passed to the macro are automatically converted into the deque's element type +/// using `.into()`, enabling the use of literals or values of different, but convertible types. +/// +/// Note: The `vecd` macro relies on the `.into()` method to convert each element into the target type +/// of the `VecDeque`. This means that the elements must be compatible with the `Into` trait for the +/// type `T` used in the `VecDeque`. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ VecDeque, vecd }; +/// // VecDeque of i32 +/// let vd1 = vecd!( 1, 2, 3, 4, 5 ); +/// +/// // VecDeque of String +/// let vd2 = vecd!{ "hello", "world", "rust" }; +/// +/// // With trailing comma +/// let vd3 = vecd!( 1.1, 2.2, 3.3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `VecDeque`. +/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the +/// type stored in the `VecDeque`. +/// +/// # Returns +/// +/// Returns a `VecDeque` containing all the specified elements. The capacity of the deque is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{ VecDeque, vecd }; +/// let vd : VecDeque< i32 > = vecd!( 1, 2, 3 ); +/// assert_eq!( vd.front(), Some( &1 ) ); // The first element is 1 +/// assert_eq!( vd.back(), Some( &3 ) ); // The last element is 3 +/// ``` +/// +/// # Example +/// +/// Creating a `VecDeque` of `&str` from string literals: +/// +/// ```rust +/// # use collection_tools::{ VecDeque, vecd }; +/// let fruits = vecd!{ "apple", "banana", "cherry" }; +/// assert_eq!( fruits.front(), Some( &"apple" ) ); // The first element +/// assert_eq!( fruits.back(), Some( &"cherry" ) ); // The last element +/// ``` +/// +#[ cfg( feature = "collection_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! vecd +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _vecd = collection_tools::VecDeque::with_capacity( _cap ); + $( + _vecd.push_back( $key ); + )* + _vecd + }}; +} + +/// Creates a `VecDeque` from a list of elements. +/// +/// The `into_vecd` macro allows for the convenient creation of a `VecDeque` with initial elements. +/// Elements passed to the macro are automatically converted into the deque's element type +/// using `.into()`, enabling the use of literals or values of different, but convertible types. +/// +/// Note: The `into_vecd` macro relies on the `.into()` method to convert each element into the target type +/// of the `VecDeque`. This means that the elements must be compatible with the `Into` trait for the +/// type `T` used in the `VecDeque`. +/// +/// # Origin +/// +/// This collection is reexported from `alloc`. +/// +/// # Syntax +/// +/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. +/// +/// ```rust +/// # use collection_tools::{ VecDeque, into_vecd }; +/// // VecDeque of i32 +/// let vd1 : VecDeque< i32 > = into_vecd!( 1, 2, 3, 4, 5 ); +/// +/// // VecDeque of String +/// let vd2 : VecDeque< String > = into_vecd!{ "hello".to_string(), "world", "rust" }; +/// +/// // With trailing comma +/// let vd3 : VecDeque< f64 > = into_vecd!( 1.1, 2.2, 3.3, ); +/// ``` +/// +/// # Parameters +/// +/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `VecDeque`. +/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the +/// type stored in the `VecDeque`. +/// +/// # Returns +/// +/// Returns a `VecDeque` containing all the specified elements. The capacity of the deque is +/// automatically determined based on the number of elements provided. +/// +/// # Example +/// +/// Basic usage with integers: +/// +/// ```rust +/// # use collection_tools::{ VecDeque, into_vecd }; +/// let vd : VecDeque< i32 > = into_vecd!( 1, 2, 3 ); +/// assert_eq!( vd.front(), Some( &1 ) ); // The first element is 1 +/// assert_eq!( vd.back(), Some( &3 ) ); // The last element is 3 +/// ``` +/// +/// # Example +/// +/// Using with different types that implement `Into< T >`: +/// +/// ```rust +/// # use collection_tools::{ VecDeque, into_vecd }; +/// let chars : VecDeque< char > = into_vecd!( 'a', 'b', 'c' ); +/// assert!( chars.contains( &'a' ) ); +/// assert!( chars.contains( &'b' ) ); +/// assert!( chars.contains( &'c' ) ); +/// ``` +/// +/// # Example +/// +/// Creating a `VecDeque` of `String` from string literals: +/// +/// ```rust +/// # use collection_tools::{ VecDeque, into_vecd }; +/// let fruits : VecDeque< String > = into_vecd!{ "apple", "banana", "cherry" }; +/// assert_eq!( fruits.front(), Some( &"apple".to_string() ) ); // The first element +/// assert_eq!( fruits.back(), Some( &"cherry".to_string() ) ); // The last element +/// ``` +/// +#[ cfg( feature = "collection_into_constructors" ) ] +#[ macro_export( local_inner_macros ) ] +macro_rules! into_vecd +{ + ( + $( $key : expr ),* $( , )? + ) + => + {{ + let _cap = count!( @count $( $key ),* ); + let mut _vecd = collection_tools::VecDeque::with_capacity( _cap ); + $( + _vecd.push_back( Into::into( $key ) ); + )* + _vecd + }}; +} diff --git a/module/core/collection_tools/src/constructors.rs b/module/core/collection_tools/src/constructors.rs deleted file mode 100644 index 1ea42a5e62..0000000000 --- a/module/core/collection_tools/src/constructors.rs +++ /dev/null @@ -1,582 +0,0 @@ -/// Creates a `BTreeMap` from a list of key-value pairs. -/// -/// The `bmap` macro facilitates the convenient creation of a `BTreeMap` with initial elements. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ BTreeMap, bmap }; -/// // BTreeMap of &str to i32 -/// let map1 = bmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// -/// // BTreeMap of &str to &str -/// let map2 = bmap!{ "name" => "value" }; -/// -/// // With trailing comma -/// let map3 = bmap!( 1 => "one", 2 => "two", 3 => "three", ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `BTreeMap`. -/// Each key and value can be of any type that implements the `Into< K >` and `Into< V >` traits, where `K` and `V` are the -/// types stored in the `BTreeMap` as keys and values, respectively. -/// -/// # Returns -/// -/// Returns a `BTreeMap` containing all the specified key-value pairs. The map's capacity is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices and integer values: -/// -/// ```rust -/// # use collection_tools::{ BTreeMap, bmap }; -/// let map = bmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// assert_eq!( map.get( "one" ), Some( &1 ) ); -/// assert_eq!( map.get( "two" ), Some( &2 ) ); -/// assert_eq!( map.get( "three" ), Some( &3 ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `BTreeMap` of integers to string slices from literals: -/// -/// ```rust -/// # use collection_tools::{ BTreeMap, bmap }; -/// let numbers = bmap!( 1 => "one", 2 => "two", 3 => "three" ); -/// assert_eq!( numbers.get( &1 ), Some( &"one" ) ); -/// assert_eq!( numbers.get( &2 ), Some( &"two" ) ); -/// assert_eq!( numbers.get( &3 ), Some( &"three" ) ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! bmap -{ - ( - $( $key : expr => $value : expr ),* $( , )? - ) - => - {{ - let mut _map = collection_tools::BTreeMap::new(); - $( - let _ = _map.insert( $key , $value ); - )* - _map - }}; -} - -/// Creates a `BTreeSet` from a list of elements. -/// -/// The `bset` macro allows for convenient creation of a `BTreeSet` with initial elements. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ BTreeSet, bset }; -/// // BTreeSet of &str -/// let set1 = bset!( "a", "b", "c" ); -/// -/// // With trailing comma -/// let set3 = bset!( 1, 2, 3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BTreeSet`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `BTreeSet`. -/// -/// # Returns -/// -/// Returns a `BTreeSet` containing all the specified elements. The capacity of the set is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices: -/// -/// ```rust -/// # use collection_tools::{ BTreeSet, bset }; -/// let set = bset!( "one", "two", "three" ); -/// assert!( set.contains( "one" ) ); -/// assert!( set.contains( "two" ) ); -/// assert!( set.contains( "three" ) ); -/// assert_eq!( set.len(), 3 ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! bset -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let mut _set = collection_tools::BTreeSet::new(); - $( - _set.insert( $key ); - )* - _set - }}; -} - -/// Creates a `BinaryHeap` from a list of elements. -/// -/// The `into_heap` macro simplifies the creation of a `BinaryHeap` with initial elements. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ BinaryHeap, heap }; -/// // BinaryHeap of i32 -/// let heap1 = heap!( 3, 1, 4, 1, 5, 9 ); -/// -/// // BinaryHeap of &str -/// let heap2 = heap!{ "pear", "apple", "banana" }; -/// -/// // With trailing comma -/// let heap3 = heap!( 2, 7, 1, 8, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BinaryHeap`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `BinaryHeap`. -/// -/// # Returns -/// -/// Returns a `BinaryHeap` containing all the specified elements. The capacity of the heap is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{ BinaryHeap, heap }; -/// let heap = heap!( 5, 3, 7, 1 ); -/// assert_eq!( heap.peek(), Some( &7 ) ); // The largest value is at the top of the heap -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! heap -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _heap = collection_tools::BinaryHeap::with_capacity( _cap ); - $( - _heap.push( $key ); - )* - _heap - }}; -} - -/// Creates a `HashMap` from a list of key-value pairs. -/// -/// The `hmap` macro allows for convenient creation of a `HashMap` with initial elements. -/// -/// # Origin -/// -/// This collection can be reexported from different crates: -/// - from `std`, if `no_std` flag if off -/// - from `hashbrown`, if `use_alloc` flag if on -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ HashMap, hmap }; -/// // HashMap of &str to i32 -/// let map1 = hmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// -/// // HashMap of &str to &str -/// let map2 = hmap!{ "name" => "value", "type" => "example" }; -/// -/// // With trailing comma -/// let map3 = hmap!( 1 => "one", 2 => "two", 3 => "three", ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `HashMap`. -/// Each key and value can be of any type that implements the `Into` and `Into` traits, where `K` and `V` are the -/// types stored in the `HashMap` as keys and values, respectively. -/// -/// # Returns -/// -/// Returns a `HashMap` containing all the specified key-value pairs. The capacity of the map is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices and integer values: -/// -/// ```rust -/// # use collection_tools::{ HashMap, hmap }; -/// let map : HashMap< &str, i32 > = hmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// assert_eq!( map.get( "one" ), Some( &1 ) ); -/// assert_eq!( map.get( "two" ), Some( &2 ) ); -/// assert_eq!( map.get( "three" ), Some( &3 ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `HashMap` of integers to strings from literals: -/// -/// ```rust -/// # use collection_tools::{ HashMap, hmap }; -/// let pairs = hmap!( 1 => "apple", 2 => "banana" ); -/// assert_eq!( pairs.get( &1 ), Some( &"apple" ) ); -/// assert_eq!( pairs.get( &2 ), Some( &"banana" ) ); -/// ``` -/// -#[macro_export(local_inner_macros)] -macro_rules! hmap -{ - ( - $( $key : expr => $value : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _map = collection_tools::HashMap::with_capacity( _cap ); - $( - let _ = _map.insert( $key, $value ); - )* - _map - }}; -} - -/// Creates a `HashSet` from a list of elements. -/// -/// The `hset` macro allows for convenient creation of a `HashSet` with initial elements. -/// -/// # Origin -/// -/// This collection can be reexported from different crates: -/// - from `std`, if `no_std` flag if off -/// - from `hashbrown`, if `use_alloc` flag if on -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ HashSet, hset }; -/// // HashSet of &str -/// let set1 = hset!( "a", "b", "c" ); -/// -/// // HashSet of &str -/// let set2 = hset!{ "a", "b", "c" }; -/// -/// // With trailing comma -/// let set3 = hset!( 1, 2, 3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `HashSet`. -/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the -/// type stored in the `HashSet`. -/// -/// # Returns -/// -/// Returns a `HashSet` containing all the specified elements. The capacity of the set is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices: -/// -/// ```rust -/// # use collection_tools::{ HashSet, hset }; -/// let set = hset!( "one", "two", "three" ); -/// assert!( set.contains( "one" ) ); -/// assert!( set.contains( "two" ) ); -/// assert!( set.contains( "three" ) ); -/// assert_eq!( set.len(), 3 ); -/// ``` -/// -/// # Example -/// -/// Creating a `HashSet` of `&str` from string literals: -/// -/// ```rust -/// # use collection_tools::{ HashSet, hset }; -/// let s = hset!{ "value" }; -/// assert_eq!( s.get( "value" ), Some( &"value" ) ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! hset -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _set = collection_tools::HashSet::with_capacity( _cap ); - $( - let _ = _set.insert( $key ); - )* - _set - }}; -} - -/// Creates a `LinkedList` from a list of elements. -/// -/// The `list` macro facilitates the creation of a `LinkedList` with initial elements. -/// -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ LinkedList, list }; -/// // LinkedList of i32 -/// let lst1 = list!( 1, 2, 3, 4, 5 ); -/// -/// // LinkedList of &str -/// let lst2 = list!{ "hello", "world", "rust" }; -/// -/// // With trailing comma -/// let lst3 = list!( 1.1, 2.2, 3.3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `LinkedList`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `LinkedList`. -/// -/// # Returns -/// -/// Returns a `LinkedList` containing all the specified elements. The capacity of the list is -/// dynamically adjusted based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{ LinkedList, list }; -/// let lst = list!( 1, 2, 3 ); -/// assert_eq!( lst.front(), Some( &1 ) ); // The first element is 1 -/// assert_eq!( lst.back(), Some( &3 ) ); // The last element is 3 -/// ``` -/// -/// # Example -/// -/// Creating a `LinkedList` of `&str` from string literals: -/// -/// ```rust -/// # use collection_tools::{ LinkedList, list }; -/// let fruits = list!{ "apple", "banana", "cherry" }; -/// assert_eq!( fruits.front(), Some( &"apple" ) ); // The first element -/// assert_eq!( fruits.back(), Some( &"cherry" ) ); // The last element -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! list -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - // "The LinkedList allows pushing and popping elements at either end in constant time." - // So no `with_capacity` - let mut _lst = collection_tools::LinkedList::new(); - $( - _lst.push_back( $key ); - )* - _lst - }}; -} - -/// Creates a `Vec` from a list of elements. -/// -/// The `vec` macro simplifies the creation of a `Vec` with initial elements. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{Vec, vec}; -/// // Vec of i32 -/// let vec1 = vec!( 1, 2, 3, 4, 5 ); -/// -/// // Vec of &str -/// let vec2 = vec!{ "hello", "world", "rust" }; -/// -/// // With trailing comma -/// let vec3 = vec!( 1.1, 2.2, 3.3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key : expr ),* $( , )?`: A comma-separated list of elements to insert into the `Vec`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `Vec`. -/// -/// # Returns -/// -/// Returns a `Vec` containing all the specified elements. The capacity of the vector is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{Vec, vec}; -/// let vec = vec!( 1, 2, 3 ); -/// assert_eq!( vec[ 0 ], 1 ); -/// assert_eq!( vec[ 1 ], 2 ); -/// assert_eq!( vec[ 2 ], 3 ); -/// ``` -/// -/// # Example -/// -/// Creating a `Vec` of `&str` from string literals: -/// -/// ```rust -/// # use collection_tools::{Vec, vec}; -/// let mixed = vec!{ "value", "another value" }; -/// assert_eq!( mixed[ 0 ], "value" ); -/// assert_eq!( mixed[ 1 ], "another value" ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! vec -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _vec = collection_tools::Vec::with_capacity( _cap ); - $( - _vec.push( $key ); - )* - _vec - }}; -} - -/// Creates a `VecDeque` from a list of elements. -/// -/// The `vecd` macro allows for the convenient creation of a `VecDeque` with initial elements. -/// Elements passed to the macro are automatically converted into the deque's element type -/// using `.into()`, enabling the use of literals or values of different, but convertible types. -/// -/// Note: The `vecd` macro relies on the `.into()` method to convert each element into the target type -/// of the `VecDeque`. This means that the elements must be compatible with the `Into` trait for the -/// type `T` used in the `VecDeque`. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ VecDeque, vecd }; -/// // VecDeque of i32 -/// let vd1 = vecd!( 1, 2, 3, 4, 5 ); -/// -/// // VecDeque of String -/// let vd2 = vecd!{ "hello", "world", "rust" }; -/// -/// // With trailing comma -/// let vd3 = vecd!( 1.1, 2.2, 3.3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `VecDeque`. -/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the -/// type stored in the `VecDeque`. -/// -/// # Returns -/// -/// Returns a `VecDeque` containing all the specified elements. The capacity of the deque is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{ VecDeque, vecd }; -/// let vd : VecDeque< i32 > = vecd!( 1, 2, 3 ); -/// assert_eq!( vd.front(), Some( &1 ) ); // The first element is 1 -/// assert_eq!( vd.back(), Some( &3 ) ); // The last element is 3 -/// ``` -/// -/// # Example -/// -/// Creating a `VecDeque` of `&str` from string literals: -/// -/// ```rust -/// # use collection_tools::{ VecDeque, vecd }; -/// let fruits = vecd!{ "apple", "banana", "cherry" }; -/// assert_eq!( fruits.front(), Some( &"apple" ) ); // The first element -/// assert_eq!( fruits.back(), Some( &"cherry" ) ); // The last element -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! vecd -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _vecd = collection_tools::VecDeque::with_capacity( _cap ); - $( - _vecd.push_back( $key ); - )* - _vecd - }}; -} diff --git a/module/core/collection_tools/src/into_constructors.rs b/module/core/collection_tools/src/into_constructors.rs deleted file mode 100644 index 59af857a21..0000000000 --- a/module/core/collection_tools/src/into_constructors.rs +++ /dev/null @@ -1,738 +0,0 @@ -/// Creates a `BTreeMap` from a list of key-value pairs. -/// -/// The `into_bmap` macro facilitates the convenient creation of a `BTreeMap` with initial elements. -/// Keys and values passed to the macro are automatically converted into the map's key and value types -/// using `.into()`, enabling the use of literals or values of different, but convertible types. -/// -/// Note: The `into_bmap` macro relies on the `.into()` method to convert each key and value into the target types -/// of the `BTreeMap`. This means that the keys and values must be compatible with the `Into< K >` and `Into< V >` traits -/// for the key type `K` and value type `V` used in the `BTreeMap`. Also, this means that sometimes you must specify the type of collection's items. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ BTreeMap, into_bmap }; -/// // BTreeMap of &str to i32 -/// let map1 : BTreeMap< &str, i32 > = into_bmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// -/// // BTreeMap of String to String -/// let map2 : BTreeMap< String, String > = into_bmap!{ "name" => "value" }; -/// -/// // With trailing comma -/// let map3 : BTreeMap< i32, &str > = into_bmap!( 1 => "one", 2 => "two", 3 => "three", ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `BTreeMap`. -/// Each key and value can be of any type that implements the `Into< K >` and `Into< V >` traits, where `K` and `V` are the -/// types stored in the `BTreeMap` as keys and values, respectively. -/// -/// # Returns -/// -/// Returns a `BTreeMap` containing all the specified key-value pairs. The map's capacity is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices and integer values: -/// -/// ```rust -/// # use collection_tools::{ BTreeMap, into_bmap }; -/// let map : BTreeMap< &str, i32 > = into_bmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// assert_eq!( map.get( "one" ), Some( &1 ) ); -/// assert_eq!( map.get( "two" ), Some( &2 ) ); -/// assert_eq!( map.get( "three" ), Some( &3 ) ); -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into< K >` and `Into< V >`: -/// -/// ```rust -/// # use collection_tools::{ BTreeMap, into_bmap }; -/// let months : BTreeMap< String, i32 > = into_bmap!( "January" => 1, "February" => 2, "March" => 3 ); -/// assert_eq!( months.get( &"January".to_string() ), Some( &1 ) ); -/// assert_eq!( months.get( &"February".to_string() ), Some( &2 ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `BTreeMap` of integers to strings from literals: -/// -/// ```rust -/// # use collection_tools::{ BTreeMap, into_bmap }; -/// let numbers : BTreeMap< i32, String > = into_bmap!( 1 => "one", 2 => "two", 3 => "three" ); -/// assert_eq!( numbers.get( &1 ), Some( &"one".to_string() ) ); -/// assert_eq!( numbers.get( &2 ), Some( &"two".to_string() ) ); -/// assert_eq!( numbers.get( &3 ), Some( &"three".to_string() ) ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! into_bmap -{ - ( - $( $key : expr => $value : expr ),* $( , )? - ) - => - {{ - let mut _map = collection_tools::BTreeMap::new(); - $( - let _ = _map.insert( Into::into( $key ), Into::into( $value ) ); - )* - _map - }}; -} - -/// Creates a `BTreeSet` from a list of elements. -/// -/// The `into_bset` macro allows for convenient creation of a `BTreeSet` with initial elements. -/// Elements passed to the macro are automatically converted into the set's element type -/// using `.into()`, facilitating the use of literals or values of different, but convertible types. -/// -/// Note: The `into_bset` macro relies on the `.into()` method to convert each element into the target type -/// of the `BTreeSet`. This means that the elements must be compatible with the `Into` trait for the -/// type `T` used in the `BTreeSet`. Also, this means that sometimes you must specify the type of collection's items. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ BTreeSet, into_bset }; -/// // BTreeSet of &str -/// let set1 : BTreeSet< &str > = into_bset!( "a", "b", "c" ); -/// -/// // BTreeSet of String -/// let set2 : BTreeSet< String > = into_bset!{ "a".to_string(), "b", "c" }; -/// -/// // With trailing comma -/// let set3 : BTreeSet< i32 > = into_bset!( 1, 2, 3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BTreeSet`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `BTreeSet`. -/// -/// # Returns -/// -/// Returns a `BTreeSet` containing all the specified elements. The capacity of the set is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices: -/// -/// ```rust -/// # use collection_tools::{ BTreeSet, into_bset }; -/// let set : BTreeSet< &str > = into_bset!( "one", "two", "three" ); -/// assert!( set.contains( "one" ) ); -/// assert!( set.contains( "two" ) ); -/// assert!( set.contains( "three" ) ); -/// assert_eq!( set.len(), 3 ); -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into`: -/// -/// ```rust -/// # use collection_tools::{ BTreeSet, into_bset }; -/// let numbers : BTreeSet< i32 > = into_bset!( 1, 2, 3 ); -/// assert!( numbers.contains( &1 ) ); -/// assert!( numbers.contains( &2 ) ); -/// assert!( numbers.contains( &3 ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `BTreeSet` of `String` from string literals: -/// -/// ```rust -/// # use collection_tools::{ BTreeSet, into_bset }; -/// let s : BTreeSet< String > = into_bset!{ "value" }; -/// assert!( s.contains( "value" ) ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! into_bset -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let mut _set = collection_tools::BTreeSet::new(); - $( - _set.insert( Into::into( $key ) ); - )* - _set - }}; -} - -/// Creates a `BinaryHeap` from a list of elements. -/// -/// The `into_heap` macro simplifies the creation of a `BinaryHeap` with initial elements. -/// Elements passed to the macro are automatically converted into the heap's element type -/// using `.into()`, allowing for the use of literals or values of different, but convertible types. -/// -/// Note: The `into_heap` macro utilizes the `.into()` method to convert each element into the target type -/// of the `BinaryHeap`. This means that the elements must be compatible with the `Into` trait for the -/// type `T` used in the `BinaryHeap`. Also, this means that sometimes you must specify the type of collection's items. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ BinaryHeap, into_heap }; -/// // BinaryHeap of i32 -/// let heap1 : BinaryHeap< i32 > = into_heap!( 3, 1, 4, 1, 5, 9 ); -/// -/// // BinaryHeap of String -/// let heap2 : BinaryHeap< String > = into_heap!{ "pear".to_string(), "apple", "banana" }; -/// -/// // With trailing comma -/// let heap3 : BinaryHeap< i32 > = into_heap!( 2, 7, 1, 8, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `BinaryHeap`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `BinaryHeap`. -/// -/// # Returns -/// -/// Returns a `BinaryHeap` containing all the specified elements. The capacity of the heap is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{ BinaryHeap, into_heap }; -/// let heap : BinaryHeap< i32 > = into_heap!( 5, 3, 7, 1 ); -/// assert_eq!( heap.peek(), Some( &7 ) ); // The largest value is at the top of the heap -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into`: -/// -/// ```rust -/// # use collection_tools::{ BinaryHeap, into_heap }; -/// let chars : BinaryHeap< char > = into_heap!( 'a', 'b', 'c' ); -/// assert_eq!( chars.peek(), Some( &'c' ) ); // Characters are ordered by their ASCII value -/// ``` -/// -/// # Example -/// -/// Creating a `BinaryHeap` of `String` from string literals: -/// -/// ```rust -/// # use collection_tools::{ BinaryHeap, into_heap }; -/// let fruits : BinaryHeap< String > = into_heap!{ "cherry", "apple", "banana" }; -/// assert_eq!( fruits.peek(), Some( &"cherry".to_string() ) ); // The lexicographically largest value is at the top -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! into_heap -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _heap = collection_tools::BinaryHeap::with_capacity( _cap ); - $( - _heap.push( Into::into( $key ) ); - )* - _heap - }}; -} - -/// Creates a `HashMap` from a list of key-value pairs. -/// -/// The `into_hmap` macro allows for convenient creation of a `HashMap` with initial elements. -/// Keys and values passed to the macro are automatically converted into the map's key and value types -/// using `.into()`, enabling the use of literals or values of different, but convertible types. -/// -/// Note: The `into_hmap` macro relies on the `.into()` method to convert each key and value into the target types -/// of the `HashMap`. This means that the keys and values must be compatible with the `Into` and `Into` traits -/// for the key type `K` and value type `V` used in the `HashMap`. Also, this means that sometimes you must specify the type of collection's items. -/// -/// # Origin -/// -/// This collection can be reexported from different crates: -/// - from `std`, if `no_std` flag if off -/// - from `hashbrown`, if `use_alloc` flag if on -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ HashMap, into_hmap }; -/// // HashMap of &str to i32 -/// let map1 : HashMap< &str, i32 > = into_hmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// -/// // HashMap of String to String -/// let map2 : HashMap< String, String > = into_hmap!{ "name".to_string() => "value".to_string(), "type" => "example" }; -/// -/// // With trailing comma -/// let map3 : HashMap< i32, &str > = into_hmap!( 1 => "one", 2 => "two", 3 => "three", ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr => $value:expr ),* $( , )?`: A comma-separated list of key-value pairs to insert into the `HashMap`. -/// Each key and value can be of any type that implements the `Into` and `Into` traits, where `K` and `V` are the -/// types stored in the `HashMap` as keys and values, respectively. -/// -/// # Returns -/// -/// Returns a `HashMap` containing all the specified key-value pairs. The capacity of the map is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices and integer values: -/// -/// ```rust -/// # use collection_tools::{ HashMap, into_hmap }; -/// let map : HashMap< &str, i32 > = into_hmap!( "one" => 1, "two" => 2, "three" => 3 ); -/// assert_eq!( map.get( "one" ), Some( &1 ) ); -/// assert_eq!( map.get( "two" ), Some( &2 ) ); -/// assert_eq!( map.get( "three" ), Some( &3 ) ); -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into` and `Into`: -/// -/// ```rust -/// # use collection_tools::{ HashMap, into_hmap }; -/// let items : HashMap< String, i32 > = into_hmap!( "pen" => 10, "book" => 45, "eraser" => 5 ); -/// assert_eq!( items.get( &"pen".to_string() ), Some(&10 ) ); -/// assert_eq!( items.get( &"book".to_string() ), Some(&45 ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `HashMap` of integers to strings from literals: -/// -/// ```rust -/// # use collection_tools::{ HashMap, into_hmap }; -/// let pairs : HashMap< i32, String > = into_hmap!( 1 => "apple", 2 => "banana" ); -/// assert_eq!( pairs.get( &1 ), Some( &"apple".to_string() ) ); -/// assert_eq!( pairs.get( &2 ), Some( &"banana".to_string() ) ); -/// ``` -/// -#[macro_export(local_inner_macros)] -macro_rules! into_hmap -{ - ( - $( $key : expr => $value : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _map = collection_tools::HashMap::with_capacity( _cap ); - $( - let _ = _map.insert( Into::into( $key ), Into::into( $value ) ); - )* - _map - }}; -} - -/// Creates a `HashSet` from a list of elements. -/// -/// The `into_hset` macro allows for convenient creation of a `HashSet` with initial elements. -/// Elements passed to the macro are automatically converted into the set's element type -/// using `.into()`, facilitating the use of literals or values of different, but convertible types. -/// -/// Note: The `into_hset` macro relies on the `.into()` method to convert each element into the target type -/// of the `HashSet`. This means that the elements must be compatible with the `Into< T >` trait for the -/// type `T` used in the `HashSet`. Also, this means that sometimes you must specify the type of collection's items. -/// -/// # Origin -/// -/// This collection can be reexported from different crates: -/// - from `std`, if `no_std` flag if off -/// - from `hashbrown`, if `use_alloc` flag if on -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ HashSet, into_hset }; -/// // HashSet of &str -/// let set1 : HashSet< &str > = into_hset!( "a", "b", "c" ); -/// -/// // HashSet of String -/// let set2 : HashSet< String > = into_hset!{ "a".to_string(), "b", "c" }; -/// -/// // With trailing comma -/// let set3 : HashSet< i32 > = into_hset!( 1, 2, 3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `HashSet`. -/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the -/// type stored in the `HashSet`. -/// -/// # Returns -/// -/// Returns a `HashSet` containing all the specified elements. The capacity of the set is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with string slices: -/// -/// ```rust -/// # use collection_tools::{ HashSet, into_hset }; -/// let set : HashSet< &str > = into_hset!( "one", "two", "three" ); -/// assert!( set.contains( "one" ) ); -/// assert!( set.contains( "two" ) ); -/// assert!( set.contains( "three" ) ); -/// assert_eq!( set.len(), 3 ); -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into< T >`: -/// -/// ```rust -/// # use collection_tools::{ HashSet, into_hset }; -/// let numbers : HashSet< i32 > = into_hset!( 1, 2, 3 ); -/// assert!( numbers.contains( &1 ) ); -/// assert!( numbers.contains( &2 ) ); -/// assert!( numbers.contains( &3 ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `HashSet` of `String` from string literals: -/// -/// ```rust -/// # use collection_tools::{ HashSet, into_hset }; -/// let s : HashSet< String > = into_hset!{ "value" }; -/// assert_eq!( s.get( "value" ), Some( &"value".to_string() ) ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! into_hset -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _set = collection_tools::HashSet::with_capacity( _cap ); - $( - let _ = _set.insert( Into::into( $key ) ); - )* - _set - }}; -} - -/// Creates a `LinkedList` from a list of elements. -/// -/// The `into_list` macro facilitates the creation of a `LinkedList` with initial elements. -/// Elements passed to the macro are automatically converted into the list's element type -/// using `.into()`, making it convenient to use literals or values of different, but convertible types. -/// -/// Note: The `into_list` macro leverages the `.into()` method to convert each element into the target type -/// of the `LinkedList`. Therefore, the elements must be compatible with the `Into` trait for the -/// type `T` used in the `LinkedList`. Also, this means that sometimes you must specify the type of collection's items. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ LinkedList, into_list }; -/// // LinkedList of i32 -/// let lst1 : LinkedList< i32 > = into_list!( 1, 2, 3, 4, 5 ); -/// -/// // LinkedList of String -/// let lst2 : LinkedList< String > = into_list!{ "hello".to_string(), "world", "rust" }; -/// -/// // With trailing comma -/// let lst3 : LinkedList< f64 > = into_list!( 1.1, 2.2, 3.3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `LinkedList`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `LinkedList`. -/// -/// # Returns -/// -/// Returns a `LinkedList` containing all the specified elements. The capacity of the list is -/// dynamically adjusted based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{ LinkedList, into_list }; -/// let lst: LinkedList< i32 > = into_list!( 1, 2, 3 ); -/// assert_eq!( lst.front(), Some( &1 ) ); // The first element is 1 -/// assert_eq!( lst.back(), Some( &3 ) ); // The last element is 3 -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into`: -/// -/// ```rust -/// # use collection_tools::{ LinkedList, into_list }; -/// let chars : LinkedList< String > = into_list!( "a", "b", "c" ); -/// assert!( chars.contains( &"a".to_string() ) ); -/// assert!( chars.contains( &"b".to_string() ) ); -/// assert!( chars.contains( &"c".to_string() ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `LinkedList` of `String` from string literals: -/// -/// ```rust -/// # use collection_tools::{ LinkedList, into_list }; -/// let fruits : LinkedList< String > = into_list!{ "apple", "banana", "cherry" }; -/// assert_eq!( fruits.front(), Some( &"apple".to_string() ) ); // The first element -/// assert_eq!( fruits.back(), Some( &"cherry".to_string() ) ); // The last element -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! into_list -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - // "The LinkedList allows pushing and popping elements at either end in constant time." - // So no `with_capacity` - let mut _lst = collection_tools::LinkedList::new(); - $( - _lst.push_back( Into::into( $key ) ); - )* - _lst - }}; -} - -/// Creates a `Vec` from a list of elements. -/// -/// The `into_vec!` macro simplifies the creation of a `Vec` with initial elements. -/// Elements passed to the macro are automatically converted into the vector's element type -/// using `.into()`, making it convenient to use literals or values of different, but convertible types. -/// -/// Note: The `into_vec!` macro utilizes the `.into()` method to convert each element into the target type -/// of the `Vec`. Therefore, the elements must be compatible with the `Into` trait for the -/// type `T` used in the `Vec`. Also, this means that sometimes you must specify the type of collection's items. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{Vec, into_vec}; -/// // Vec of i32 -/// let vec1 : Vec< i32 > = into_vec!( 1, 2, 3, 4, 5 ); -/// -/// // Vec of String -/// let vec2 : Vec< String > = into_vec!{ "hello", "world", "rust" }; -/// -/// // With trailing comma -/// let vec3 : Vec< f64 > = into_vec!( 1.1, 2.2, 3.3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key : expr ),* $( , )?`: A comma-separated list of elements to insert into the `Vec`. -/// Each element can be of any type that implements the `Into` trait, where `T` is the -/// type stored in the `Vec`. -/// -/// # Returns -/// -/// Returns a `Vec` containing all the specified elements. The capacity of the vector is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{Vec, into_vec}; -/// let vec : Vec< i32 > = into_vec!( 1, 2, 3 ); -/// assert_eq!( vec[ 0 ], 1 ); -/// assert_eq!( vec[ 1 ], 2 ); -/// assert_eq!( vec[ 2 ], 3 ); -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into`: -/// -/// ```rust -/// # use collection_tools::{Vec, into_vec}; -/// let words : Vec< String > = into_vec!( "alpha", "beta", "gamma" ); -/// assert_eq!( words[ 0 ], "alpha" ); -/// assert_eq!( words[ 1 ], "beta" ); -/// assert_eq!( words[ 2 ], "gamma" ); -/// ``` -/// -/// # Example -/// -/// Creating a `Vec` of `String` from string literals and String objects: -/// -/// ```rust -/// # use collection_tools::{Vec, into_vec}; -/// let mixed : Vec< String > = into_vec!{ "value", "another value".to_string() }; -/// assert_eq!( mixed[ 0 ], "value" ); -/// assert_eq!( mixed[ 1 ], "another value" ); -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! into_vec -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _vec = collection_tools::Vec::with_capacity( _cap ); - $( - _vec.push( Into::into( $key ) ); - )* - _vec - }}; -} - -/// Creates a `VecDeque` from a list of elements. -/// -/// The `into_vecd` macro allows for the convenient creation of a `VecDeque` with initial elements. -/// Elements passed to the macro are automatically converted into the deque's element type -/// using `.into()`, enabling the use of literals or values of different, but convertible types. -/// -/// Note: The `into_vecd` macro relies on the `.into()` method to convert each element into the target type -/// of the `VecDeque`. This means that the elements must be compatible with the `Into` trait for the -/// type `T` used in the `VecDeque`. -/// -/// # Origin -/// -/// This collection is reexported from `alloc`. -/// -/// # Syntax -/// -/// The macro can be called with a comma-separated list of elements. A trailing comma is optional. -/// -/// ```rust -/// # use collection_tools::{ VecDeque, into_vecd }; -/// // VecDeque of i32 -/// let vd1 : VecDeque< i32 > = into_vecd!( 1, 2, 3, 4, 5 ); -/// -/// // VecDeque of String -/// let vd2 : VecDeque< String > = into_vecd!{ "hello".to_string(), "world", "rust" }; -/// -/// // With trailing comma -/// let vd3 : VecDeque< f64 > = into_vecd!( 1.1, 2.2, 3.3, ); -/// ``` -/// -/// # Parameters -/// -/// - `$( $key:expr ),* $( , )?`: A comma-separated list of elements to insert into the `VecDeque`. -/// Each element can be of any type that implements the `Into< T >` trait, where `T` is the -/// type stored in the `VecDeque`. -/// -/// # Returns -/// -/// Returns a `VecDeque` containing all the specified elements. The capacity of the deque is -/// automatically determined based on the number of elements provided. -/// -/// # Example -/// -/// Basic usage with integers: -/// -/// ```rust -/// # use collection_tools::{ VecDeque, into_vecd }; -/// let vd : VecDeque< i32 > = into_vecd!( 1, 2, 3 ); -/// assert_eq!( vd.front(), Some( &1 ) ); // The first element is 1 -/// assert_eq!( vd.back(), Some( &3 ) ); // The last element is 3 -/// ``` -/// -/// # Example -/// -/// Using with different types that implement `Into< T >`: -/// -/// ```rust -/// # use collection_tools::{ VecDeque, into_vecd }; -/// let chars : VecDeque< char > = into_vecd!( 'a', 'b', 'c' ); -/// assert!( chars.contains( &'a' ) ); -/// assert!( chars.contains( &'b' ) ); -/// assert!( chars.contains( &'c' ) ); -/// ``` -/// -/// # Example -/// -/// Creating a `VecDeque` of `String` from string literals: -/// -/// ```rust -/// # use collection_tools::{ VecDeque, into_vecd }; -/// let fruits : VecDeque< String > = into_vecd!{ "apple", "banana", "cherry" }; -/// assert_eq!( fruits.front(), Some( &"apple".to_string() ) ); // The first element -/// assert_eq!( fruits.back(), Some( &"cherry".to_string() ) ); // The last element -/// ``` -/// -#[ macro_export( local_inner_macros ) ] -macro_rules! into_vecd -{ - ( - $( $key : expr ),* $( , )? - ) - => - {{ - let _cap = count!( @count $( $key ),* ); - let mut _vecd = collection_tools::VecDeque::with_capacity( _cap ); - $( - _vecd.push_back( Into::into( $key ) ); - )* - _vecd - }}; -} diff --git a/module/core/collection_tools/src/lib.rs b/module/core/collection_tools/src/lib.rs index 4447d1dd7b..7564144d2c 100644 --- a/module/core/collection_tools/src/lib.rs +++ b/module/core/collection_tools/src/lib.rs @@ -4,34 +4,13 @@ #![ doc( html_root_url = "https://docs.rs/collection_tools/latest/collection_tools/" ) ] #![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "Readme.md" ) ) ] -// qqq : make subdirectory for each container +// qqq : make subdirectory for each container -- done -// qqq : move out of lib.rs file -/// Not meant to be called directly. -#[ doc( hidden ) ] -#[ macro_export( local_inner_macros ) ] -macro_rules! count -{ - ( @single $( $x : tt )* ) => ( () ); - - ( - @count $( $rest : expr ),* - ) - => - ( - < [ () ] >::len( &[ $( count!( @single $rest ) ),* ] ) - ); -} - -/// Macros to construct the collections. -/// Basically a tweaked version of `literally` crate but using `alloc` / `hashbrown` instead of `std` -#[ cfg( all( feature = "enabled", feature = "collection_constructors" ) ) ] -pub mod constructors; +// qqq : move out of lib.rs file -- moved to `collections.rs` -/// Macros to construct the collections, using `.into()` under the hood. -/// Often requires explicitly specifying type to cast to. -#[ cfg( all( feature = "enabled", feature = "collection_into_constructors" ) ) ] -pub mod into_constructors; +/// Module containing all collection macros +#[ cfg( feature = "enabled" ) ] +pub mod collections; /// Namespace with dependencies. #[ cfg( feature = "enabled" ) ] @@ -57,15 +36,12 @@ pub mod protected #[ allow( unused_imports ) ] pub use super::orphan::*; - // #[ cfg( feature = "use_alloc" ) ] extern crate alloc; - // #[ cfg( feature = "use_alloc" ) ] #[ doc( inline ) ] #[ allow( unused_imports ) ] pub use alloc::vec::Vec; - // #[ cfg( feature = "use_alloc" ) ] #[ doc( inline ) ] #[ allow( unused_imports ) ] pub use alloc::collections::{ BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque }; @@ -74,7 +50,7 @@ pub mod protected #[ cfg( feature = "use_alloc" ) ] #[ doc( inline ) ] #[ allow( unused_imports ) ] - pub use hashbrown::{ HashMap, HashSet }; + pub use super::dependency::hashbrown::{ HashMap, HashSet }; #[ cfg( not( feature = "no_std" ) ) ] #[ doc( inline ) ] @@ -106,40 +82,30 @@ pub mod exposed pub mod prelude { - #[ cfg( feature = "collection_constructors" ) ] - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use super::constructors::*; - - #[ cfg( feature = "collection_into_constructors" ) ] - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use super::into_constructors::*; - -// qqq : for Antont : uncomment, make it working and cover by tests -// #[ cfg( feature = "prelude" ) ] -// #[ doc( inline ) ] -// #[ allow( unused_imports ) ] -// pub use std::collections:: -// { -// HashMap as Map, -// HashSet as Set, -// HashMap, -// HashSet, -// VecDeque, -// BTreeMap, -// BTreeSet, -// BinaryHeap, -// LinkedList, -// }; -// -// #[ cfg( feature = "prelude" ) ] -// #[ doc( inline ) ] -// #[ allow( unused_imports ) ] -// pub use std::vec:: -// { -// Vec, -// Vec as DynArray, -// }; + // qqq : for Anton : uncomment, make it working and cover by tests + // #[ cfg( feature = "prelude" ) ] + // #[ doc( inline ) ] + // #[ allow( unused_imports ) ] + // pub use crate:: + // { + // HashMap as Map, + // HashSet as Set, + // HashMap, + // HashSet, + // VecDeque, + // BTreeMap, + // BTreeSet, + // BinaryHeap, + // LinkedList, + // }; + + // #[ cfg( feature = "prelude" ) ] + // #[ doc( inline ) ] + // #[ allow( unused_imports ) ] + // pub use crate:: + // { + // Vec, + // Vec as DynArray, + // }; } diff --git a/module/core/collection_tools/src/vec.rs b/module/core/collection_tools/src/vec.rs deleted file mode 100644 index f4e6502089..0000000000 --- a/module/core/collection_tools/src/vec.rs +++ /dev/null @@ -1,2 +0,0 @@ - -pub use core::slice::Iter diff --git a/module/core/collection_tools/tests/inc/bmap.rs b/module/core/collection_tools/tests/inc/bmap.rs new file mode 100644 index 0000000000..86beb642c3 --- /dev/null +++ b/module/core/collection_tools/tests/inc/bmap.rs @@ -0,0 +1,52 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut map : the_module::BTreeMap< i32, i32 > = the_module::BTreeMap::new(); + map.insert( 1, 2 ); + let exp = 2; + let got = *map.get( &1 ).unwrap(); + assert_eq!( exp, got ); + +} + +#[ test ] +#[ cfg( feature = "collection_constructors" ) ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::BTreeMap< i32, i32 > = the_module::bmap!{}; + let exp = the_module::BTreeMap::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::bmap!{ 3 => 13, 4 => 1 }; + let mut exp = the_module::BTreeMap::new(); + exp.insert(3, 13); + exp.insert(4, 1); + assert_eq!( got, exp ); + +} + +#[ cfg( feature = "collection_into_constructors" ) ] +#[ test ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::BTreeMap< i32, i32 > = the_module::into_bmap!{}; + let exp = the_module::BTreeMap::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::into_bmap!{ 3 => 13, 4 => 1 }; + let mut exp = the_module::BTreeMap::new(); + exp.insert(3, 13); + exp.insert(4, 1); + assert_eq!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/bset.rs b/module/core/collection_tools/tests/inc/bset.rs new file mode 100644 index 0000000000..70b7e56144 --- /dev/null +++ b/module/core/collection_tools/tests/inc/bset.rs @@ -0,0 +1,51 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut map : the_module::BTreeSet< i32 > = the_module::BTreeSet::new(); + map.insert( 1 ); + assert_eq!( map.contains( &1 ), true ); + assert_eq!( map.contains( &2 ), false ); + +} + +#[ test ] +#[ cfg( feature = "collection_constructors" ) ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::BTreeSet< i32 > = the_module::bset!{}; + let exp = the_module::BTreeSet::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::bset!{ 3, 13 }; + let mut exp = the_module::BTreeSet::new(); + exp.insert(3); + exp.insert(13); + assert_eq!( got, exp ); + +} + +#[ test ] +#[ cfg( feature = "collection_into_constructors" ) ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::BTreeSet< i32 > = the_module::into_bset!{}; + let exp = the_module::BTreeSet::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::into_bset!{ 3, 13 }; + let mut exp = the_module::BTreeSet::new(); + exp.insert(3); + exp.insert(13); + assert_eq!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/constructors.rs b/module/core/collection_tools/tests/inc/constructors.rs deleted file mode 100644 index dda241a1a4..0000000000 --- a/module/core/collection_tools/tests/inc/constructors.rs +++ /dev/null @@ -1,171 +0,0 @@ -#[ allow( unused_imports ) ] -use super::*; - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn b_tree_map() -{ - - // test.case( "empty" ); - let got : the_module::BTreeMap< i32, i32 > = the_module::bmap!{}; - let exp = the_module::BTreeMap::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::bmap!{ 3 => 13, 4 => 1 }; - let mut exp = the_module::BTreeMap::new(); - exp.insert(3, 13); - exp.insert(4, 1); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn b_tree_set() -{ - - // test.case( "empty" ); - let got : the_module::BTreeSet< i32 > = the_module::bset!{}; - let exp = the_module::BTreeSet::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::bset!{ 3, 13 }; - let mut exp = the_module::BTreeSet::new(); - exp.insert(3); - exp.insert(13); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn binary_heap() -{ - - // test.case( "empty" ); - let got : the_module::BinaryHeap< i32 > = the_module::heap!{}; - let exp: the_module::BinaryHeap< i32 > = the_module::BinaryHeap::new(); - assert_eq!( got.into_vec(), exp.into_vec() ); - - // test.case( "multiple entry" ); - let got = the_module::heap!{ 3, 13 }; - let mut exp = the_module::BinaryHeap::new(); - exp.push(3); - exp.push(13); - assert_eq!( got.into_sorted_vec(), exp.into_sorted_vec() ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn hash_map() -{ - - // test.case( "empty" ); - let got : the_module::HashMap< i32, i32 > = the_module::hmap!{}; - let exp = the_module::HashMap::new(); - assert_eq!( got, exp ); - - - // test.case( "multiple entry" ); - let got = the_module::hmap!{ 3 => 13, 4 => 1 }; - let mut exp = the_module::HashMap::new(); - exp.insert( 3, 13 ); - exp.insert( 4, 1 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn hash_set() -{ - - // test.case( "empty" ); - let got : the_module::HashSet< i32 > = the_module::hset!{}; - let exp = the_module::HashSet::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::hset!{ 13, 11 }; - let mut exp = the_module::HashSet::new(); - exp.insert( 11 ); - exp.insert( 13 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn linked_list() -{ - - // test.case( "empty" ); - let got : the_module::LinkedList< i32 > = the_module::list!{}; - let exp = the_module::LinkedList::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::list!{ 13, 15 }; - let mut exp = the_module::LinkedList::new(); - exp.push_front( 15 ); - exp.push_front( 13 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn vec() -{ - - // test.case( "empty" ); - let got : the_module::Vec< i32 > = the_module::vec!{}; - let exp = the_module::Vec::< i32 >::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::vec!{ 3, 13 }; - let mut exp = the_module::Vec::new(); - exp.push( 3 ); - exp.push( 13 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn vec_deque() -{ - - // test.case( "empty" ); - let got : the_module::VecDeque< i32 > = the_module::vecd!{}; - let exp = the_module::VecDeque::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::vecd!{ 3, 13 }; - let mut exp = the_module::VecDeque::new(); - exp.push_front( 13 ); - exp.push_front( 3 ); - assert_eq!( got, exp ); - -} diff --git a/module/core/collection_tools/tests/inc/heap.rs b/module/core/collection_tools/tests/inc/heap.rs new file mode 100644 index 0000000000..7da1ff7265 --- /dev/null +++ b/module/core/collection_tools/tests/inc/heap.rs @@ -0,0 +1,52 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut map : the_module::BinaryHeap< i32 > = the_module::BinaryHeap::new(); + map.push( 1 ); + let exp = Some(1).as_ref(); + let got = map.peek(); + assert_eq!( exp, got ); + +} + +#[ test ] +#[ cfg( feature = "collection_constructors" ) ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::BinaryHeap< i32 > = the_module::heap!{}; + let exp: the_module::BinaryHeap< i32 > = the_module::BinaryHeap::new(); + assert_eq!( got.into_vec(), exp.into_vec() ); + + // test.case( "multiple entry" ); + let got = the_module::heap!{ 3, 13 }; + let mut exp = the_module::BinaryHeap::new(); + exp.push(3); + exp.push(13); + assert_eq!( got.into_sorted_vec(), exp.into_sorted_vec() ); + +} + +#[ test ] +#[ cfg( feature = "collection_into_constructors" ) ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::BinaryHeap< i32 > = the_module::into_heap!{}; + let exp = the_module::BinaryHeap::< i32 >::new(); + assert_eq!( got.into_vec(), exp.into_vec() ); + + // test.case( "multiple entry" ); + let got : the_module::BinaryHeap< i32 > = the_module::into_heap!{ 3, 13 }; + let mut exp = the_module::BinaryHeap::new(); + exp.push(3); + exp.push(13); + assert_eq!( got.into_sorted_vec(), exp.into_sorted_vec() ); + +} diff --git a/module/core/collection_tools/tests/inc/hmap.rs b/module/core/collection_tools/tests/inc/hmap.rs new file mode 100644 index 0000000000..0e6167ce1f --- /dev/null +++ b/module/core/collection_tools/tests/inc/hmap.rs @@ -0,0 +1,62 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut map1 : the_module::HashMap< i32, i32 > = the_module::HashMap::new(); + map1.insert( 1, 2 ); + let exp = 2; + let got = *map1.get( &1 ).unwrap(); + assert_eq!( exp, got ); + +// let mut map2 : the_module::Map< i32, i32 > = the_module::Map::new(); +// map2.insert( 1, 2 ); +// let exp = 2; +// let got = *map2.get( &1 ).unwrap(); +// assert_eq!( exp, got ); + +// assert_eq!( map1, map2 ); + +} + +#[ test ] +#[ cfg( feature = "collection_constructors" ) ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::HashMap< i32, i32 > = the_module::hmap!{}; + let exp = the_module::HashMap::new(); + assert_eq!( got, exp ); + + + // test.case( "multiple entry" ); + let got = the_module::hmap!{ 3 => 13, 4 => 1 }; + let mut exp = the_module::HashMap::new(); + exp.insert( 3, 13 ); + exp.insert( 4, 1 ); + assert_eq!( got, exp ); + +} + +#[ test ] +#[ cfg( feature = "collection_into_constructors" ) ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::HashMap< i32, i32 > = the_module::into_hmap!{}; + let exp = the_module::HashMap::new(); + assert_eq!( got, exp ); + + + // test.case( "multiple entry" ); + let got = the_module::into_hmap!{ 3 => 13, 4 => 1 }; + let mut exp = the_module::HashMap::new(); + exp.insert( 3, 13 ); + exp.insert( 4, 1 ); + assert_eq!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/hset.rs b/module/core/collection_tools/tests/inc/hset.rs new file mode 100644 index 0000000000..9241ef29d0 --- /dev/null +++ b/module/core/collection_tools/tests/inc/hset.rs @@ -0,0 +1,58 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut set1 : the_module::HashSet< i32 > = the_module::HashSet::new(); + set1.insert( 1 ); + assert_eq!( set1.contains( &1 ), true ); + assert_eq!( set1.contains( &2 ), false ); + +// let mut set2 : the_module::Set< i32 > = the_module::Set::new(); +// set2.insert( 1 ); +// assert_eq!( set2.contains( &1 ), true ); +// assert_eq!( set2.contains( &2 ), false ); + +// assert_eq!( set1, set2 ); + +} + +#[ test ] +#[ cfg( feature = "collection_constructors" ) ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::HashSet< i32 > = the_module::hset!{}; + let exp = the_module::HashSet::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::hset!{ 13, 11 }; + let mut exp = the_module::HashSet::new(); + exp.insert( 11 ); + exp.insert( 13 ); + assert_eq!( got, exp ); + +} + +#[ test ] +#[ cfg( feature = "collection_into_constructors" ) ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::HashSet< i32 > = the_module::into_hset!{}; + let exp = the_module::HashSet::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::into_hset!{ 13, 11 }; + let mut exp = the_module::HashSet::new(); + exp.insert( 11 ); + exp.insert( 13 ); + assert_eq!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/into_constructors.rs b/module/core/collection_tools/tests/inc/into_constructors.rs deleted file mode 100644 index 7423159092..0000000000 --- a/module/core/collection_tools/tests/inc/into_constructors.rs +++ /dev/null @@ -1,173 +0,0 @@ -// xxx : uncomment - -#[ allow( unused_imports ) ] -use super::*; - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn b_tree_map() -{ - - // test.case( "empty" ); - let got : the_module::BTreeMap< i32, i32 > = the_module::into_bmap!{}; - let exp = the_module::BTreeMap::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::into_bmap!{ 3 => 13, 4 => 1 }; - let mut exp = the_module::BTreeMap::new(); - exp.insert(3, 13); - exp.insert(4, 1); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn b_tree_set() -{ - - // test.case( "empty" ); - let got : the_module::BTreeSet< i32 > = the_module::into_bset!{}; - let exp = the_module::BTreeSet::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::into_bset!{ 3, 13 }; - let mut exp = the_module::BTreeSet::new(); - exp.insert(3); - exp.insert(13); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn binary_heap() -{ - - // test.case( "empty" ); - let got : the_module::BinaryHeap< i32 > = the_module::into_heap!{}; - let exp = the_module::BinaryHeap::< i32 >::new(); - assert_eq!( got.into_vec(), exp.into_vec() ); - - // test.case( "multiple entry" ); - let got : the_module::BinaryHeap< i32 > = the_module::into_heap!{ 3, 13 }; - let mut exp = the_module::BinaryHeap::new(); - exp.push(3); - exp.push(13); - assert_eq!( got.into_sorted_vec(), exp.into_sorted_vec() ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn hash_map() -{ - - // test.case( "empty" ); - let got : the_module::HashMap< i32, i32 > = the_module::into_hmap!{}; - let exp = the_module::HashMap::new(); - assert_eq!( got, exp ); - - - // test.case( "multiple entry" ); - let got = the_module::into_hmap!{ 3 => 13, 4 => 1 }; - let mut exp = the_module::HashMap::new(); - exp.insert( 3, 13 ); - exp.insert( 4, 1 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn hash_set() -{ - - // test.case( "empty" ); - let got : the_module::HashSet< i32 > = the_module::into_hset!{}; - let exp = the_module::HashSet::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::into_hset!{ 13, 11 }; - let mut exp = the_module::HashSet::new(); - exp.insert( 11 ); - exp.insert( 13 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn linked_list() -{ - - // test.case( "empty" ); - let got : the_module::LinkedList< i32 > = the_module::into_list!{}; - let exp = the_module::LinkedList::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got = the_module::into_list!{ 13, 15 }; - let mut exp = the_module::LinkedList::new(); - exp.push_front( 15 ); - exp.push_front( 13 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn vec() -{ - - // test.case( "empty" ); - let got : the_module::Vec< i32 > = the_module::into_vec!{}; - let exp = the_module::Vec::< i32 >::new(); - assert_eq!( got, exp ); - - // test.case( "multiple entry" ); - let got : the_module::Vec< i32 > = the_module::into_vec!{ 3, 13 }; - let mut exp = the_module::Vec::new(); - exp.push( 3 ); - exp.push( 13 ); - assert_eq!( got, exp ); - -} - -// - -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn vec_deque() -{ - - // test.case( "empty" ); - let got : the_module::VecDeque< i32 > = the_module::into_vecd!{}; - let exp = the_module::VecDeque::new(); - assert_eq!( got, exp ); - - // test.case( "single entry" ); - let got = the_module::into_vecd!{ 3, 13 }; - let mut exp = the_module::VecDeque::new(); - exp.push_front( 13 ); - exp.push_front( 3 ); - assert_eq!( got, exp ); - -} diff --git a/module/core/collection_tools/tests/inc/list.rs b/module/core/collection_tools/tests/inc/list.rs new file mode 100644 index 0000000000..956d1af847 --- /dev/null +++ b/module/core/collection_tools/tests/inc/list.rs @@ -0,0 +1,51 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut map : the_module::LinkedList< i32 > = the_module::LinkedList::new(); + map.push_back( 1 ); + assert_eq!( map.contains( &1 ), true ); + assert_eq!( map.contains( &2 ), false ); + +} + +#[ test ] +#[ cfg( feature = "collection_constructors" ) ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::LinkedList< i32 > = the_module::list!{}; + let exp = the_module::LinkedList::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::list!{ 13, 15 }; + let mut exp = the_module::LinkedList::new(); + exp.push_front( 15 ); + exp.push_front( 13 ); + assert_eq!( got, exp ); + +} + +#[ cfg( feature = "collection_into_constructors" ) ] +#[ test ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::LinkedList< i32 > = the_module::into_list!{}; + let exp = the_module::LinkedList::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::into_list!{ 13, 15 }; + let mut exp = the_module::LinkedList::new(); + exp.push_front( 15 ); + exp.push_front( 13 ); + assert_eq!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/mod.rs b/module/core/collection_tools/tests/inc/mod.rs index 00cc188bc4..aa87ee1867 100644 --- a/module/core/collection_tools/tests/inc/mod.rs +++ b/module/core/collection_tools/tests/inc/mod.rs @@ -1,16 +1,15 @@ -#[ allow( unused_imports ) ] use super::*; -#[ cfg( any( feature = "collection_into_constructors") ) ] -mod into_constructors; - -#[ cfg( any( feature = "collection_constructors" ) ) ] -mod constructors; - -#[ cfg( any( feature = "collection_std" ) ) ] -mod reexport; +mod bmap; +mod bset; +mod heap; +mod hmap; +mod hset; +mod list; +mod vec; +mod vecd; mod components; -// qqq : make subdirectory for each container -// qqq : don't put tests otsude of directory `inc` +// qqq : make subdirectory for each container -- done +// qqq : don't put tests otsude of directory `inc` -- done diff --git a/module/core/collection_tools/tests/inc/reexport.rs b/module/core/collection_tools/tests/inc/reexport.rs deleted file mode 100644 index 000c6bc3fd..0000000000 --- a/module/core/collection_tools/tests/inc/reexport.rs +++ /dev/null @@ -1,105 +0,0 @@ -use super::*; - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn b_tree_map() -{ - let mut map : the_module::BTreeMap< i32, i32 > = the_module::BTreeMap::new(); - map.insert( 1, 2 ); - let exp = 2; - let got = *map.get( &1 ).unwrap(); - assert_eq!( exp, got ); -} - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn b_tree_set() -{ - let mut map : the_module::BTreeSet< i32 > = the_module::BTreeSet::new(); - map.insert( 1 ); - assert_eq!( map.contains( &1 ), true ); - assert_eq!( map.contains( &2 ), false ); -} - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn binary_heap() -{ - let mut map : the_module::BinaryHeap< i32 > = the_module::BinaryHeap::new(); - map.push( 1 ); - let exp = Some(1).as_ref(); - let got = map.peek(); - assert_eq!( exp, got ); -} - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn hash_map() -{ - let mut map : the_module::HashMap< i32, i32 > = the_module::HashMap::new(); - map.insert( 1, 2 ); - let exp = 2; - let got = *map.get( &1 ).unwrap(); - assert_eq!( exp, got ); -} - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn hash_set() -{ - let mut map : the_module::HashSet< i32 > = the_module::HashSet::new(); - map.insert( 1 ); - assert_eq!( map.contains( &1 ), true ); - assert_eq!( map.contains( &2 ), false ); -} - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn linked_list() -{ - let mut map : the_module::LinkedList< i32 > = the_module::LinkedList::new(); - map.push_back( 1 ); - assert_eq!( map.contains( &1 ), true ); - assert_eq!( map.contains( &2 ), false ); -} - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn vec() -{ - - let mut map : the_module::Vec< i32 > = the_module::Vec::new(); - map.push( 1 ); - map.push( 2 ); - let got = map.first().unwrap().clone(); - assert_eq!( got, 1 ); - let got = map.last().unwrap().clone(); - assert_eq!( got, 2 ); - -} - -// - -#[ test ] -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -fn vec_deque() -{ - let mut map : the_module::VecDeque< i32 > = the_module::VecDeque::new(); - map.push_back( 1 ); - assert_eq!( map.contains( &1 ), true ); - assert_eq!( map.contains( &2 ), false ); -} diff --git a/module/core/collection_tools/tests/inc/vec.rs b/module/core/collection_tools/tests/inc/vec.rs new file mode 100644 index 0000000000..59d1896925 --- /dev/null +++ b/module/core/collection_tools/tests/inc/vec.rs @@ -0,0 +1,64 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut vec1 : the_module::Vec< i32 > = the_module::Vec::new(); + vec1.push( 1 ); + vec1.push( 2 ); + let got = vec1.first().unwrap().clone(); + assert_eq!( got, 1 ); + let got = vec1.last().unwrap().clone(); + assert_eq!( got, 2 ); + +// let mut vec2 : the_module::DynArray< i32 > = the_module::DynArray::new(); +// vec2.push( 1 ); +// vec2.push( 2 ); +// let got = vec2.first().unwrap().clone(); +// assert_eq!( got, 1 ); +// let got = vec2.last().unwrap().clone(); +// assert_eq!( got, 2 ); + +// assert_eq!( vec1, vec2 ); + +} + +#[ cfg( feature = "collection_constructors" ) ] +#[ test ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::Vec< i32 > = the_module::vec!{}; + let exp = the_module::Vec::< i32 >::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::vec!{ 3, 13 }; + let mut exp = the_module::Vec::new(); + exp.push( 3 ); + exp.push( 13 ); + assert_eq!( got, exp ); + +} + +#[ cfg( feature = "collection_into_constructors" ) ] +#[ test ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::Vec< i32 > = the_module::into_vec!{}; + let exp = the_module::Vec::< i32 >::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got : the_module::Vec< i32 > = the_module::into_vec!{ 3, 13 }; + let mut exp = the_module::Vec::new(); + exp.push( 3 ); + exp.push( 13 ); + assert_eq!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/vecd.rs b/module/core/collection_tools/tests/inc/vecd.rs new file mode 100644 index 0000000000..b64073f993 --- /dev/null +++ b/module/core/collection_tools/tests/inc/vecd.rs @@ -0,0 +1,51 @@ +use super::*; + +#[ cfg( not( feature = "no_std" ) ) ] +#[ test ] +fn reexport() +{ + + let mut map : the_module::VecDeque< i32 > = the_module::VecDeque::new(); + map.push_back( 1 ); + assert_eq!( map.contains( &1 ), true ); + assert_eq!( map.contains( &2 ), false ); + +} + +#[ cfg( feature = "collection_constructors" ) ] +#[ test ] +fn constructor() +{ + + // test.case( "empty" ); + let got : the_module::VecDeque< i32 > = the_module::vecd!{}; + let exp = the_module::VecDeque::new(); + assert_eq!( got, exp ); + + // test.case( "multiple entry" ); + let got = the_module::vecd!{ 3, 13 }; + let mut exp = the_module::VecDeque::new(); + exp.push_front( 13 ); + exp.push_front( 3 ); + assert_eq!( got, exp ); + +} + +#[ cfg( feature = "collection_into_constructors" ) ] +#[ test ] +fn into_constructor() +{ + + // test.case( "empty" ); + let got : the_module::VecDeque< i32 > = the_module::into_vecd!{}; + let exp = the_module::VecDeque::new(); + assert_eq!( got, exp ); + + // test.case( "single entry" ); + let got = the_module::into_vecd!{ 3, 13 }; + let mut exp = the_module::VecDeque::new(); + exp.push_front( 13 ); + exp.push_front( 3 ); + assert_eq!( got, exp ); + +} diff --git a/module/core/former/Cargo.toml b/module/core/former/Cargo.toml index 09489d9b38..15989207b1 100644 --- a/module/core/former/Cargo.toml +++ b/module/core/former/Cargo.toml @@ -59,7 +59,7 @@ derive_from_components = [ "derive_components", "former_meta/derive_from_compone [dependencies] former_meta = { workspace = true } -collection_tools = { workspace = true, features = [ "collection_std", "collection_constructors" ] } +collection_tools = { workspace = true, features = [ "collection_constructors" ] } [dev-dependencies]