layir/
id.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::{fmt::Debug, hash::Hash};

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct Id<T>(u64, std::marker::PhantomData<T>);

impl<T> Clone for Id<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Id<T> {}

impl<T> PartialEq for Id<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
    }
}

impl<T> Debug for Id<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Id")
            .field(&self.0)
            .field(&format_args!("_"))
            .finish()
    }
}

impl<T> Eq for Id<T> {}

impl<T> Id<T> {
    pub(crate) fn new() -> Self {
        Self(0, std::marker::PhantomData)
    }

    pub(crate) fn alloc(&mut self) -> Self {
        *self = Self(self.0 + 1, std::marker::PhantomData);
        *self
    }
}

impl<T> Hash for Id<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}