Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d515da6

Browse files
committedNov 2, 2024
const_with_hasher test: actually construct a usable HashMap
1 parent ecd55b1 commit d515da6

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
 

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::Entry::{Occupied, Vacant};
55
use super::HashMap;
66
use crate::assert_matches::assert_matches;
77
use crate::cell::RefCell;
8-
use crate::hash::RandomState;
8+
use crate::hash::{BuildHasher, BuildHasherDefault, DefaultHasher, RandomState};
99
use crate::test_helpers::test_rng;
1010

1111
// https://github.com/rust-lang/rust/issues/62301
@@ -1124,6 +1124,26 @@ fn from_array() {
11241124

11251125
#[test]
11261126
fn const_with_hasher() {
1127-
const X: HashMap<(), (), ()> = HashMap::with_hasher(());
1128-
assert_eq!(X.len(), 0);
1127+
const X: HashMap<(), (), BuildHasherDefault<DefaultHasher>> =
1128+
HashMap::with_hasher(BuildHasherDefault::new());
1129+
let mut x = X;
1130+
assert_eq!(x.len(), 0);
1131+
x.insert((), ());
1132+
assert_eq!(x.len(), 1);
1133+
1134+
// It *is* possible to do this without using the `BuildHasherDefault` type.
1135+
struct MyBuildDefaultHasher;
1136+
impl BuildHasher for MyBuildDefaultHasher {
1137+
type Hasher = DefaultHasher;
1138+
1139+
fn build_hasher(&self) -> Self::Hasher {
1140+
DefaultHasher::new()
1141+
}
1142+
}
1143+
1144+
const Y: HashMap<(), (), MyBuildDefaultHasher> = HashMap::with_hasher(MyBuildDefaultHasher);
1145+
let mut y = Y;
1146+
assert_eq!(y.len(), 0);
1147+
y.insert((), ());
1148+
assert_eq!(y.len(), 1);
11291149
}

‎std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@
328328
// Library features (core):
329329
// tidy-alphabetical-start
330330
#![feature(array_chunks)]
331+
#![feature(build_hasher_default_const_new)]
331332
#![feature(c_str_module)]
332333
#![feature(char_internals)]
333334
#![feature(clone_to_uninit)]

0 commit comments

Comments
 (0)
Please sign in to comment.