Skip to content

Commit

Permalink
const_with_hasher test: actually construct a usable HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 2, 2024
1 parent ecd55b1 commit d515da6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
26 changes: 23 additions & 3 deletions std/src/collections/hash/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Entry::{Occupied, Vacant};
use super::HashMap;
use crate::assert_matches::assert_matches;
use crate::cell::RefCell;
use crate::hash::RandomState;
use crate::hash::{BuildHasher, BuildHasherDefault, DefaultHasher, RandomState};
use crate::test_helpers::test_rng;

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

#[test]
fn const_with_hasher() {
const X: HashMap<(), (), ()> = HashMap::with_hasher(());
assert_eq!(X.len(), 0);
const X: HashMap<(), (), BuildHasherDefault<DefaultHasher>> =
HashMap::with_hasher(BuildHasherDefault::new());
let mut x = X;
assert_eq!(x.len(), 0);
x.insert((), ());
assert_eq!(x.len(), 1);

// It *is* possible to do this without using the `BuildHasherDefault` type.
struct MyBuildDefaultHasher;
impl BuildHasher for MyBuildDefaultHasher {
type Hasher = DefaultHasher;

fn build_hasher(&self) -> Self::Hasher {
DefaultHasher::new()
}
}

const Y: HashMap<(), (), MyBuildDefaultHasher> = HashMap::with_hasher(MyBuildDefaultHasher);
let mut y = Y;
assert_eq!(y.len(), 0);
y.insert((), ());
assert_eq!(y.len(), 1);
}
1 change: 1 addition & 0 deletions std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@
// Library features (core):
// tidy-alphabetical-start
#![feature(array_chunks)]
#![feature(build_hasher_default_const_new)]
#![feature(c_str_module)]
#![feature(char_internals)]
#![feature(clone_to_uninit)]
Expand Down

0 comments on commit d515da6

Please sign in to comment.