19
19
/// assert_eq!(map.keys().next(), Some(&"a"));
20
20
/// ```
21
21
macro_rules! indexmap {
22
- ( @single $( $x: tt) * ) => ( ( ) ) ;
23
- ( @count $( $rest: expr) ,* ) => ( <[ ( ) ] >:: len( & [ $( $crate:: indexmap!( @single $rest) ) ,* ] ) ) ;
24
-
25
22
( $( $key: expr => $value: expr, ) +) => { $crate:: indexmap!( $( $key => $value) ,+) } ;
26
23
( $( $key: expr => $value: expr) ,* ) => {
27
24
{
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 ) ;
30
29
$(
31
- _map . insert( $key, $value) ;
30
+ map . insert( $key, $value) ;
32
31
) *
33
- _map
32
+ map
34
33
}
35
34
} ;
36
35
}
@@ -56,18 +55,17 @@ macro_rules! indexmap {
56
55
/// assert_eq!(set.iter().next(), Some(&"a"));
57
56
/// ```
58
57
macro_rules! indexset {
59
- ( @single $( $x: tt) * ) => ( ( ) ) ;
60
- ( @count $( $rest: expr) ,* ) => ( <[ ( ) ] >:: len( & [ $( $crate:: indexset!( @single $rest) ) ,* ] ) ) ;
61
-
62
58
( $( $value: expr, ) +) => { $crate:: indexset!( $( $value) ,+) } ;
63
59
( $( $value: expr) ,* ) => {
64
60
{
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 ) ;
67
65
$(
68
- _set . insert( $value) ;
66
+ set . insert( $value) ;
69
67
) *
70
- _set
68
+ set
71
69
}
72
70
} ;
73
71
}
0 commit comments