Skip to main content

tui_lipan/core/node/
id.rs

1use crate::utils::arena::ArenaId;
2
3/// Runtime node identifier.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5pub struct NodeId {
6    pub(crate) index: u32,
7    pub(crate) generation: u32,
8}
9
10impl NodeId {
11    /// An invalid node id.
12    pub const INVALID: Self = Self {
13        index: u32::MAX,
14        generation: 0,
15    };
16
17    pub(crate) fn new(index: u32, generation: u32) -> Self {
18        Self { index, generation }
19    }
20
21    pub(crate) fn index(self) -> usize {
22        self.index as usize
23    }
24
25    pub(crate) fn is_invalid(self) -> bool {
26        self.index == u32::MAX
27    }
28}
29
30impl Default for NodeId {
31    fn default() -> Self {
32        Self::INVALID
33    }
34}
35
36impl ArenaId for NodeId {
37    const INVALID: Self = Self::INVALID;
38
39    fn from_parts(index: u32, generation: u32) -> Self {
40        Self::new(index, generation)
41    }
42
43    fn index(self) -> usize {
44        self.index()
45    }
46
47    fn generation(self) -> u32 {
48        self.generation
49    }
50}