pub struct AnErr<K, const DEPTH: usize = 3, const REASON_LEN: usize = 29>{
pub reasons: [Option<AsciiStr<REASON_LEN>>; DEPTH],
pub locations: [Option<&'static Location<'static>>; DEPTH],
pub kinds: [Option<K>; DEPTH],
pub len: u8,
}Expand description
A compact, Copy, zero-allocation error type that records a parallel stack
of error kinds, source locations, and per-level human-readable reasons.
AnErr stores up to DEPTH levels of error context. Each level contains:
- an error kind of type
K, - the source location where the level was created,
- an optional reason specific to that level (
AsciiStr<REASON_LEN>).
The kind enum provides the general error category while the per-level reason carries concrete details (e.g. a bad value, file path, token, etc.).
The type implements Copy and performs no heap allocation. Default memory
footprint is small and fully controllable via the generic parameters.
§Type Parameters
K: Error kind type. Must implementCopy + Clone + Debug + PartialEq + Eq.DEPTH: Maximum number of context levels (default3). Additional context beyond this limit is silently discarded.REASON_LEN: Maximum length of each individual reason in bytes (default29). Longer reasons are silently truncated.
§Construction
use an_error::{AnErr, an_err};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MyKind {
Parse,
Io,
Validation,
}
pub type MyError = AnErr<MyKind, 4, 64>;
fn parse() -> Result<(), MyError> {
Err(an_err!(MyKind::Parse, "unexpected token at byte {}", 42))
}
fn load(path: &str) -> Result<(), MyError> {
let inner = parse()
.map_err(|e| an_err!(MyKind::Io, "while loading config from {}", path => e))?;
Ok(())
}All constructors and the context method capture the call site via #[track_caller].
§Display
The Display implementation produces output of the following form:
--
• Trace (2 levels):
1. Io @ src/io.rs:42:10 while loading config from /etc/foo
2. Parse @ src/parser.rs:17:5 unexpected token at byte 42Each trace level shows its own reason (if present) immediately after the location.
§Invariants
Maintained by all constructors and context:
lenis always in1..=DEPTH.- For every
iin0..len,kinds[i]andlocations[i]areSome. reasons[i]isSomeonly if a non-empty reason was supplied for that level.
Fields§
§reasons: [Option<AsciiStr<REASON_LEN>>; DEPTH]Per-level reasons. Only the first len entries are valid.
None means no reason (or an empty reason) was provided for that level.
locations: [Option<&'static Location<'static>>; DEPTH]Parallel stack of source locations.
Only the first len entries are valid.
kinds: [Option<K>; DEPTH]Parallel stack of error kinds (one per call-stack level).
Only the first len entries are valid.
len: u8Current depth of the error trace (1 = original error).
Implementations§
Source§impl<K, const DEPTH: usize, const REASON_LEN: usize> AnErr<K, DEPTH, REASON_LEN>
impl<K, const DEPTH: usize, const REASON_LEN: usize> AnErr<K, DEPTH, REASON_LEN>
Sourcepub fn with_reason(kind: K, reason: AsciiStr<REASON_LEN>) -> Self
pub fn with_reason(kind: K, reason: AsciiStr<REASON_LEN>) -> Self
Creates a new error with the given kind and reason.
If the reason is empty, it is stored as None.
Sourcepub fn with_fmt(kind: K, args: Arguments<'_>) -> Self
pub fn with_fmt(kind: K, args: Arguments<'_>) -> Self
Creates a new error with the given kind and a formatted reason.
The formatted string is truncated if it exceeds REASON_LEN bytes.
Sourcepub fn context(&mut self, kind: K, new_reason: AsciiStr<REASON_LEN>)
pub fn context(&mut self, kind: K, new_reason: AsciiStr<REASON_LEN>)
Appends a new context level and optional reason to this error.
If new_reason is empty, no reason is stored for the new level.
If the maximum depth is already reached, the call is a no-op.
Sourcepub fn context_fmt(&mut self, kind: K, args: Arguments<'_>)
pub fn context_fmt(&mut self, kind: K, args: Arguments<'_>)
Appends a new context level with a formatted reason.
Used internally by the an_err! macro. The formatted string is
truncated if it exceeds REASON_LEN bytes.
Trait Implementations§
Source§impl<K, const DEPTH: usize, const REASON_LEN: usize> Error for AnErr<K, DEPTH, REASON_LEN>
impl<K, const DEPTH: usize, const REASON_LEN: usize> Error for AnErr<K, DEPTH, REASON_LEN>
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()