machine_check_common/
node_id.rs

1use std::fmt::{Debug, Display};
2use std::num::NonZeroU64;
3
4use serde::{Deserialize, Serialize};
5
6/// State identifier. Represents an actual system state.
7///
8/// The identifier has 64 bits so there is no realistic possibility of overflow.
9/// Even generating states at a rate of 10 giga per second, it would take
10/// 58.45 years to overflow.
11///
12#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
13pub struct StateId(pub NonZeroU64);
14
15impl Debug for StateId {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        write!(f, "{}", self.0)
18    }
19}
20
21impl Display for StateId {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        <StateId as Debug>::fmt(self, f)
24    }
25}
26
27/// Node identifier. Either a root node or an actual system state.
28///
29/// The identifier has 64 bits so there is no realistic possibility of overflow.
30/// Even generating states at a rate of 10 giga per second, it would take
31/// 58.45 years to overflow.
32#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
33pub struct NodeId(Option<NonZeroU64>);
34
35impl NodeId {
36    /// A node that roots the state space. Its children are exactly the initial states.
37    pub const ROOT: NodeId = NodeId(None);
38}
39
40impl From<StateId> for NodeId {
41    fn from(state_id: StateId) -> Self {
42        NodeId(Some(state_id.0))
43    }
44}
45
46impl TryFrom<NodeId> for StateId {
47    type Error = ();
48
49    fn try_from(value: NodeId) -> Result<Self, ()> {
50        match value.0 {
51            Some(id) => Ok(StateId(id)),
52            None => Err(()),
53        }
54    }
55}
56
57impl Debug for NodeId {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self.0 {
60            Some(id) => write!(f, "{}", id),
61            None => write!(f, "0"),
62        }
63    }
64}
65
66impl Display for NodeId {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        <NodeId as Debug>::fmt(self, f)
69    }
70}