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