rarena_allocator/
error.rs

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
pub use dbutils::error::*;

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[derive(Debug)]
pub(crate) struct MagicVersionMismatch {
  expected_version: u16,
  found_version: u16,
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
impl MagicVersionMismatch {
  #[inline]
  pub(crate) const fn new(expected_version: u16, found_version: u16) -> Self {
    Self {
      expected_version,
      found_version,
    }
  }
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
impl core::fmt::Display for MagicVersionMismatch {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(
      f,
      "ARENA's magic version mismatch: expected version {}, but found version {}.",
      self.expected_version, self.found_version
    )
  }
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
impl core::error::Error for MagicVersionMismatch {}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[derive(Debug)]
pub(crate) struct VersionMismatch {
  expected_version: u16,
  found_version: u16,
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
impl VersionMismatch {
  #[inline]
  pub(crate) const fn new(expected_version: u16, found_version: u16) -> Self {
    Self {
      expected_version,
      found_version,
    }
  }
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
impl core::fmt::Display for VersionMismatch {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(
      f,
      "ARENA's version mismatch: expected version {}, but found version {}.",
      self.expected_version, self.found_version
    )
  }
}

#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
impl core::error::Error for VersionMismatch {}

/// An error indicating that the arena is full
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
  /// Insufficient space in the arena
  InsufficientSpace {
    /// The requested size
    requested: u32,
    /// The remaining size
    available: u32,
  },
  /// The arena is read-only
  ReadOnly,

  // /// The requested size is larger than the page size
  // LargerThanPageSize {
  //   /// The requested size
  //   requested: u32,
  //   /// The page size
  //   page_size: u32,
  // },
  /// Index is out of range
  OutOfBounds {
    /// The offset
    offset: usize,
    /// The current allocated size of the arena
    allocated: usize,
  },

  /// Returned when decoding a LEB128 value fails
  DecodeVarintError(dbutils::leb128::DecodeVarintError),
}

impl From<dbutils::leb128::DecodeVarintError> for Error {
  #[inline]
  fn from(e: dbutils::leb128::DecodeVarintError) -> Self {
    Self::DecodeVarintError(e)
  }
}

// impl Error {
//   #[cfg(all(feature = "memmap", not(target_family = "wasm")))]
//   #[inline]
//   pub(crate) const fn larger_than_page_size(requested: u32, page_size: u32) -> Self {
//     Self::LargerThanPageSize {
//       requested,
//       page_size,
//     }
//   }
// }

impl core::fmt::Display for Error {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self {
      Self::InsufficientSpace {
        requested,
        available,
      } => write!(
        f,
        "Allocation failed: requested size is {}, but only {} is available",
        requested, available
      ),
      Self::ReadOnly => write!(f, "Arena is read-only"),
      // Self::LargerThanPageSize {
      //   requested,
      //   page_size,
      // } => write!(
      //   f,
      //   "Allocation failed: cannot allocate in the same page, requested size is {}, but the page size is {}",
      //   requested, page_size
      // ),
      Self::OutOfBounds { offset, allocated } => write!(
        f,
        "Index out of bounds: offset {} is out of range, the current allocated size is {}",
        offset, allocated
      ),
      Self::DecodeVarintError(e) => e.fmt(f),
    }
  }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}