egui/epaint/src/util.rs
Emil Ernerfeldt 8e4fd942a9
Refactor memory data (#836)
This refactors the widget state storage introduced by @optozorax in https://github.com/emilk/egui/pull/257

* Unify the four buckets (`data`, `data_temp`, `id_data` and `id_data_temp`) into a single `data`.
  * Less complexity, and also less chance of error (storing in one bucket, reading from another).
* Store data by `Id` and `TypeId`.
  * Users can thus reuse the same `Id` to store many types.
  * Uses a simple xor of id and typeid, which is fast and good since both id and typeid are already high-entropy hashes.
* Use different suffixes on the functions to pick if you want the data persisted or not (`get_temp`, `insert_persisted`, etc).
  * Writing with one suffix and reading with the other works.
* To store state not bound to a specific `Id` (i.e. only based on type), use the new `Id::null` as the key.
2021-10-27 08:51:34 +02:00

15 lines
458 B
Rust

/// Hash the given value with a predictable hasher.
#[inline]
pub fn hash(value: impl std::hash::Hash) -> u64 {
use std::hash::Hasher as _;
let mut hasher = ahash::AHasher::new_with_keys(123, 456);
value.hash(&mut hasher);
hasher.finish()
}
/// 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()
}