pub struct AnErr<K, const REASON_LEN: usize = 31>{
pub kind: K,
pub reason: LiteStr<REASON_LEN>,
}Expand description
A compact, Copy, zero-allocation error type consisting of a single
error kind and a human-readable reason string.
When context is added via context, context_fmt, or the => form of
an_err!, the new reason text is appended to the existing reason.
The total is silently truncated to REASON_LEN
bytes if necessary.
Fields§
§kind: KThe error kind.
reason: LiteStr<REASON_LEN>Accumulated reason string (controlled by REASON_LEN).
Can be empty.
Implementations§
Source§impl<K, const REASON_LEN: usize> AnErr<K, REASON_LEN>
impl<K, const REASON_LEN: usize> AnErr<K, REASON_LEN>
Sourcepub fn with_reason(kind: K, reason: LiteStr<REASON_LEN>) -> Self
pub fn with_reason(kind: K, reason: LiteStr<REASON_LEN>) -> Self
Creates a new error with the given kind and reason.
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 text is truncated if it exceeds REASON_LEN bytes.
Sourcepub fn context(&mut self, new_reason: LiteStr<REASON_LEN>)
pub fn context(&mut self, new_reason: LiteStr<REASON_LEN>)
Appends context by appending the given reason text to the accumulated
reason. Truncates if the total would exceed REASON_LEN bytes.
Sourcepub fn context_fmt(&mut self, args: Arguments<'_>)
pub fn context_fmt(&mut self, args: Arguments<'_>)
Appends context using a formatted reason string.
Source§impl<K, const REASON_LEN: usize> AnErr<K, REASON_LEN>
impl<K, const REASON_LEN: usize> AnErr<K, REASON_LEN>
Sourcepub fn to_wire_bytes(
&self,
kind_to_u16: impl Fn(K) -> u16,
buf: &mut [u8],
) -> Result<usize, ()>
pub fn to_wire_bytes( &self, kind_to_u16: impl Fn(K) -> u16, buf: &mut [u8], ) -> Result<usize, ()>
Serialize this error to a fixed-size byte buffer for transmission.
The provided buffer must be at least Self::wire_size() bytes long.
Returns the number of bytes written.
Sourcepub const fn wire_size() -> usize
pub const fn wire_size() -> usize
Returns the exact size (in bytes) of the wire representation.
Sourcepub fn from_wire_bytes(
bytes: &[u8],
u16_to_kind: impl Fn(u16) -> Option<K>,
) -> Option<Self>
pub fn from_wire_bytes( bytes: &[u8], u16_to_kind: impl Fn(u16) -> Option<K>, ) -> Option<Self>
Deserialize from a wire buffer directly into an AnErr.
Requires a closure that maps the stored u16 back to your concrete K.
Returns None on corruption, wrong size, unknown version, or mapping failure.
Trait Implementations§
impl<K, const REASON_LEN: usize> Copy for AnErr<K, REASON_LEN>
impl<K, const REASON_LEN: usize> Eq for AnErr<K, REASON_LEN>
Source§impl<K, const REASON_LEN: usize> Error for AnErr<K, REASON_LEN>
impl<K, const REASON_LEN: usize> Error for AnErr<K, 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()
Source§impl<K, const REASON_LEN: usize> PartialEq for AnErr<K, REASON_LEN>
impl<K, const REASON_LEN: usize> PartialEq for AnErr<K, REASON_LEN>
impl<K, const REASON_LEN: usize> StructuralPartialEq for AnErr<K, REASON_LEN>
Auto Trait Implementations§
impl<K, const REASON_LEN: usize> Freeze for AnErr<K, REASON_LEN>where
K: Freeze,
impl<K, const REASON_LEN: usize> RefUnwindSafe for AnErr<K, REASON_LEN>where
K: RefUnwindSafe,
impl<K, const REASON_LEN: usize> Send for AnErr<K, REASON_LEN>where
K: Send,
impl<K, const REASON_LEN: usize> Sync for AnErr<K, REASON_LEN>where
K: Sync,
impl<K, const REASON_LEN: usize> Unpin for AnErr<K, REASON_LEN>where
K: Unpin,
impl<K, const REASON_LEN: usize> UnsafeUnpin for AnErr<K, REASON_LEN>where
K: UnsafeUnpin,
impl<K, const REASON_LEN: usize> UnwindSafe for AnErr<K, REASON_LEN>where
K: UnwindSafe,
Blanket Implementations§
Source§impl<T> AsErrorSource for Twhere
T: Error + 'static,
impl<T> AsErrorSource for Twhere
T: Error + 'static,
Source§fn as_error_source(&self) -> &(dyn Error + 'static)
fn as_error_source(&self) -> &(dyn Error + 'static)
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.