Skip to main content

xtce_model/
ids.rs

1//! Typed indices into the [`crate::XtceDb`] arenas.
2//!
3//! The IR stores every entity in a `Vec` and refers to it by a `u32` newtype rather than by
4//! `Rc<RefCell<..>>` or `HashMap<String, ..>`. That keeps the model `Send + Sync`, makes
5//! cloning cheap, gives the code generator a stable numbering to emit, and turns reference
6//! following into an array index instead of a hash lookup.
7//!
8//! The newtypes are distinct so that a [`ParamId`] can never be used where a [`ContainerId`]
9//! is expected.
10
11use std::fmt;
12
13macro_rules! id_types {
14    ($($(#[$meta:meta])* $name:ident),* $(,)?) => {$(
15        $(#[$meta])*
16        #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17        pub struct $name(u32);
18
19        impl $name {
20            /// Wraps a raw index.
21            #[inline]
22            #[must_use]
23            pub const fn new(index: u32) -> Self {
24                Self(index)
25            }
26
27            /// The raw index, for use as an array subscript.
28            #[inline]
29            #[must_use]
30            pub const fn index(self) -> usize {
31                self.0 as usize
32            }
33
34            /// The raw index as a `u32`, for serialisation and code generation.
35            #[inline]
36            #[must_use]
37            pub const fn raw(self) -> u32 {
38                self.0
39            }
40        }
41
42        impl fmt::Debug for $name {
43            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44                write!(f, concat!(stringify!($name), "({})"), self.0)
45            }
46        }
47    )*};
48}
49
50id_types! {
51    /// Index of a `SpaceSystem`.
52    SpaceSystemId,
53    /// Index of a `Parameter`.
54    ParamId,
55    /// Index of a parameter type.
56    TypeId,
57    /// Index of a `SequenceContainer`.
58    ContainerId,
59    /// Index of a `MetaCommand`.
60    MetaCommandId,
61}
62
63/// A contiguous slice of a shared arena, stored in two words.
64#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
65pub struct Span {
66    start: u32,
67    len: u32,
68}
69
70impl Span {
71    /// An empty span.
72    pub const EMPTY: Self = Self { start: 0, len: 0 };
73
74    /// Builds a span from a start index and length.
75    #[inline]
76    #[must_use]
77    pub const fn new(start: u32, len: u32) -> Self {
78        Self { start, len }
79    }
80
81    /// Builds a span covering `start..end` of an arena, saturating on overflow.
82    #[must_use]
83    pub fn between(start: usize, end: usize) -> Self {
84        let start_u32 = u32::try_from(start).unwrap_or(u32::MAX);
85        let len = u32::try_from(end.saturating_sub(start)).unwrap_or(u32::MAX);
86        Self {
87            start: start_u32,
88            len,
89        }
90    }
91
92    /// Index of the first covered element.
93    #[inline]
94    #[must_use]
95    pub const fn start(self) -> usize {
96        self.start as usize
97    }
98
99    /// Number of elements covered.
100    #[inline]
101    #[must_use]
102    pub const fn len(self) -> usize {
103        self.len as usize
104    }
105
106    /// Whether the span covers nothing.
107    #[inline]
108    #[must_use]
109    pub const fn is_empty(self) -> bool {
110        self.len == 0
111    }
112
113    /// Slices `arena` with this span, yielding an empty slice if it is out of bounds.
114    #[inline]
115    #[must_use]
116    pub fn slice<T>(self, arena: &[T]) -> &[T] {
117        let start = self.start as usize;
118        arena
119            .get(start..start + self.len as usize)
120            .unwrap_or_default()
121    }
122}