rarena_allocator/
error.rs1pub 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#[derive(Debug, Clone, PartialEq, Eq)]
69pub enum Error {
70 InsufficientSpace {
72 requested: u32,
74 available: u32,
76 },
77 ReadOnly,
79
80 OutOfBounds {
89 offset: usize,
91 allocated: usize,
93 },
94
95 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
106impl 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::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 {}