freya_native_core/
node_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
use std::ops::Deref;

#[derive(Clone, Copy, PartialEq, Debug, Eq, PartialOrd, Ord, Hash, Default)]
pub struct NodeId(shipyard::EntityId);

impl Deref for NodeId {
    type Target = shipyard::EntityId;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl From<NodeId> for shipyard::EntityId {
    fn from(value: NodeId) -> Self {
        value.0
    }
}

impl From<shipyard::EntityId> for NodeId {
    fn from(value: shipyard::EntityId) -> Self {
        NodeId(value)
    }
}

impl NodeId {
    pub fn serialize(&self) -> String {
        format!("{}-{}", self.index(), self.gen())
    }

    pub fn deserialize(node_id: &str) -> Self {
        let (index, gen) = node_id.split_once('-').unwrap();
        Self(shipyard::EntityId::new_from_index_and_gen(
            index.parse().unwrap(),
            gen.parse().unwrap(),
        ))
    }
}

impl torin::prelude::NodeKey for NodeId {}