Skip to main content

murk_arena/
error.rs

1//! Arena-specific error types.
2
3use std::error::Error;
4use std::fmt;
5
6use murk_core::FieldId;
7
8/// Errors that can occur during arena operations.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum ArenaError {
11    /// Segment pool is full — no more segments can be allocated.
12    CapacityExceeded {
13        /// Number of bytes requested.
14        requested: usize,
15        /// Total capacity available across all segments.
16        capacity: usize,
17    },
18    /// A `FieldHandle` from a generation that has been reclaimed.
19    StaleHandle {
20        /// The generation encoded in the handle.
21        handle_generation: u32,
22        /// The oldest live generation in the arena.
23        oldest_live: u32,
24    },
25    /// A `FieldId` that is not registered in the arena.
26    UnknownField {
27        /// The unrecognised field.
28        field: FieldId,
29    },
30    /// Attempted to write a field whose mutability does not permit writes
31    /// in the current context (e.g. writing a `Static` field after init).
32    NotWritable {
33        /// The field that was not writable.
34        field: FieldId,
35    },
36    /// Arena configuration is invalid.
37    InvalidConfig {
38        /// What is wrong with the configuration.
39        reason: String,
40    },
41}
42
43impl fmt::Display for ArenaError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::CapacityExceeded {
47                requested,
48                capacity,
49            } => {
50                write!(
51                    f,
52                    "arena capacity exceeded: requested {requested} bytes, capacity {capacity} bytes"
53                )
54            }
55            Self::StaleHandle {
56                handle_generation,
57                oldest_live,
58            } => {
59                write!(
60                    f,
61                    "stale handle: generation {handle_generation}, oldest live {oldest_live}"
62                )
63            }
64            Self::UnknownField { field } => {
65                write!(f, "unknown field: {field}")
66            }
67            Self::NotWritable { field } => {
68                write!(f, "field {field} is not writable in this context")
69            }
70            Self::InvalidConfig { reason } => {
71                write!(f, "invalid arena config: {reason}")
72            }
73        }
74    }
75}
76
77impl Error for ArenaError {}