plugmem_arena/error.rs
1//! The crate-wide error type.
2//!
3//! All four storage structures share one error enum: capacity problems are
4//! always typed errors, never panics, and a caller embedding several
5//! structures (the plugmem engine does) handles one type.
6
7use crate::arena::PAGE_BYTES;
8use crate::chunk::CHUNK_PAYLOAD;
9
10/// Errors returned by storage operations. No operation in this crate panics
11/// on resource exhaustion — capacity problems are always typed errors.
12/// (Contract violations — a wrong key length, an out-of-range id — panic,
13/// because they are caller bugs, not runtime conditions; each method
14/// documents its panics.)
15#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize))]
17pub enum Error {
18 /// Growing a byte pool would exceed the structure's configured
19 /// `max_bytes` ceiling.
20 #[error("capacity exceeded: pool would grow past {max_bytes} bytes")]
21 CapacityExceeded {
22 /// The configured ceiling that would have been crossed.
23 max_bytes: usize,
24 },
25 /// The [`Slot`](crate::Slot) layout constants are invalid.
26 #[error(
27 "invalid slot layout: size {size}, key_len {key_len} (require 1 <= key_len <= size <= {PAGE_BYTES})"
28 )]
29 BadSlot {
30 /// Declared [`Slot::SIZE`](crate::Slot::SIZE).
31 size: usize,
32 /// Declared [`Slot::KEY_LEN`](crate::Slot::KEY_LEN).
33 key_len: usize,
34 },
35 /// [`ArenaCfg::shards`](crate::ArenaCfg::shards) is not a non-zero power
36 /// of two.
37 #[error("shard count must be a non-zero power of two, got {got}")]
38 BadShardCount {
39 /// The rejected shard count.
40 got: usize,
41 },
42 /// A blob passed to [`BlobHeap::push`](crate::BlobHeap::push) is longer
43 /// than the configured [`max_blob`](crate::BlobHeapCfg::max_blob).
44 #[error("blob of {len} bytes exceeds the configured max_blob of {max_blob} bytes")]
45 BlobTooLarge {
46 /// Length of the rejected blob.
47 len: usize,
48 /// The configured per-blob ceiling.
49 max_blob: usize,
50 },
51 /// A value passed to [`ChunkPool::push`](crate::ChunkPool::push) is
52 /// longer than a chunk's payload ([`CHUNK_PAYLOAD`] bytes) and can never
53 /// fit without crossing a chunk boundary.
54 #[error("value of {len} bytes exceeds the chunk payload of {CHUNK_PAYLOAD} bytes")]
55 ValueTooLarge {
56 /// Length of the rejected value.
57 len: usize,
58 },
59 /// A serialized image failed load-time validation (`load` methods).
60 ///
61 /// The message names the violated invariant. Loading never panics on
62 /// arbitrary bytes — every inconsistency maps to this variant.
63 #[error("corrupt image: {0}")]
64 Corrupt(&'static str),
65}