about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-11-02 11:22:22 +0100
committerRalf Jung <post@ralfj.de>2024-11-02 11:27:14 +0100
commit34432f749463983e140e5047fb33b212e695f36a (patch)
tree0a919af8f06b0c661f4ff0be5660dc20da7dcc31 /library/std/src
parentb5f4883a06f1d861b108ac892f07f4fa37eb1bed (diff)
downloadrust-34432f749463983e140e5047fb33b212e695f36a.tar.gz
rust-34432f749463983e140e5047fb33b212e695f36a.zip
const_with_hasher test: actually construct a usable HashMap
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/collections/hash/map/tests.rs26
-rw-r--r--library/std/src/lib.rs1
2 files changed, 24 insertions, 3 deletions
diff --git a/library/std/src/collections/hash/map/tests.rs b/library/std/src/collections/hash/map/tests.rs
index b79ad1c3119..a275488a556 100644
--- a/library/std/src/collections/hash/map/tests.rs
+++ b/library/std/src/collections/hash/map/tests.rs
@@ -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
@@ -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);
 }
diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
index 1de52eb7b21..887d845e03c 100644
--- a/library/std/src/lib.rs
+++ b/library/std/src/lib.rs
@@ -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)]