map_macro/hashbrown.rs
1//! Macros for initializing [`hashbrown`] maps and sets.
2//!
3//! # Example
4//!
5//! ```
6//! use map_macro::hashbrown::hash_map;
7//!
8//! let hello = hash_map! {
9//! "en" => "Hello",
10//! "de" => "Hallo",
11//! "fr" => "Bonjour",
12//! "es" => "Hola",
13//! "cat" => "Hola",
14//! "🌍" => "👋",
15//! };
16//! ```
17//!
18//! # Supported Versions of `hashbrown`
19//!
20//! As of writing this, up to the current `hashbrown` version `0.14` **all**
21//! versions of `hashbrown` are supported.
22//! So you can use the macros from this module with any version of `hashbrown`
23//! to date.
24//! Though highly likely, compatibility can't be guaranteed with future versions
25//! of `hashbrown` that break [SemVer compatibility](https://semver.org/#semantic-versioning-specification-semver)
26//! with `0.14`.
27//! If `hashbrown` were to remove the [`FromIterator`](::core::iter::FromIterator)
28//! implementations of `HashMap` and `HashSet` in a release that is
29//! incompatible with `0.14` (i.e. `0.15` or `1.0`) compatibility with the
30//! macros from this module would break for that new version.
31//!
32//! **Note:** to be compatible with all versions of `hashbrown` at once, this
33//! crate doesn't re-export `hashbrown`.
34//! That means that (I) you need to specify it as a dependency yourself and
35//! (II) you can't rename it or the macros from this module won't be able to
36//! import the needed types, resulting in a compile-time error.
37//!
38
39/// Macro for creating a [`HashMap`](::hashbrown::HashMap).
40///
41/// Syntactic sugar for [`HashMap::from_iter`](::hashbrown::HashMap#method.from_iter).
42///
43/// # Examples
44///
45/// ```rust
46/// use map_macro::hashbrown::hash_map;
47///
48/// let goodbye = hash_map! {
49/// "en" => "Goodbye",
50/// "de" => "Auf Wiedersehen",
51/// "fr" => "Au revoir",
52/// "es" => "Adios",
53/// "cat" => "Adéu",
54/// };
55/// ```
56///
57#[doc(hidden)]
58#[macro_export]
59macro_rules! __hb_hash_map {
60 {$($k: expr => $v: expr),* $(,)?} => {
61 <::hashbrown::HashMap::<_, _> as ::core::iter::FromIterator<_>>::from_iter([$(($k, $v),)*])
62 };
63}
64
65/// Explicitly typed equivalent of [`hash_map!`](self::hash_map).
66///
67/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
68///
69/// # Examples
70///
71/// ```rust
72/// use std::fmt::Debug;
73///
74/// use hashbrown::HashMap;
75///
76/// use map_macro::hashbrown::hash_map_e;
77///
78/// let goodbye: HashMap<&str, &dyn Debug> = hash_map_e! {
79/// "en" => &"Goodbye",
80/// "de" => &"Auf Wiedersehen",
81/// "fr" => &"Au revoir",
82/// "es" => &"Adios",
83/// "cat" => &"Adéu",
84/// };
85///
86/// println!("{:?}", goodbye);
87/// ```
88///
89#[doc(hidden)]
90#[macro_export]
91macro_rules! __hb_hash_map_e {
92 {$($k: expr => $v: expr),* $(,)?} => {
93 <::hashbrown::HashMap::<_, _> as ::core::iter::FromIterator<_>>::from_iter([$(($k as _, $v as _),)*])
94 };
95}
96
97/// Macro for creating a [`HashSet`](::hashbrown::HashSet).
98///
99/// Syntactic sugar for [`HashSet::from_iter`](::hashbrown::HashSet#method.from_iter).
100///
101/// # Examples
102///
103/// ```rust
104/// use map_macro::hashbrown::hash_set;
105///
106/// let x = hash_set! { 1, 2, 3, 3, 4 };
107///
108/// assert_eq!(x.len(), 4);
109/// ```
110///
111#[doc(hidden)]
112#[macro_export]
113macro_rules! __hb_hash_set {
114 {$($v: expr),* $(,)?} => {
115 <::hashbrown::HashSet::<_> as ::core::iter::FromIterator<_>>::from_iter([$($v,)*])
116 };
117}
118
119/// Explicitly typed equivalent of [`hash_set!`](self::hash_set).
120///
121/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
122///
123/// # Examples
124///
125/// ```rust
126/// use hashbrown::HashSet;
127///
128/// use map_macro::hashbrown::hash_set_e;
129///
130/// enum Foo { A, B, C, D }
131///
132/// let x: HashSet<u8> = hash_set_e! { Foo::A, Foo::B, Foo::C, Foo::C, Foo::D };
133///
134/// assert_eq!(x.len(), 4);
135/// ```
136///
137#[doc(hidden)]
138#[macro_export]
139macro_rules! __hb_hash_set_e {
140 {$($v: expr),* $(,)?} => {
141 <::hashbrown::HashSet::<_> as ::core::iter::FromIterator<_>>::from_iter([$($v as _,)*])
142 };
143}
144
145#[doc(inline)]
146pub use __hb_hash_map as hash_map;
147
148#[doc(inline)]
149pub use __hb_hash_map_e as hash_map_e;
150
151#[doc(inline)]
152pub use __hb_hash_set as hash_set;
153
154#[doc(inline)]
155pub use __hb_hash_set_e as hash_set_e;