Skip to main content

molframe_core/provider/
ids.rs

1//! Stable global identities and compact chunk-local rows.
2
3use std::fmt;
4
5macro_rules! global_id {
6    ($name:ident, $label:literal) => {
7        #[doc = concat!("Stable 64-bit ", $label, ".")]
8        #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9        #[repr(transparent)]
10        pub struct $name(u64);
11
12        impl $name {
13            #[doc = concat!("Creates a ", $label, " from its stable value.")]
14            #[must_use]
15            pub const fn new(value: u64) -> Self {
16                Self(value)
17            }
18
19            #[doc = concat!("Returns the raw ", $label, " value.")]
20            #[must_use]
21            pub const fn get(self) -> u64 {
22                self.0
23            }
24
25            #[doc = concat!("Returns the next ", $label, ", if representable.")]
26            #[must_use]
27            pub const fn next(self) -> Option<Self> {
28                match self.0.checked_add(1) {
29                    Some(value) => Some(Self(value)),
30                    None => None,
31                }
32            }
33        }
34
35        impl fmt::Debug for $name {
36            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37                write!(formatter, "{}({})", $label, self.0)
38            }
39        }
40
41        impl fmt::Display for $name {
42            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
43                write!(formatter, "{}", self.0)
44            }
45        }
46    };
47}
48
49global_id!(DatasetId, "dataset");
50global_id!(ChunkId, "chunk");
51global_id!(LogicalRow, "logical row");
52
53/// Compact row index used only inside one resident chunk.
54#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
55#[repr(transparent)]
56pub struct LocalRow(u32);
57
58impl LocalRow {
59    /// Creates a local row from its chunk-relative ordinal.
60    #[must_use]
61    pub const fn new(value: u32) -> Self {
62        Self(value)
63    }
64
65    /// Returns the chunk-relative ordinal.
66    #[must_use]
67    pub const fn get(self) -> u32 {
68        self.0
69    }
70
71    /// Returns the next local row, if representable.
72    #[must_use]
73    pub const fn next(self) -> Option<Self> {
74        match self.0.checked_add(1) {
75            Some(value) => Some(Self(value)),
76            None => None,
77        }
78    }
79}
80
81impl fmt::Debug for LocalRow {
82    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
83        write!(formatter, "local row({})", self.0)
84    }
85}
86
87impl fmt::Display for LocalRow {
88    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(formatter, "{}", self.0)
90    }
91}