1
0
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:
Emil Ernerfeldt
2020-04-17 23:44:14 +02:00
parent 407df94945
commit 624e709a8f
7 changed files with 46 additions and 38 deletions

29
emigui/src/id.rs Normal file
View 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())
}
}