Skip to content

Commit e49cdbc

Browse files
committed
Remove internal patterns in indexmap! and indexset!
1 parent 2109261 commit e49cdbc

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/macros.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@
1919
/// assert_eq!(map.keys().next(), Some(&"a"));
2020
/// ```
2121
macro_rules! indexmap {
22-
(@single $($x:tt)*) => (());
23-
(@count $($rest:expr),*) => (<[()]>::len(&[$($crate::indexmap!(@single $rest)),*]));
24-
2522
($($key:expr => $value:expr,)+) => { $crate::indexmap!($($key => $value),+) };
2623
($($key:expr => $value:expr),*) => {
2724
{
28-
let _cap = $crate::indexmap!(@count $($key),*);
29-
let mut _map = $crate::IndexMap::with_capacity(_cap);
25+
// Note: `stringify!($key)` is just here to consume the repetition,
26+
// but we throw away that string literal during constant evaluation.
27+
const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
28+
let mut map = $crate::IndexMap::with_capacity(CAP);
3029
$(
31-
_map.insert($key, $value);
30+
map.insert($key, $value);
3231
)*
33-
_map
32+
map
3433
}
3534
};
3635
}
@@ -56,18 +55,17 @@ macro_rules! indexmap {
5655
/// assert_eq!(set.iter().next(), Some(&"a"));
5756
/// ```
5857
macro_rules! indexset {
59-
(@single $($x:tt)*) => (());
60-
(@count $($rest:expr),*) => (<[()]>::len(&[$($crate::indexset!(@single $rest)),*]));
61-
6258
($($value:expr,)+) => { $crate::indexset!($($value),+) };
6359
($($value:expr),*) => {
6460
{
65-
let _cap = $crate::indexset!(@count $($value),*);
66-
let mut _set = $crate::IndexSet::with_capacity(_cap);
61+
// Note: `stringify!($value)` is just here to consume the repetition,
62+
// but we throw away that string literal during constant evaluation.
63+
const CAP: usize = <[()]>::len(&[$({ stringify!($value); }),*]);
64+
let mut set = $crate::IndexSet::with_capacity(CAP);
6765
$(
68-
_set.insert($value);
66+
set.insert($value);
6967
)*
70-
_set
68+
set
7169
}
7270
};
7371
}

0 commit comments

Comments
 (0)