From 481b282e8a123364631c0c1a702ae609c878a9a3 Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Sat, 14 Aug 2021 00:02:02 -0400 Subject: [PATCH 1/2] make `BuildHasher` object safe --- library/core/src/hash/mod.rs | 5 ++++- src/test/ui/build-hasher-object-safe.rs | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/build-hasher-object-safe.rs diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index 9dbefe0822e36..8dec643255f64 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -520,7 +520,10 @@ pub trait BuildHasher { /// ); /// ``` #[unstable(feature = "build_hasher_simple_hash_one", issue = "86161")] - fn hash_one(&self, x: T) -> u64 { + fn hash_one(&self, x: T) -> u64 + where + Self: Sized, + { let mut hasher = self.build_hasher(); x.hash(&mut hasher); hasher.finish() diff --git a/src/test/ui/build-hasher-object-safe.rs b/src/test/ui/build-hasher-object-safe.rs new file mode 100644 index 0000000000000..50ec8e5e8d96e --- /dev/null +++ b/src/test/ui/build-hasher-object-safe.rs @@ -0,0 +1,10 @@ +// run-pass + +use std::hash::BuildHasher; +use std::collections::hash_map::{DefaultHasher, RandomState}; + +fn ensure_object_safe(_: &dyn BuildHasher) {} + +fn main() { + ensure_object_safe(&RandomState::new()); +} From 58f988fa408fb58266c1fbbc62fa29c4473d5e10 Mon Sep 17 00:00:00 2001 From: ibraheemdev Date: Sun, 15 Aug 2021 13:00:25 -0400 Subject: [PATCH 2/2] move object safety test to library/core --- library/core/tests/hash/mod.rs | 9 ++++++++- src/test/ui/build-hasher-object-safe.rs | 10 ---------- 2 files changed, 8 insertions(+), 11 deletions(-) delete mode 100644 src/test/ui/build-hasher-object-safe.rs diff --git a/library/core/tests/hash/mod.rs b/library/core/tests/hash/mod.rs index 1566d35749017..72ccdd4848a47 100644 --- a/library/core/tests/hash/mod.rs +++ b/library/core/tests/hash/mod.rs @@ -1,7 +1,7 @@ mod sip; use std::default::Default; -use std::hash::{Hash, Hasher}; +use std::hash::{BuildHasher, Hash, Hasher}; use std::rc::Rc; struct MyHasher { @@ -139,3 +139,10 @@ fn test_indirect_hasher() { } assert_eq!(hasher.hash, 5); } + +#[test] +fn test_build_hasher_object_safe() { + use std::collections::hash_map::{DefaultHasher, RandomState}; + + let _: &dyn BuildHasher = &RandomState::new(); +} diff --git a/src/test/ui/build-hasher-object-safe.rs b/src/test/ui/build-hasher-object-safe.rs deleted file mode 100644 index 50ec8e5e8d96e..0000000000000 --- a/src/test/ui/build-hasher-object-safe.rs +++ /dev/null @@ -1,10 +0,0 @@ -// run-pass - -use std::hash::BuildHasher; -use std::collections::hash_map::{DefaultHasher, RandomState}; - -fn ensure_object_safe(_: &dyn BuildHasher) {} - -fn main() { - ensure_object_safe(&RandomState::new()); -}