diff options
| author | Nixon Enraght-Moony <nixon.emoony@gmail.com> | 2022-10-02 12:57:10 +0100 |
|---|---|---|
| committer | Nixon Enraght-Moony <nixon.emoony@gmail.com> | 2022-10-02 13:07:13 +0100 |
| commit | 346a49fe48fd22bd9588d929bc3115016e654801 (patch) | |
| tree | 6704500fa72588d96411e45bdd80016c5a52bcfb | |
| parent | c2590e6e892cce1105cc57b480aa07a51ae95fe5 (diff) | |
| download | rust-346a49fe48fd22bd9588d929bc3115016e654801.tar.gz rust-346a49fe48fd22bd9588d929bc3115016e654801.zip | |
Make Hash{Set,Map}::with_hasher unstably const
| -rw-r--r-- | library/std/src/collections/hash/map.rs | 3 | ||||
| -rw-r--r-- | library/std/src/collections/hash/map/tests.rs | 6 | ||||
| -rw-r--r-- | library/std/src/collections/hash/set.rs | 3 | ||||
| -rw-r--r-- | library/std/src/collections/hash/set/tests.rs | 6 | ||||
| -rw-r--r-- | library/std/src/lib.rs | 1 |
5 files changed, 17 insertions, 2 deletions
diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index d2db4bb7a46..34983b976e3 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -280,7 +280,8 @@ impl<K, V, S> HashMap<K, V, S> { /// ``` #[inline] #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] - pub fn with_hasher(hash_builder: S) -> HashMap<K, V, S> { + #[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")] + pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> { HashMap { base: base::HashMap::with_hasher(hash_builder) } } diff --git a/library/std/src/collections/hash/map/tests.rs b/library/std/src/collections/hash/map/tests.rs index cb3032719fa..65634f2063f 100644 --- a/library/std/src/collections/hash/map/tests.rs +++ b/library/std/src/collections/hash/map/tests.rs @@ -1115,3 +1115,9 @@ fn from_array() { // that's a problem! let _must_not_require_type_annotation = HashMap::from([(1, 2)]); } + +#[test] +fn const_with_hasher() { + const X: HashMap<(), (), ()> = HashMap::with_hasher(()); + assert_eq!(X.len(), 0); +} diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 5b6a415fadc..c36eeae3388 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -376,7 +376,8 @@ impl<T, S> HashSet<T, S> { /// ``` #[inline] #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] - pub fn with_hasher(hasher: S) -> HashSet<T, S> { + #[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")] + pub const fn with_hasher(hasher: S) -> HashSet<T, S> { HashSet { base: base::HashSet::with_hasher(hasher) } } diff --git a/library/std/src/collections/hash/set/tests.rs b/library/std/src/collections/hash/set/tests.rs index 233db276b9e..941a0450cc7 100644 --- a/library/std/src/collections/hash/set/tests.rs +++ b/library/std/src/collections/hash/set/tests.rs @@ -496,3 +496,9 @@ fn from_array() { // that's a problem! let _must_not_require_type_annotation = HashSet::from([1, 2]); } + +#[test] +fn const_with_hasher() { + const X: HashSet<(), ()> = HashSet::with_hasher(()); + assert_eq!(X.len(), 0); +} diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 97eed8a65c5..353785d3d12 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -351,6 +351,7 @@ // Only used in tests/benchmarks: // // Only for const-ness: +#![feature(const_collections_with_hasher)] #![feature(const_io_structs)] #![feature(const_ip)] #![feature(const_ipv4)] |
