Use ahash for Id and other things that need hashing

This commit is contained in:
Emil Ernerfeldt 2021-09-28 17:56:24 +02:00
parent f6fb4d942a
commit 2e83e36146
2 changed files with 5 additions and 11 deletions

View file

@ -1,7 +1,5 @@
// TODO: have separate types `PositionId` and `UniqueId`. ? // TODO: have separate types `PositionId` and `UniqueId`. ?
use std::hash::Hash;
/// egui tracks widgets frame-to-frame using `Id`s. /// egui tracks widgets frame-to-frame using `Id`s.
/// ///
/// For instance, if you start dragging a slider one frame, egui stores /// For instance, if you start dragging a slider one frame, egui stores
@ -37,21 +35,17 @@ impl Id {
} }
/// Generate a new `Id` by hashing some source (e.g. a string or integer). /// Generate a new `Id` by hashing some source (e.g. a string or integer).
pub fn new(source: impl Hash) -> Id { pub fn new(source: impl std::hash::Hash) -> Id {
// NOTE: AHasher is NOT suitable for this!
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher; use std::hash::Hasher;
let mut hasher = DefaultHasher::default(); let mut hasher = epaint::ahash::AHasher::new_with_keys(123, 456);
source.hash(&mut hasher); source.hash(&mut hasher);
Id(hasher.finish()) Id(hasher.finish())
} }
/// Generate a new `Id` by hashing the parent `Id` and the given argument. /// Generate a new `Id` by hashing the parent `Id` and the given argument.
pub fn with(self, child: impl Hash) -> Id { pub fn with(self, child: impl std::hash::Hash) -> Id {
// NOTE: AHasher is NOT suitable for this!
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher; use std::hash::Hasher;
let mut hasher = DefaultHasher::default(); let mut hasher = epaint::ahash::AHasher::new_with_keys(123, 456);
hasher.write_u64(self.0); hasher.write_u64(self.0);
child.hash(&mut hasher); child.hash(&mut hasher);
Id(hasher.finish()) Id(hasher.finish())

View file

@ -1,7 +1,7 @@
/// Hash the given value with a predictable hasher. /// Hash the given value with a predictable hasher.
#[inline] #[inline]
pub fn hash(value: impl std::hash::Hash) -> u64 { pub fn hash(value: impl std::hash::Hash) -> u64 {
hash_with(value, std::collections::hash_map::DefaultHasher::default()) hash_with(value, ahash::AHasher::new_with_keys(123, 456))
} }
/// Hash the given value with the given hasher. /// Hash the given value with the given hasher.