statime_base/
identifiers.rs1use core::sync::atomic::AtomicUsize;
2
3#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
5pub struct ClockId(usize);
6
7impl ClockId {
8 #[expect(
10 clippy::new_without_default,
11 reason = "The new value is non-trivial and non-constant, therefore not fitting for default."
12 )]
13 pub fn new() -> ClockId {
14 static COUNTER: AtomicUsize = AtomicUsize::new(0);
15 ClockId(COUNTER.fetch_add(1, core::sync::atomic::Ordering::Relaxed))
16 }
17}
18
19#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
26pub struct LinkId(ClockId, ClockId, usize);
27
28impl LinkId {
29 pub fn new(a: ClockId, b: ClockId) -> Option<LinkId> {
31 static COUNTER: AtomicUsize = AtomicUsize::new(0);
32
33 if a == b {
34 None
35 } else {
36 Some(LinkId(
37 a,
38 b,
39 COUNTER.fetch_add(1, core::sync::atomic::Ordering::Relaxed),
40 ))
41 }
42 }
43
44 #[must_use]
46 pub fn first_clock(self) -> ClockId {
47 self.0
48 }
49
50 #[must_use]
52 pub fn second_clock(self) -> ClockId {
53 self.1
54 }
55
56 #[must_use]
58 pub fn contains_clock(self, clock: ClockId) -> bool {
59 self.0 == clock || self.1 == clock
60 }
61
62 #[must_use]
66 pub fn forward(self) -> DirectedLinkId {
67 DirectedLinkId(self, Direction::Forward)
68 }
69
70 #[must_use]
74 pub fn reverse(self) -> DirectedLinkId {
75 DirectedLinkId(self, Direction::Reverse)
76 }
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
81pub struct DirectedLinkId(LinkId, Direction);
82
83impl DirectedLinkId {
84 #[must_use]
86 pub fn new(link_id: LinkId, direction: Direction) -> DirectedLinkId {
87 DirectedLinkId(link_id, direction)
88 }
89
90 #[must_use]
92 pub fn link_id(self) -> LinkId {
93 self.0
94 }
95
96 #[must_use]
98 pub fn direction(self) -> Direction {
99 self.1
100 }
101
102 #[must_use]
104 pub fn reverse(self) -> DirectedLinkId {
105 DirectedLinkId::new(self.0, self.1.reverse())
106 }
107
108 #[must_use]
110 pub fn from_clock(self) -> ClockId {
111 match self.1 {
112 Direction::Forward => self.0.first_clock(),
113 Direction::Reverse => self.0.second_clock(),
114 }
115 }
116
117 #[must_use]
119 pub fn to_clock(self) -> ClockId {
120 match self.1 {
121 Direction::Forward => self.0.second_clock(),
122 Direction::Reverse => self.0.first_clock(),
123 }
124 }
125
126 #[must_use]
128 pub fn is_reverse_of(self, other: DirectedLinkId) -> bool {
129 self.0 == other.0 && self.1.is_reverse_of(other.1)
130 }
131}
132
133#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
136pub enum Direction {
137 Forward,
139 Reverse,
141}
142
143impl Direction {
144 #[must_use]
146 pub fn reverse(self) -> Direction {
147 match self {
148 Direction::Forward => Direction::Reverse,
149 Direction::Reverse => Direction::Forward,
150 }
151 }
152
153 #[must_use]
155 pub fn is_reverse_of(self, other: Direction) -> bool {
156 self != other
157 }
158}
159
160#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
162#[non_exhaustive]
163pub enum SourceType {
164 Pps,
166 Sock,
168 Ntp,
170 Csptp,
172}