Skip to content

Commit cd7ad97

Browse files
committed
Auto merge of #151190 - estebank:const-default-hashmap, r=oli-obk
Add `const Default` impls for `HashSet` and `HashMap` with custom `Hasher` Follow up to #134628. Tracking issue #143894. r? @fmease cc @fee1-dead @oli-obk This doesn't allow for `const H: HashSet<()> = Default::default();`, but would allow for it to work with a custom hasher, which would enable the `Fx*` family to work.
2 parents fe98ddc + 5f58acb commit cd7ad97

7 files changed

Lines changed: 24 additions & 11 deletions

File tree

library/core/src/hash/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ impl<H> Clone for BuildHasherDefault<H> {
784784
}
785785

786786
#[stable(since = "1.7.0", feature = "build_hasher")]
787-
impl<H> Default for BuildHasherDefault<H> {
787+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
788+
impl<H> const Default for BuildHasherDefault<H> {
788789
fn default() -> BuildHasherDefault<H> {
789790
Self::new()
790791
}

library/core/src/hash/sip.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ impl SipHasher13 {
166166
/// Creates a new `SipHasher13` with the two initial keys set to 0.
167167
#[inline]
168168
#[unstable(feature = "hashmap_internals", issue = "none")]
169+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
169170
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
170-
pub fn new() -> SipHasher13 {
171+
pub const fn new() -> SipHasher13 {
171172
SipHasher13::new_with_keys(0, 0)
172173
}
173174

174175
/// Creates a `SipHasher13` that is keyed off the provided keys.
175176
#[inline]
176177
#[unstable(feature = "hashmap_internals", issue = "none")]
178+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
177179
#[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")]
178-
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
180+
pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
179181
SipHasher13 { hasher: Hasher::new_with_keys(key0, key1) }
180182
}
181183
}
@@ -338,7 +340,8 @@ impl<S: Sip> Clone for Hasher<S> {
338340
}
339341
}
340342

341-
impl<S: Sip> Default for Hasher<S> {
343+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
344+
impl<S: Sip> const Default for Hasher<S> {
342345
/// Creates a `Hasher<S>` with the two initial keys set to 0.
343346
#[inline]
344347
fn default() -> Hasher<S> {

library/std/src/collections/hash/map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,9 +1444,10 @@ where
14441444
}
14451445

14461446
#[stable(feature = "rust1", since = "1.0.0")]
1447-
impl<K, V, S> Default for HashMap<K, V, S>
1447+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
1448+
impl<K, V, S> const Default for HashMap<K, V, S>
14481449
where
1449-
S: Default,
1450+
S: [const] Default,
14501451
{
14511452
/// Creates an empty `HashMap<K, V, S>`, with the `Default` value for the hasher.
14521453
#[inline]

library/std/src/collections/hash/map/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,4 +1053,7 @@ fn const_with_hasher() {
10531053
assert_eq!(y.len(), 0);
10541054
y.insert((), ());
10551055
assert_eq!(y.len(), 1);
1056+
1057+
const Z: HashMap<(), (), BuildHasherDefault<DefaultHasher>> = Default::default();
1058+
assert_eq!(X, Z);
10561059
}

library/std/src/collections/hash/set.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,14 +1236,15 @@ where
12361236
}
12371237

12381238
#[stable(feature = "rust1", since = "1.0.0")]
1239-
impl<T, S> Default for HashSet<T, S>
1239+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
1240+
impl<T, S> const Default for HashSet<T, S>
12401241
where
1241-
S: Default,
1242+
S: [const] Default,
12421243
{
12431244
/// Creates an empty `HashSet<T, S>` with the `Default` value for the hasher.
12441245
#[inline]
12451246
fn default() -> HashSet<T, S> {
1246-
HashSet { base: Default::default() }
1247+
HashSet { base: base::HashSet::with_hasher(Default::default()) }
12471248
}
12481249
}
12491250

library/std/src/collections/hash/set/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,9 @@ fn from_array() {
504504
#[test]
505505
fn const_with_hasher() {
506506
const X: HashSet<(), ()> = HashSet::with_hasher(());
507+
const Y: HashSet<(), ()> = Default::default();
507508
assert_eq!(X.len(), 0);
509+
assert_eq!(Y.len(), 0);
508510
}
509511

510512
#[test]

library/std/src/hash/random.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ impl DefaultHasher {
105105
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
106106
#[inline]
107107
#[allow(deprecated)]
108+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
108109
#[must_use]
109-
pub fn new() -> DefaultHasher {
110+
pub const fn new() -> DefaultHasher {
110111
DefaultHasher(SipHasher13::new_with_keys(0, 0))
111112
}
112113
}
113114

114115
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
115-
impl Default for DefaultHasher {
116+
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
117+
impl const Default for DefaultHasher {
116118
/// Creates a new `DefaultHasher` using [`new`].
117119
/// See its documentation for more.
118120
///

0 commit comments

Comments
 (0)