redoubt_buffer/
error.rs

1// Copyright (c) 2025-2026 Federico Hoerth <memparanoid@gmail.com>
2// SPDX-License-Identifier: GPL-3.0-only
3// See LICENSE in the repository root for full license text.
4
5//! Error types for redoubt-buffer.
6use alloc::boxed::Box;
7use thiserror::Error;
8
9/// Errors from page syscalls.
10#[derive(Debug, Error, Clone, Copy, Eq, PartialEq)]
11#[repr(u8)]
12pub enum PageError {
13    #[error("mmap failed")]
14    Create = 0,
15
16    #[error("mlock failed")]
17    Lock = 1,
18
19    #[error("mprotect(PROT_NONE) failed")]
20    Protect = 2,
21
22    #[error("mprotect(PROT_WRITE) failed")]
23    Unprotect = 3,
24
25    #[error("madvise(MADV_DONTDUMP) failed")]
26    Madvise = 4,
27}
28
29/// Errors that can occur when working with buffers.
30#[derive(Debug, Error)]
31pub enum BufferError {
32    /// An error occurred during a page operation.
33    #[error("PageError: {0}")]
34    Page(#[from] PageError),
35
36    /// The page is no longer available.
37    #[error("page is no longer available")]
38    PageNoLongerAvailable,
39
40    /// An error occurred in a callback function.
41    #[error("callback error: {0:?}")]
42    CallbackError(Box<dyn core::fmt::Debug + Send + Sync + 'static>),
43
44    /// A mutex was poisoned.
45    #[error("mutex poisoned")]
46    MutexPoisoned,
47}
48
49impl BufferError {
50    /// Creates a CallbackError from any Debug + Send + Sync error.
51    pub fn callback_error<E: core::fmt::Debug + Send + Sync + 'static>(e: E) -> Self {
52        Self::CallbackError(Box::new(e))
53    }
54}