runic_diagnostics/source/
source_id.rs1use core::num::NonZeroU32;
2
3#[cfg(feature = "std")]
4static NEXT_SOURCE_NAMESPACE: std::sync::Mutex<u32> = std::sync::Mutex::new(u32::MAX);
5
6#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct SourceRevision(pub u32);
8
9#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
11pub struct SourceNamespace(Option<NonZeroU32>);
12
13impl Default for SourceNamespace {
14 fn default() -> Self {
15 Self::UNKNOWN
16 }
17}
18
19impl core::fmt::Display for SourceNamespace {
20 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21 if let Some(value) = self.0 {
22 core::fmt::Display::fmt(&value, f)
23 } else {
24 f.write_str("unknown")
25 }
26 }
27}
28
29impl SourceNamespace {
30 pub const UNKNOWN: Self = Self(None);
35
36 pub const fn new(namespace: NonZeroU32) -> Self {
38 Self(Some(namespace))
39 }
40
41 pub fn new_unchecked(namespace: u32) -> Self {
46 Self(NonZeroU32::new(namespace))
47 }
48
49 pub const fn is_unknown(self) -> bool {
51 self.0.is_none()
52 }
53
54 #[cfg(feature = "std")]
59 pub fn fresh() -> Option<Self> {
60 let mut next =
61 NEXT_SOURCE_NAMESPACE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
62 let value = NonZeroU32::new(*next)?;
63 *next = value.get().saturating_sub(1);
64 Some(Self::new(value))
65 }
66}
67
68#[cfg(all(test, feature = "std"))]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn fresh_namespaces_are_known_and_distinct() {
74 let first = SourceNamespace::fresh().unwrap();
75 let second = SourceNamespace::fresh().unwrap();
76 assert!(!first.is_unknown());
77 assert!(!second.is_unknown());
78 assert_ne!(first, second);
79 }
80}
81
82#[cfg(feature = "arbitrary")]
83impl proptest::prelude::Arbitrary for SourceNamespace {
84 type Parameters = ();
85 type Strategy = proptest::prelude::BoxedStrategy<Self>;
86
87 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
88 use proptest::prelude::*;
89
90 any::<u32>().prop_map(SourceNamespace::new_unchecked).boxed()
91 }
92}
93
94#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
96pub struct SourceId {
97 namespace: SourceNamespace,
98 local: u32,
99}
100
101impl Default for SourceId {
102 fn default() -> Self {
103 Self::UNKNOWN
104 }
105}
106
107impl SourceId {
108 pub const UNKNOWN: Self = Self {
109 namespace: SourceNamespace::UNKNOWN,
110 local: u32::MAX,
111 };
112
113 pub const fn new(namespace: SourceNamespace, local: u32) -> Self {
114 Self { namespace, local }
115 }
116
117 pub const fn namespace(self) -> SourceNamespace {
118 self.namespace
119 }
120
121 pub const fn local(self) -> u32 {
122 self.local
123 }
124
125 pub const fn is_unknown(self) -> bool {
126 self.namespace.is_unknown()
127 }
128}
129
130#[cfg(feature = "arbitrary")]
131impl proptest::prelude::Arbitrary for SourceId {
132 type Parameters = ();
133 type Strategy = proptest::prelude::BoxedStrategy<Self>;
134
135 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
136 use proptest::prelude::*;
137
138 (any::<SourceNamespace>(), any::<u32>())
139 .prop_map(|(namespace, local)| SourceId { namespace, local })
140 .boxed()
141 }
142}
143
144#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
146pub enum SourceKey {
147 Session(SourceId),
148 Attached(SourceId),
149}
150
151impl SourceKey {
152 pub const fn id(self) -> SourceId {
153 match self {
154 Self::Session(id) | Self::Attached(id) => id,
155 }
156 }
157}