Skip to content

Commit

Permalink
Rollup merge of #88031 - ibraheemdev:build-hasher-object-safe, r=m-ou-se
Browse files Browse the repository at this point in the history
Make `BuildHasher` object safe

Resolves #87991
  • Loading branch information
GuillaumeGomez authored Aug 18, 2021
2 parents 627bc60 + 58f988f commit 9b7c771
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion library/core/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,10 @@ pub trait BuildHasher {
/// );
/// ```
#[unstable(feature = "build_hasher_simple_hash_one", issue = "86161")]
fn hash_one<T: Hash>(&self, x: T) -> u64 {
fn hash_one<T: Hash>(&self, x: T) -> u64
where
Self: Sized,
{
let mut hasher = self.build_hasher();
x.hash(&mut hasher);
hasher.finish()
Expand Down
9 changes: 8 additions & 1 deletion library/core/tests/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<Hasher = DefaultHasher> = &RandomState::new();
}

0 comments on commit 9b7c771

Please sign in to comment.