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
/*
    Appellation: id <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::id::AtomicId;

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize,))]
pub struct IndexId<Idx = usize> {
    id: AtomicId,
    index: Idx,
}

impl<Idx> IndexId<Idx> {
    pub fn new(id: AtomicId, index: Idx) -> Self {
        Self { id, index }
    }
    pub fn from_index(index: Idx) -> Self {
        Self {
            id: AtomicId::new(),
            index,
        }
    }

    pub fn id(&self) -> usize {
        *self.id
    }

    pub fn index(&self) -> &Idx {
        &self.index
    }
}

impl<Idx> core::fmt::Display for IndexId<Idx>
where
    Idx: core::fmt::Display,
{
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "{}({})", self.id(), self.index())
    }
}