Skip to main content

collection_tools/collection/
btree_map.rs

1#[ allow( unused_imports, clippy ::wildcard_imports ) ]
2use super :: *;
3
4#[ doc( inline ) ]
5#[ allow( unused_imports ) ]
6#[ allow( clippy ::pub_use ) ]
7pub use alloc ::collections ::btree_map :: *;
8
9/// Creates a `BTreeMap` from a list of key-value pairs.
10///
11/// The `bmap` macro facilitates the convenient creation of a `BTreeMap` with initial elements.
12///
13/// # Origin
14///
15/// This collection is reexported from `alloc`.
16///
17/// # Syntax
18///
19/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional.
20///
21/// ```rust
22/// # use collection_tools :: { BTreeMap, bmap };
23/// // BTreeMap of &str to i32
24/// let map1 = bmap!( "one" => 1, "two" => 2, "three" => 3 );
25///
26/// // BTreeMap of &str to &str
27/// let map2 = bmap!{ "name" => "value" };
28///
29/// // With trailing comma
30/// let map3 = bmap!( 1 => "one", 2 => "two", 3 => "three", );
31/// ```
32///
33/// # Parameters
34///
35/// - `$( $key: expr => $value: expr ),* $( , )?` : A comma-separated list of key-value pairs to insert into the `BTreeMap`.
36///   Each key and value can be of any type that implements the `Into< K >` and `Into< V >` traits, where `K` and `V` are the
37///   types stored in the `BTreeMap` as keys and values, respectively.
38///
39/// # Returns
40///
41/// Returns a `BTreeMap` containing all the specified key-value pairs. The map's capacity is
42/// automatically determined based on the number of elements provided.
43///
44/// # Example
45///
46/// Basic usage with string slices and integer values :
47///
48/// ```rust
49/// # use collection_tools :: { BTreeMap, bmap };
50/// let map = bmap!( "one" => 1, "two" => 2, "three" => 3 );
51/// assert_eq!( map.get( "one" ), Some( &1 ) );
52/// assert_eq!( map.get( "two" ), Some( &2 ) );
53/// assert_eq!( map.get( "three" ), Some( &3 ) );
54/// ```
55///
56/// # Example
57///
58/// Creating a `BTreeMap` of integers to string slices from literals :
59///
60/// ```rust
61/// # use collection_tools :: { BTreeMap, bmap };
62/// let numbers = bmap!( 1 => "one", 2 => "two", 3 => "three" );
63/// assert_eq!( numbers.get( &1 ), Some( &"one" ) );
64/// assert_eq!( numbers.get( &2 ), Some( &"two" ) );
65/// assert_eq!( numbers.get( &3 ), Some( &"three" ) );
66/// ```
67///
68#[ cfg( feature = "collection_constructors" ) ]
69#[ macro_export( local_inner_macros ) ]
70macro_rules! bmap
71{
72  (
73  $( $key: expr => $value: expr ),* $( , )?
74 )
75  =>
76  {{
77  let mut _map = $crate ::collection ::BTreeMap ::new();
78  $(
79   let _ = _map.insert( $key, $value );
80 )*
81  _map
82 }};
83}
84
85/// Creates a `BTreeMap` from a list of key-value pairs.
86///
87/// The `into_bmap` macro facilitates the convenient creation of a `BTreeMap` with initial elements.
88/// Keys and values passed to the macro are automatically converted into the map's key and value types
89/// using `.into()`, enabling the use of literals or values of different, but convertible types.
90///
91/// Note: The `into_bmap` macro relies on the `.into()` method to convert each key and value into the target types
92/// of the `BTreeMap`. This means that the keys and values must be compatible with the `Into< K >` and `Into< V >` traits
93/// 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.
94///
95/// # Origin
96///
97/// This collection is reexported from `alloc`.
98///
99/// # Syntax
100///
101/// The macro can be called with a comma-separated list of key-value pairs. A trailing comma is optional.
102///
103/// ```rust
104/// # use collection_tools :: { BTreeMap, into_bmap };
105/// // BTreeMap of &str to i32
106/// let map1: BTreeMap< &str, i32 > = into_bmap!( "one" => 1, "two" => 2, "three" => 3 );
107///
108/// // BTreeMap of String to String
109/// let map2: BTreeMap< String, String > = into_bmap!{ "name" => "value" };
110///
111/// // With trailing comma
112/// let map3: BTreeMap< i32, &str > = into_bmap!( 1 => "one", 2 => "two", 3 => "three", );
113/// ```
114///
115/// # Parameters
116///
117/// - `$( $key: expr => $value: expr ),* $( , )?` : A comma-separated list of key-value pairs to insert into the `BTreeMap`.
118///   Each key and value can be of any type that implements the `Into< K >` and `Into< V >` traits, where `K` and `V` are the
119///   types stored in the `BTreeMap` as keys and values, respectively.
120///
121/// # Returns
122///
123/// Returns a `BTreeMap` containing all the specified key-value pairs. The map's capacity is
124/// automatically determined based on the number of elements provided.
125///
126/// # Example
127///
128/// Basic usage with string slices and integer values :
129///
130/// ```rust
131/// # use collection_tools :: { BTreeMap, into_bmap };
132/// let map: BTreeMap< &str, i32 > = into_bmap!( "one" => 1, "two" => 2, "three" => 3 );
133/// assert_eq!( map.get( "one" ), Some( &1 ) );
134/// assert_eq!( map.get( "two" ), Some( &2 ) );
135/// assert_eq!( map.get( "three" ), Some( &3 ) );
136/// ```
137///
138/// # Example
139///
140/// Using with different types that implement `Into< K >` and `Into< V >` :
141///
142/// ```rust
143/// # use collection_tools :: { BTreeMap, into_bmap };
144/// let months: BTreeMap< String, i32 > = into_bmap!( "January" => 1, "February" => 2, "March" => 3 );
145/// assert_eq!( months.get( &"January".to_string() ), Some( &1 ) );
146/// assert_eq!( months.get( &"February".to_string() ), Some( &2 ) );
147/// ```
148///
149/// # Example
150///
151/// Creating a `BTreeMap` of integers to strings from literals :
152///
153/// ```rust
154/// # use collection_tools :: { BTreeMap, into_bmap };
155/// let numbers: BTreeMap< i32, String > = into_bmap!( 1 => "one", 2 => "two", 3 => "three" );
156/// assert_eq!( numbers.get( &1 ), Some( &"one".to_string() ) );
157/// assert_eq!( numbers.get( &2 ), Some( &"two".to_string() ) );
158/// assert_eq!( numbers.get( &3 ), Some( &"three".to_string() ) );
159/// ```
160///
161#[ cfg( feature = "collection_into_constructors" ) ]
162#[ macro_export( local_inner_macros ) ]
163macro_rules! into_bmap
164{
165  (
166  $( $key: expr => $value: expr ),* $( , )?
167 )
168  =>
169  {{
170  let mut _map = $crate ::collection ::BTreeMap ::new();
171  $(
172   let _ = _map.insert( Into ::into( $key ), Into ::into( $value ) );
173 )*
174  _map
175 }};
176}