mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 21:00:03 -04:00
Add epaint::util::hash function for hashing a value
This commit is contained in:
@@ -82,6 +82,7 @@ mod stroke;
|
||||
pub mod tessellator;
|
||||
pub mod text;
|
||||
mod texture_atlas;
|
||||
pub mod util;
|
||||
|
||||
pub use {
|
||||
color::{Color32, Rgba},
|
||||
|
||||
@@ -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<Galley> {
|
||||
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) => {
|
||||
|
||||
12
epaint/src/util.rs
Normal file
12
epaint/src/util.rs
Normal file
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user