Debian Codegraph @

src/macros.rs

raw symbols SCIP
1 /// Create an [`OrderMap`][crate::OrderMap] from a list of key-value pairs
2 /// and a [`BuildHasherDefault`][core::hash::BuildHasherDefault]-wrapped custom hasher.
3 ///
4 /// ## Example
5 ///
6 /// ```
7 /// use ordermap::ordermap_with_default;
8 /// use fnv::FnvHasher;
9 ///
10 /// let map = ordermap_with_default!{
11 /// FnvHasher;
12 /// "a" => 1,
13 /// "b" => 2,
14 /// };
15 /// assert_eq!(map["a"], 1);
16 /// assert_eq!(map["b"], 2);
17 /// assert_eq!(map.get("c"), None);
18 ///
19 /// // "a" is the first key
20 /// assert_eq!(map.keys().next(), Some(&"a"));
21 /// ```
22 #[macro_export]
23 macro_rules! ordermap_with_default {
24 ($H:ty; $($key:expr => $value:expr,)+) => { $crate::ordermap_with_default!($H; $($key => $value),+) };
25 ($H:ty; $($key:expr => $value:expr),*) => {{
26 let builder = ::core::hash::BuildHasherDefault::<$H>::new();
27 const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
28 #[allow(unused_mut)]
29 // Specify your custom `H` (must implement Default + Hasher) as the hasher:
30 let mut map = $crate::OrderMap::with_capacity_and_hasher(CAP, builder);
31 $(
32 map.insert($key, $value);
33 )*
34 map
35 }};
36 }
37
38 #[cfg(feature = "std")]
39 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
40 #[macro_export]
41 /// Create an [`OrderMap`][crate::OrderMap] from a list of key-value pairs
42 ///
43 /// ## Example
44 ///
45 /// ```
46 /// use ordermap::ordermap;
47 ///
48 /// let map = ordermap!{
49 /// "a" => 1,
50 /// "b" => 2,
51 /// };
52 /// assert_eq!(map["a"], 1);
53 /// assert_eq!(map["b"], 2);
54 /// assert_eq!(map.get("c"), None);
55 ///
56 /// // "a" is the first key
57 /// assert_eq!(map.keys().next(), Some(&"a"));
58 /// ```
59 macro_rules! ordermap {
60 ($($key:expr => $value:expr,)+) => { $crate::ordermap!($($key => $value),+) };
61 ($($key:expr => $value:expr),*) => {
62 {
63 // Note: `stringify!($key)` is just here to consume the repetition,
64 // but we throw away that string literal during constant evaluation.
65 const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
66 let mut map = $crate::OrderMap::with_capacity(CAP);
67 $(
68 map.insert($key, $value);
69 )*
70 map
71 }
72 };
73 }
74
75 /// Create an [`OrderSet`][crate::OrderSet] from a list of values
76 /// and a [`BuildHasherDefault`][core::hash::BuildHasherDefault]-wrapped custom hasher.
77 ///
78 /// ## Example
79 ///
80 /// ```
81 /// use ordermap::orderset_with_default;
82 /// use fnv::FnvHasher;
83 ///
84 /// let set = orderset_with_default!{
85 /// FnvHasher;
86 /// "a",
87 /// "b",
88 /// };
89 /// assert!(set.contains("a"));
90 /// assert!(set.contains("b"));
91 /// assert!(!set.contains("c"));
92 ///
93 /// // "a" is the first value
94 /// assert_eq!(set.iter().next(), Some(&"a"));
95 /// ```
96 #[macro_export]
97 macro_rules! orderset_with_default {
98 ($H:ty; $($value:expr,)+) => { $crate::orderset_with_default!($H; $($value),+) };
99 ($H:ty; $($value:expr),*) => {{
100 let builder = ::core::hash::BuildHasherDefault::<$H>::default();
101 const CAP: usize = <[()]>::len(&[$({ stringify!($value); }),*]);
102 #[allow(unused_mut)]
103 // Specify your custom `H` (must implement Default + Hash) as the hasher:
104 let mut set = $crate::OrderSet::with_capacity_and_hasher(CAP, builder);
105 $(
106 set.insert($value);
107 )*
108 set
109 }};
110 }
111
112 #[cfg(feature = "std")]
113 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
114 #[macro_export]
115 /// Create an [`OrderSet`][crate::OrderSet] from a list of values
116 ///
117 /// ## Example
118 ///
119 /// ```
120 /// use ordermap::orderset;
121 ///
122 /// let set = orderset!{
123 /// "a",
124 /// "b",
125 /// };
126 /// assert!(set.contains("a"));
127 /// assert!(set.contains("b"));
128 /// assert!(!set.contains("c"));
129 ///
130 /// // "a" is the first value
131 /// assert_eq!(set.iter().next(), Some(&"a"));
132 /// ```
133 macro_rules! orderset {
134 ($($value:expr,)+) => { $crate::orderset!($($value),+) };
135 ($($value:expr),*) => {
136 {
137 // Note: `stringify!($value)` is just here to consume the repetition,
138 // but we throw away that string literal during constant evaluation.
139 const CAP: usize = <[()]>::len(&[$({ stringify!($value); }),*]);
140 let mut set = $crate::OrderSet::with_capacity(CAP);
141 $(
142 set.insert($value);
143 )*
144 set
145 }
146 };
147 }