1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use bytemuck::{Pod, Zeroable};
use std::fmt::{Display, Formatter};
use std::ops::{Add, AddAssign, Sub, SubAssign};

macro_rules! id {

  (
    $(#[$meta:meta])*
    $x:ident
  ) => {

    $(#[$meta])*
    #[repr(C)]
    #[derive(Default, Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Pod, Zeroable)]
    pub struct $x(pub u64);

    impl Display for $x {
      fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
      }
    }

    impl From<u64> for $x {
      #[inline(always)]
      fn from(value: u64) -> Self {
        $x(value)
      }
    }

    impl From<$x> for u64 {
      #[inline(always)]
      fn from(value: $x) -> Self {
        value.0
      }
    }

    impl Add<u64> for $x {
      type Output = $x;

      #[inline(always)]
      fn add(self, rhs: u64) -> Self::Output {
        $x(self.0 + rhs)
      }
    }

    impl Sub<u64> for $x {
      type Output = $x;

      #[inline(always)]
      fn sub(self, rhs: u64) -> Self::Output {
        $x(self.0 - rhs)
      }
    }

    impl AddAssign<u64> for $x {
      #[inline(always)]
      fn add_assign(&mut self, rhs: u64) {
        self.0 += rhs;
      }
    }

    impl SubAssign<u64> for $x {
      #[inline(always)]
      fn sub_assign(&mut self, rhs: u64) {
        self.0 -= rhs;
      }
    }

    impl PartialEq<$x> for u64 {
      #[inline(always)]
      fn eq(&self, other: &$x) -> bool {
        *self == other.0
      }
    }
  };
}

id!(
  /// The Page ID. Page address = `PgId` * page_size
  PgId
);

/// Create a PgId
pub(crate) const fn pd(id: u64) -> PgId {
  PgId(id)
}

id!(
  /// The Transaction ID. Monotonic and incremented every commit
  TxId
);

/// Create a TxId
pub(crate) const fn td(id: u64) -> TxId {
  TxId(id)
}