diff --git a/egui/src/any/serializable/type_id.rs b/egui/src/any/serializable/type_id.rs index 320e726ea..abc377c14 100644 --- a/egui/src/any/serializable/type_id.rs +++ b/egui/src/any/serializable/type_id.rs @@ -13,11 +13,6 @@ impl TypeId { impl From 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)) } } diff --git a/egui/src/util/cache.rs b/egui/src/util/cache.rs index 575ee0b6b..88a7a4dc7 100644 --- a/egui/src/util/cache.rs +++ b/egui/src/util/cache.rs @@ -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 std::fmt::Debug for Cache { impl Cache 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() -} diff --git a/epaint/src/lib.rs b/epaint/src/lib.rs index 0ccb0bddb..e63de0979 100644 --- a/epaint/src/lib.rs +++ b/epaint/src/lib.rs @@ -82,6 +82,7 @@ mod stroke; pub mod tessellator; pub mod text; mod texture_atlas; +pub mod util; pub use { color::{Color32, Rgba}, diff --git a/epaint/src/text/fonts.rs b/epaint/src/text/fonts.rs index fe204fa9f..f90a46f35 100644 --- a/epaint/src/text/fonts.rs +++ b/epaint/src/text/fonts.rs @@ -1,8 +1,4 @@ -use std::{ - collections::BTreeMap, - hash::{Hash, Hasher}, - sync::Arc, -}; +use std::{collections::BTreeMap, sync::Arc}; use crate::{ mutex::Mutex, @@ -261,10 +257,7 @@ impl Fonts { let mut atlas = atlas.lock(); let texture = atlas.texture_mut(); // Make sure we seed the texture version with something unique based on the default characters: - use std::collections::hash_map::DefaultHasher; - let mut hasher = DefaultHasher::default(); - texture.pixels.hash(&mut hasher); - texture.version = hasher.finish(); + texture.version = crate::util::hash(&texture.pixels); } Self { @@ -412,11 +405,7 @@ struct GalleyCache { impl GalleyCache { fn layout(&mut self, fonts: &Fonts, job: LayoutJob) -> Arc { - let hash = { - let mut hasher = ahash::AHasher::new_with_keys(123, 456); // TODO: even faster hasher? - job.hash(&mut hasher); - hasher.finish() - }; + let hash = crate::util::hash_with(&job, ahash::AHasher::new_with_keys(123, 456)); // TODO: even faster hasher? match self.cache.entry(hash) { std::collections::hash_map::Entry::Occupied(entry) => { diff --git a/epaint/src/util.rs b/epaint/src/util.rs new file mode 100644 index 000000000..229818416 --- /dev/null +++ b/epaint/src/util.rs @@ -0,0 +1,12 @@ +/// Hash the given value with a predictable hasher. +#[inline] +pub fn hash(value: impl std::hash::Hash) -> u64 { + hash_with(value, std::collections::hash_map::DefaultHasher::default()) +} + +/// Hash the given value with the given hasher. +#[inline] +pub fn hash_with(value: impl std::hash::Hash, mut hasher: impl std::hash::Hasher) -> u64 { + value.hash(&mut hasher); + hasher.finish() +}