Add epaint::util::hash function for hashing a value

This commit is contained in:
Emil Ernerfeldt
2021-09-26 03:16:12 +02:00
parent 976260c2bd
commit ba0e3780a1
5 changed files with 19 additions and 29 deletions
+1 -6
View File
@@ -13,11 +13,6 @@ impl TypeId {
impl From<std::any::TypeId> for TypeId {
fn from(id: std::any::TypeId) -> Self {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
id.hash(&mut hasher);
Self(hasher.finish())
Self(epaint::util::hash(id))
}
}
+2 -9
View File
@@ -1,4 +1,4 @@
use std::hash::{Hash, Hasher};
use epaint::util::hash;
const SIZE: usize = 1024; // must be small for web/WASM build (for unknown reason)
@@ -24,7 +24,7 @@ impl<K, V> std::fmt::Debug for Cache<K, V> {
impl<K, V> Cache<K, V>
where
K: Hash + PartialEq,
K: std::hash::Hash + PartialEq,
{
pub fn get(&self, key: &K) -> Option<&V> {
let bucket = (hash(key) % (SIZE as u64)) as usize;
@@ -39,10 +39,3 @@ where
self.0[bucket] = Some((key, value));
}
}
fn hash(value: impl Hash) -> u64 {
use std::collections::hash_map::DefaultHasher;
let mut hasher = DefaultHasher::default();
value.hash(&mut hasher);
hasher.finish()
}