rarena_allocator/
error.rs

1pub use dbutils::error::*;
2
3#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
4#[derive(Debug)]
5pub(crate) struct MagicVersionMismatch {
6  expected_version: u16,
7  found_version: u16,
8}
9
10#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
11impl MagicVersionMismatch {
12  #[inline]
13  pub(crate) const fn new(expected_version: u16, found_version: u16) -> Self {
14    Self {
15      expected_version,
16      found_version,
17    }
18  }
19}
20
21#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
22impl core::fmt::Display for MagicVersionMismatch {
23  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24    write!(
25      f,
26      "ARENA's magic version mismatch: expected version {}, but found version {}.",
27      self.expected_version, self.found_version
28    )
29  }
30}
31
32#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
33impl core::error::Error for MagicVersionMismatch {}
34
35#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
36#[derive(Debug)]
37pub(crate) struct VersionMismatch {
38  expected_version: u16,
39  found_version: u16,
40}
41
42#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
43impl VersionMismatch {
44  #[inline]
45  pub(crate) const fn new(expected_version: u16, found_version: u16) -> Self {
46    Self {
47      expected_version,
48      found_version,
49    }
50  }
51}
52
53#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
54impl core::fmt::Display for VersionMismatch {
55  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
56    write!(
57      f,
58      "ARENA's version mismatch: expected version {}, but found version {}.",
59      self.expected_version, self.found_version
60    )
61  }
62}
63
64#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
65impl core::error::Error for VersionMismatch {}
66
67/// An error indicating that the arena is full
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub enum Error {
70  /// Insufficient space in the arena
71  InsufficientSpace {
72    /// The requested size
73    requested: u32,
74    /// The remaining size
75    available: u32,
76  },
77  /// The arena is read-only
78  ReadOnly,
79
80  // /// The requested size is larger than the page size
81  // LargerThanPageSize {
82  //   /// The requested size
83  //   requested: u32,
84  //   /// The page size
85  //   page_size: u32,
86  // },
87  /// Index is out of range
88  OutOfBounds {
89    /// The offset
90    offset: usize,
91    /// The current allocated size of the arena
92    allocated: usize,
93  },
94
95  /// Returned when decoding a LEB128 value fails
96  DecodeVarintError(dbutils::leb128::DecodeVarintError),
97}
98
99impl From<dbutils::leb128::DecodeVarintError> for Error {
100  #[inline]
101  fn from(e: dbutils::leb128::DecodeVarintError) -> Self {
102    Self::DecodeVarintError(e)
103  }
104}
105
106// impl Error {
107//   #[cfg(all(feature = "memmap", not(target_family = "wasm")))]
108//   #[inline]
109//   pub(crate) const fn larger_than_page_size(requested: u32, page_size: u32) -> Self {
110//     Self::LargerThanPageSize {
111//       requested,
112//       page_size,
113//     }
114//   }
115// }
116
117impl core::fmt::Display for Error {
118  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
119    match self {
120      Self::InsufficientSpace {
121        requested,
122        available,
123      } => write!(
124        f,
125        "Allocation failed: requested size is {}, but only {} is available",
126        requested, available
127      ),
128      Self::ReadOnly => write!(f, "Arena is read-only"),
129      // Self::LargerThanPageSize {
130      //   requested,
131      //   page_size,
132      // } => write!(
133      //   f,
134      //   "Allocation failed: cannot allocate in the same page, requested size is {}, but the page size is {}",
135      //   requested, page_size
136      // ),
137      Self::OutOfBounds { offset, allocated } => write!(
138        f,
139        "Index out of bounds: offset {} is out of range, the current allocated size is {}",
140        offset, allocated
141      ),
142      Self::DecodeVarintError(e) => e.fmt(f),
143    }
144  }
145}
146
147#[cfg(feature = "std")]
148impl std::error::Error for Error {}