bbolt_rs/common/
ids.rs

1use bytemuck::{Pod, Zeroable};
2use std::fmt::{Display, Formatter};
3use std::ops::{Add, AddAssign, Sub, SubAssign};
4
5macro_rules! id {
6
7  (
8    $(#[$meta:meta])*
9    $x:ident
10  ) => {
11
12    $(#[$meta])*
13    #[repr(C)]
14    #[derive(Default, Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Pod, Zeroable)]
15    pub struct $x(pub u64);
16
17    impl Display for $x {
18      fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        write!(f, "{}", self.0)
20      }
21    }
22
23    impl From<u64> for $x {
24      #[inline(always)]
25      fn from(value: u64) -> Self {
26        $x(value)
27      }
28    }
29
30    impl From<$x> for u64 {
31      #[inline(always)]
32      fn from(value: $x) -> Self {
33        value.0
34      }
35    }
36
37    impl Add<u64> for $x {
38      type Output = $x;
39
40      #[inline(always)]
41      fn add(self, rhs: u64) -> Self::Output {
42        $x(self.0 + rhs)
43      }
44    }
45
46    impl Sub<u64> for $x {
47      type Output = $x;
48
49      #[inline(always)]
50      fn sub(self, rhs: u64) -> Self::Output {
51        $x(self.0 - rhs)
52      }
53    }
54
55    impl AddAssign<u64> for $x {
56      #[inline(always)]
57      fn add_assign(&mut self, rhs: u64) {
58        self.0 += rhs;
59      }
60    }
61
62    impl SubAssign<u64> for $x {
63      #[inline(always)]
64      fn sub_assign(&mut self, rhs: u64) {
65        self.0 -= rhs;
66      }
67    }
68
69    impl PartialEq<$x> for u64 {
70      #[inline(always)]
71      fn eq(&self, other: &$x) -> bool {
72        *self == other.0
73      }
74    }
75  };
76}
77
78id!(
79  /// The Page ID. Page address = `PgId` * page_size
80  PgId
81);
82
83/// Create a PgId
84pub(crate) const fn pd(id: u64) -> PgId {
85  PgId(id)
86}
87
88id!(
89  /// The Transaction ID. Monotonic and incremented every commit
90  TxId
91);
92
93/// Create a TxId
94pub(crate) const fn td(id: u64) -> TxId {
95  TxId(id)
96}