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§
impl Freeze for Category
impl RefUnwindSafe for Category
impl Send for Category
impl Sync for Category
impl Unpin for Category
impl UnwindSafe for Category
Blanket Implementations§
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
Mutably borrows from an owned value. Read more