Skip to main content

AnErr

Struct AnErr 

Source
pub struct AnErr<K, const REASON_LEN: usize = 31>
where K: Copy + Clone + Debug + PartialEq + Eq,
{ 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: K

The 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>
where K: Copy + Clone + Debug + PartialEq + Eq,

Source

pub fn new(kind: K) -> Self

Creates a new error with the given kind and empty reason.

Source

pub fn with_reason(kind: K, reason: LiteStr<REASON_LEN>) -> Self

Creates a new error with the given kind and reason.

Source

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.

Source

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.

Source

pub fn context_fmt(&mut self, args: Arguments<'_>)

Appends context using a formatted reason string.

Source

pub fn kind(&self) -> K

Returns the current error kind.

Source

pub fn reason(&self) -> &LiteStr<REASON_LEN>

Returns the accumulated reason.

Source§

impl<K, const REASON_LEN: usize> AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq,

Source

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.

Source

pub const fn wire_size() -> usize

Returns the exact size (in bytes) of the wire representation.

Source

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§

Source§

impl<K, const REASON_LEN: usize> Clone for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq + Clone,

Source§

fn clone(&self) -> AnErr<K, REASON_LEN>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, const REASON_LEN: usize> Copy for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq + Copy,

Source§

impl<K, const REASON_LEN: usize> Debug for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, const REASON_LEN: usize> Display for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, const REASON_LEN: usize> Eq for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq + Eq,

Source§

impl<K, const REASON_LEN: usize> Error for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq,

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<K, const REASON_LEN: usize> From<K> for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq,

Source§

fn from(kind: K) -> Self

Converts to this type from the input type.
Source§

impl<K, const REASON_LEN: usize> PartialEq for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq + PartialEq,

Source§

fn eq(&self, other: &AnErr<K, REASON_LEN>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, const REASON_LEN: usize> StructuralPartialEq for AnErr<K, REASON_LEN>
where K: Copy + Clone + Debug + PartialEq + Eq + PartialEq,

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsErrorSource for T
where T: Error + 'static,

Source§

fn as_error_source(&self) -> &(dyn Error + 'static)

For maximum effectiveness, this needs to be called as a method to benefit from Rust’s automatic dereferencing of method receivers.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more