1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2024-2026 Gabriel Bjørnager Jensen.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you
// can obtain one at:
// <https://mozilla.org/MPL/2.0/>.
//! The [`SimpleError`] error type.
use crate::io::ErrorKind;
use core::fmt::{self, Display, Formatter};
// NOTE: Alignement ensures some padding bits in
// addresses.
/// An error kind with an error message.
///
/// Objects of this type are intended to be
/// allocated in static memory.
#[repr(align(4))]
#[derive(Clone, Copy, Debug)]
pub struct SimpleError {
/// The error kind.
kind: ErrorKind,
/// The error message.
message: &'static str,
}
impl SimpleError {
/// Constructs a new, simple input/output error.
#[inline]
#[must_use]
pub const fn new(kind: ErrorKind, message: &'static str) -> Self {
Self { kind, message }
}
/// Retrieves the kind of error.
#[inline(always)]
#[must_use]
pub const fn kind(&self) -> ErrorKind {
self.kind
}
/// Retrieves the error message.
#[inline(always)]
#[must_use]
pub const fn message(&self) -> &'static str {
self.message
}
}
impl Display for SimpleError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.kind, self.message)?;
Ok(())
}
}
impl core::error::Error for SimpleError {}