Struct ErrorData

Source
pub struct ErrorData { /* private fields */ }
Expand description

The entire data of the error and its error code chain.

This is a wrapper over a bit-packed u32 value that contains five 4-bit wide ErrorCodes and four 3-bit wide ErrorCodeFormatter indices.

The bit layout of the underlying u32 value is a follows:

  • Bits b0..b20 contain 5 error codes, each error code is 4 bits.
    • b0..b4: the error code of the current error (returned by code())
    • b4..b8: chained error code 0
    • b8..b12: chained error code 1
    • b12..b16: chained error code 2
    • b16..b20: chained error code 3
  • Bits b20..b32 contain 4 formatter indices, each index has 3 bits.
    • b20..b23: formatter index + 1 of chained error 0 (0 means not present) (returned by first_formatter_index())
    • b23..b26: formatter index + 1 of chained error 1 (0 means not present)
    • b26..b29: formatter index + 1 of chained error 2 (0 means not present)
    • b29..b32: formatter index + 1 of chained error 3 (0 means not present)

The first error code represents the most recent or current error. The next four error codes with the formatter indices represent the error chain which can be empty. The error chain (as described in the documentation of Error) is a singly linked list. As much of the data used for error reporting is constant or static, so that no dynamic allocation is needed, to make runtime memory usage as small as possible and to make it cheap to copy an error value around. This is also the case with the error chain.

Every ErrorCode value belongs to a type that implements the trait ErrorCategory. Using this trait it is possible to print a custom name and additional information for every ErrorCode value. Only the ErrorCategory of the most recent error code has to be known, all other error categories can then be retrieved by iterating over the linked list. The ErrorCategory is also needed for the linked list to be possible.

Every formatter index in the chain represents the ErrorCodeFormatter function of the error category and error code. A formatter function is retrieved by calling the formatter function of the previous error code, and passing it the index of the next formatter function. The called formatter function gets the next formatter function from from the slice returned by ErrorCategory::chainable_category_formatters() using the next formatter index. This is only possible if the ErrorCategory associated with the called formatter function is linked to the ErrorCategory of the next error code in the chain.

An error category A is linked to an error category B if at least one of the A::L1 to A::L5 associated types is B and the nth element (where n is the digit of the A::Ln associated type used) of the slice returned by A::chainable_category_formatters() is the ErrorCodeFormatter function for B.

Implementations§

Source§

impl ErrorData

Source

pub const fn new(error_code: ErrorCode) -> ErrorData

Create new ErrorData that contains the supplied error_code and has an empty chain.

Source

pub fn set_code(&mut self, code: ErrorCode) -> ErrorCode

Replace the error code with code and return the old one.

Note: That the categories of the new error code and the old must be the same.

Source

pub fn code(&self) -> ErrorCode

Get the most recent error code of the error.

Source

pub fn first_formatter_index(&self) -> Option<u8>

Get the first formatter index in the chain if available.

Source

pub fn chain_len(&self) -> usize

Get the number of chained error codes.

Source

pub fn chain_full(&self) -> bool

Whether the error chain is full.

Source

pub fn push_front( &mut self, error_code: ErrorCode, category_index: u8, ) -> Option<(ErrorCode, u8)>

Prepend the current error code to the front of the error chain and set the current error code to error_code.

Returns the back of the error chain before modification if it gets overwritten by this operation (when the chain overflows).

Note: error_code is masked to the first 4 bits and category_index is masked to the first 3 bits.

Source

pub fn chain(&mut self, error_code: ErrorCode, category_index: u8)

Chain this error with a new error specified by error_code.

This prepends the current error code to the front of the error chain and sets error_code as the new current error code.

§Panics

If the feature panic-on-overflow is enabled and the error chain is already full before this operation, this function will panic. If the feature is not enabled and the error chain is already full, the last error in the chain will be lost.

Trait Implementations§

Source§

impl Clone for ErrorData

Source§

fn clone(&self) -> ErrorData

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<C: ErrorCategory> From<Error<C>> for ErrorData

Source§

fn from(error: Error<C>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ErrorData

Source§

fn eq(&self, other: &ErrorData) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Copy for ErrorData

Source§

impl Eq for ErrorData

Source§

impl StructuralPartialEq for ErrorData

Auto Trait Implementations§

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> 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<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, 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.