use crate::SharedString;
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ElementId {
Name(SharedString),
Integer(u64),
NamedInteger(SharedString, u64),
}
impl ElementId {
pub fn hash64(&self) -> u64 {
let mut h = rustc_hash::FxHasher::default();
self.hash(&mut h);
h.finish()
}
}
impl From<&'static str> for ElementId {
fn from(s: &'static str) -> Self {
ElementId::Name(s.into())
}
}
impl From<String> for ElementId {
fn from(s: String) -> Self {
ElementId::Name(s.into())
}
}
impl From<SharedString> for ElementId {
fn from(s: SharedString) -> Self {
ElementId::Name(s)
}
}
impl From<u64> for ElementId {
fn from(v: u64) -> Self {
ElementId::Integer(v)
}
}
impl From<usize> for ElementId {
fn from(v: usize) -> Self {
ElementId::Integer(v as u64)
}
}
impl From<i32> for ElementId {
fn from(v: i32) -> Self {
ElementId::Integer(v as u64)
}
}
impl From<(&'static str, usize)> for ElementId {
fn from((s, i): (&'static str, usize)) -> Self {
ElementId::NamedInteger(s.into(), i as u64)
}
}
#[macro_export]
macro_rules! unique_id {
($(#[$meta:meta])* $vis:vis struct $name:ident;) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
$vis struct $name(pub u64);
impl $name {
pub fn next() -> Self {
static COUNTER: ::std::sync::atomic::AtomicU64 = ::std::sync::atomic::AtomicU64::new(1);
Self(COUNTER.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed))
}
}
};
}
unique_id! {
pub struct ImageId;
}