const_with_hasher test: actually construct a usable HashMap

This commit is contained in:
Ralf Jung 2024-11-02 11:22:22 +01:00
parent b5f4883a06
commit 34432f7494
2 changed files with 24 additions and 3 deletions

View File

@ -5,7 +5,7 @@
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
@ -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);
}

View File

@ -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)]