1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 21:00:03 -04:00

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.
This commit is contained in:
Emil Ernerfeldt
2021-10-27 08:51:34 +02:00
committed by GitHub
parent bbe0f6089c
commit 8e4fd942a9
31 changed files with 917 additions and 1347 deletions

View File

@@ -1,7 +1,10 @@
/// Hash the given value with a predictable hasher.
#[inline]
pub fn hash(value: impl std::hash::Hash) -> u64 {
hash_with(value, ahash::AHasher::new_with_keys(123, 456))
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.