Skip to content

Commit 346a49f

Browse files
committed
Make Hash{Set,Map}::with_hasher unstably const
1 parent c2590e6 commit 346a49f

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ impl<K, V, S> HashMap<K, V, S> {
280280
/// ```
281281
#[inline]
282282
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
283-
pub fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
283+
#[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")]
284+
pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
284285
HashMap { base: base::HashMap::with_hasher(hash_builder) }
285286
}
286287

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

+6
Original file line numberDiff line numberDiff line change
@@ -1115,3 +1115,9 @@ fn from_array() {
11151115
// that's a problem!
11161116
let _must_not_require_type_annotation = HashMap::from([(1, 2)]);
11171117
}
1118+
1119+
#[test]
1120+
fn const_with_hasher() {
1121+
const X: HashMap<(), (), ()> = HashMap::with_hasher(());
1122+
assert_eq!(X.len(), 0);
1123+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ impl<T, S> HashSet<T, S> {
376376
/// ```
377377
#[inline]
378378
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
379-
pub fn with_hasher(hasher: S) -> HashSet<T, S> {
379+
#[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")]
380+
pub const fn with_hasher(hasher: S) -> HashSet<T, S> {
380381
HashSet { base: base::HashSet::with_hasher(hasher) }
381382
}
382383

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

+6
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,9 @@ fn from_array() {
496496
// that's a problem!
497497
let _must_not_require_type_annotation = HashSet::from([1, 2]);
498498
}
499+
500+
#[test]
501+
fn const_with_hasher() {
502+
const X: HashSet<(), ()> = HashSet::with_hasher(());
503+
assert_eq!(X.len(), 0);
504+
}

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@
351351
// Only used in tests/benchmarks:
352352
//
353353
// Only for const-ness:
354+
#![feature(const_collections_with_hasher)]
354355
#![feature(const_io_structs)]
355356
#![feature(const_ip)]
356357
#![feature(const_ipv4)]

0 commit comments

Comments
 (0)