Skip to content

Commit ff6c67a

Browse files
committed
refactor: let users specify Hasher instead of BuildHasher
1 parent c94b22b commit ff6c67a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/macros.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
///
55
/// ```
66
/// use indexmap::indexmap_with_hasher;
7-
/// use std::collections::hash_map::RandomState;
7+
/// use std::hash::DefaultHasher;
88
///
99
/// let map = indexmap_with_hasher!{
10-
/// RandomState;
10+
/// DefaultHasher;
1111
/// "a" => 1,
1212
/// "b" => 2,
1313
/// };
@@ -22,10 +22,11 @@
2222
macro_rules! indexmap_with_hasher {
2323
($S:ty; $($key:expr => $value:expr,)+) => { $crate::indexmap_with_hasher!($S; $($key => $value),+) };
2424
($S:ty; $($key:expr => $value:expr),*) => {{
25+
type BuildS = core::hash::BuildHasherDefault<$S>;
2526
const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
2627
#[allow(unused_mut)]
27-
// Specify your custom S (must implement Default) as the hasher:
28-
let mut map = $crate::IndexMap::<_, _, $S>::with_capacity_and_hasher(CAP, <$S>::default());
28+
// Specify your custom S (must implement Default + Hasher) as the hasher:
29+
let mut map = $crate::IndexMap::<_, _, BuildS>::with_capacity_and_hasher(CAP, <BuildS>::default());
2930
$(
3031
map.insert($key, $value);
3132
)*
@@ -76,10 +77,10 @@ macro_rules! indexmap {
7677
///
7778
/// ```
7879
/// use indexmap::indexset_with_hasher;
79-
/// use std::collections::hash_map::RandomState;
80+
/// use std::hash::DefaultHasher;
8081
///
8182
/// let set = indexset_with_hasher!{
82-
/// RandomState;
83+
/// DefaultHasher;
8384
/// "a",
8485
/// "b",
8586
/// };
@@ -94,10 +95,11 @@ macro_rules! indexmap {
9495
macro_rules! indexset_with_hasher {
9596
($S:ty; $($value:expr,)+) => { $crate::indexset_with_hasher!($S; $($value),+) };
9697
($S:ty; $($value:expr),*) => {{
98+
type BuildS = core::hash::BuildHasherDefault<$S>;
9799
const CAP: usize = <[()]>::len(&[$({ stringify!($value); }),*]);
98100
#[allow(unused_mut)]
99101
// Specify your custom S (must implement Default) as the hasher:
100-
let mut set = $crate::IndexSet::<_, $S>::with_capacity_and_hasher(CAP, <$S>::default());
102+
let mut set = $crate::IndexSet::<_, BuildS>::with_capacity_and_hasher(CAP, <BuildS>::default());
101103
$(
102104
set.insert($value);
103105
)*

0 commit comments

Comments
 (0)