diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index c3f3cd7294254..a800e1b41fbe3 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -784,7 +784,8 @@ impl Clone for BuildHasherDefault { } #[stable(since = "1.7.0", feature = "build_hasher")] -impl Default for BuildHasherDefault { +#[rustc_const_unstable(feature = "const_default", issue = "143894")] +impl const Default for BuildHasherDefault { fn default() -> BuildHasherDefault { Self::new() } diff --git a/library/core/src/hash/sip.rs b/library/core/src/hash/sip.rs index 4f2e8a22d1805..4569c7da035d1 100644 --- a/library/core/src/hash/sip.rs +++ b/library/core/src/hash/sip.rs @@ -166,16 +166,18 @@ impl SipHasher13 { /// Creates a new `SipHasher13` with the two initial keys set to 0. #[inline] #[unstable(feature = "hashmap_internals", issue = "none")] + #[rustc_const_unstable(feature = "const_default", issue = "143894")] #[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")] - pub fn new() -> SipHasher13 { + pub const fn new() -> SipHasher13 { SipHasher13::new_with_keys(0, 0) } /// Creates a `SipHasher13` that is keyed off the provided keys. #[inline] #[unstable(feature = "hashmap_internals", issue = "none")] + #[rustc_const_unstable(feature = "const_default", issue = "143894")] #[deprecated(since = "1.13.0", note = "use `std::hash::DefaultHasher` instead")] - pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 { + pub const fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 { SipHasher13 { hasher: Hasher::new_with_keys(key0, key1) } } } @@ -338,7 +340,8 @@ impl Clone for Hasher { } } -impl Default for Hasher { +#[rustc_const_unstable(feature = "const_default", issue = "143894")] +impl const Default for Hasher { /// Creates a `Hasher` with the two initial keys set to 0. #[inline] fn default() -> Hasher { diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index ad6328f76ed6d..b82beb3b8b2ef 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1444,9 +1444,10 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for HashMap +#[rustc_const_unstable(feature = "const_default", issue = "143894")] +impl const Default for HashMap where - S: Default, + S: [const] Default, { /// Creates an empty `HashMap`, with the `Default` value for the hasher. #[inline] diff --git a/library/std/src/collections/hash/map/tests.rs b/library/std/src/collections/hash/map/tests.rs index 9f7df20a1d7e4..cc1a9900bec97 100644 --- a/library/std/src/collections/hash/map/tests.rs +++ b/library/std/src/collections/hash/map/tests.rs @@ -1053,4 +1053,7 @@ fn const_with_hasher() { assert_eq!(y.len(), 0); y.insert((), ()); assert_eq!(y.len(), 1); + + const Z: HashMap<(), (), BuildHasherDefault> = Default::default(); + assert_eq!(X, Z); } diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 02a4a0d9c8156..3f3d601e4d30c 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1236,14 +1236,15 @@ where } #[stable(feature = "rust1", since = "1.0.0")] -impl Default for HashSet +#[rustc_const_unstable(feature = "const_default", issue = "143894")] +impl const Default for HashSet where - S: Default, + S: [const] Default, { /// Creates an empty `HashSet` with the `Default` value for the hasher. #[inline] fn default() -> HashSet { - HashSet { base: Default::default() } + HashSet { base: base::HashSet::with_hasher(Default::default()) } } } diff --git a/library/std/src/collections/hash/set/tests.rs b/library/std/src/collections/hash/set/tests.rs index 8ee8a3e8bf6ae..d7bbc7bdc6a5b 100644 --- a/library/std/src/collections/hash/set/tests.rs +++ b/library/std/src/collections/hash/set/tests.rs @@ -504,7 +504,9 @@ fn from_array() { #[test] fn const_with_hasher() { const X: HashSet<(), ()> = HashSet::with_hasher(()); + const Y: HashSet<(), ()> = Default::default(); assert_eq!(X.len(), 0); + assert_eq!(Y.len(), 0); } #[test] diff --git a/library/std/src/hash/random.rs b/library/std/src/hash/random.rs index 236803b24a2ec..fab090e31f03e 100644 --- a/library/std/src/hash/random.rs +++ b/library/std/src/hash/random.rs @@ -105,14 +105,16 @@ impl DefaultHasher { #[stable(feature = "hashmap_default_hasher", since = "1.13.0")] #[inline] #[allow(deprecated)] + #[rustc_const_unstable(feature = "const_default", issue = "143894")] #[must_use] - pub fn new() -> DefaultHasher { + pub const fn new() -> DefaultHasher { DefaultHasher(SipHasher13::new_with_keys(0, 0)) } } #[stable(feature = "hashmap_default_hasher", since = "1.13.0")] -impl Default for DefaultHasher { +#[rustc_const_unstable(feature = "const_default", issue = "143894")] +impl const Default for DefaultHasher { /// Creates a new `DefaultHasher` using [`new`]. /// See its documentation for more. ///