mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Make Id a proper newtype
This commit is contained in:
29
emigui/src/id.rs
Normal file
29
emigui/src/id.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::{collections::hash_map::DefaultHasher, hash::Hash};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
|
||||
pub struct Id(u64);
|
||||
|
||||
impl Id {
|
||||
pub fn whole_screen() -> Self {
|
||||
Self(0)
|
||||
}
|
||||
|
||||
pub fn popup() -> Self {
|
||||
Self(1)
|
||||
}
|
||||
|
||||
pub fn new<H: Hash>(source: &H) -> Id {
|
||||
use std::hash::Hasher;
|
||||
let mut hasher = DefaultHasher::new();
|
||||
source.hash(&mut hasher);
|
||||
Id(hasher.finish())
|
||||
}
|
||||
|
||||
pub fn with<H: Hash>(self, child: &H) -> Id {
|
||||
use std::hash::Hasher;
|
||||
let mut hasher = DefaultHasher::new();
|
||||
hasher.write_u64(self.0);
|
||||
child.hash(&mut hasher);
|
||||
Id(hasher.finish())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user