Skip to main content

oxgraph_db/
id.rs

1//! Canonical database identity newtypes.
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7/// Declares one canonical `u64` identifier newtype.
8macro_rules! id_newtype {
9    ($name:ident, $doc:literal) => {
10        #[doc = $doc]
11        ///
12        /// # Performance
13        ///
14        /// Copying, comparing, ordering, and hashing are `O(1)`.
15        #[derive(
16            Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
17        )]
18        #[repr(transparent)]
19        pub struct $name(u64);
20
21        impl $name {
22            /// Creates an identifier from a raw canonical value.
23            ///
24            /// # Performance
25            ///
26            /// This function is `O(1)`.
27            #[must_use]
28            pub const fn new(value: u64) -> Self {
29                Self(value)
30            }
31
32            /// Returns the raw canonical value.
33            ///
34            /// # Performance
35            ///
36            /// This function is `O(1)`.
37            #[must_use]
38            pub const fn get(self) -> u64 {
39                self.0
40            }
41
42            /// Returns the next identifier if it fits.
43            ///
44            /// # Performance
45            ///
46            /// This function is `O(1)`.
47            #[must_use]
48            pub const fn checked_next(self) -> Option<Self> {
49                match self.0.checked_add(1) {
50                    Some(value) => Some(Self(value)),
51                    None => None,
52                }
53            }
54        }
55
56        impl fmt::Display for $name {
57            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
58                write!(formatter, "{}", self.0)
59            }
60        }
61    };
62}
63
64id_newtype!(ElementId, "Stable canonical element identifier.");
65id_newtype!(RelationId, "Stable canonical relation identifier.");
66id_newtype!(IncidenceId, "Stable canonical incidence identifier.");
67id_newtype!(RoleId, "Stable canonical structural role identifier.");
68id_newtype!(LabelId, "Stable catalog label identifier.");
69id_newtype!(RelationTypeId, "Stable catalog relation-type identifier.");
70id_newtype!(PropertyKeyId, "Stable catalog property-key identifier.");
71id_newtype!(ProjectionId, "Stable catalog projection identifier.");
72id_newtype!(IndexId, "Stable catalog index identifier.");
73id_newtype!(CommitSeq, "Monotonic committed transaction sequence.");
74id_newtype!(TransactionId, "Monotonic writer transaction identifier.");
75id_newtype!(
76    CheckpointGeneration,
77    "Immutable checkpoint generation identifier."
78);