Struct error_code::Category

source ·
pub struct Category {
    pub name: &'static str,
    pub message: fn(_: c_int, _: &mut MessageBuf) -> &str,
    pub equivalent: fn(_: c_int, _: &ErrorCode) -> bool,
    pub is_would_block: fn(_: c_int) -> bool,
}
Expand description

Interface for error category

It is implemented as pointers in order to avoid generics or overhead of fat pointers.

§Custom implementation example

use error_code::{ErrorCode, Category};
use error_code::types::c_int;

use core::ptr;

static MY_CATEGORY: Category = Category {
    name: "MyError",
    message,
    equivalent,
    is_would_block
};

fn equivalent(code: c_int, other: &ErrorCode) -> bool {
    ptr::eq(&MY_CATEGORY, other.category()) && code == other.raw_code()
}

fn is_would_block(_: c_int) -> bool {
    false
}

fn message(code: c_int, out: &mut error_code::MessageBuf) -> &str {
    let msg = match code {
        0 => "Success",
        1 => "Bad",
        _ => "Whatever",
    };

    debug_assert!(msg.len() <= out.len());
    unsafe {
        ptr::copy_nonoverlapping(msg.as_ptr(), out.as_mut_ptr() as *mut u8, msg.len())
    }
    msg
}

#[inline(always)]
pub fn my_error(code: c_int) -> ErrorCode {
    ErrorCode::new(code, &MY_CATEGORY)
}

Fields§

§name: &'static str

Category name

§message: fn(_: c_int, _: &mut MessageBuf) -> &str

Maps error code and writes descriptive error message accordingly.

In case of insufficient buffer, prefer to truncate message or just don’t write big ass message.

In case of error, just write generic name.

Returns formatted message as string.

§equivalent: fn(_: c_int, _: &ErrorCode) -> bool

Checks whether error code is equivalent to another one.

§Args:

  • Raw error code, belonging to this category
  • Another error code being compared against this category.

§Recommendation

Generally error code is equal if it belongs to the same category (use ptr::eq to compare pointers to Category) and raw error codes are equal.

§is_would_block: fn(_: c_int) -> bool

Returns true if supplied error code indicates WouldBlock like error.

This should true only for errors that indicate operation can be re-tried later.

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