1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
os::raw::c_char,
result::Result as StdResult,
};
/**
* The error type
*/
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
/**
* Failed to initialize object
*/
FailedInit,
/**
* Data size mismatched
*/
MismatchSize,
/**
* Invalid argument
*/
InvalidArg,
}
impl StdError for Error {}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
use self::Error::*;
match self {
FailedInit => "creation error".fmt(f),
MismatchSize => "data size mismatch".fmt(f),
InvalidArg => "invalid argument".fmt(f),
}
}
}
/**
* The alias for result type with payload
*/
pub type Result<T> = StdResult<T, Error>;
/**
* The alias for rusult type without payload
*/
pub type Status = Result<()>;
/**
* The trait for null-terminated string constants
*/
pub trait AsNativeStr {
/**
* Implementations should return strings ended with '\0'
* (for ex.: `"energy\0"`)
*/
fn as_native_str(&self) -> &'static str;
/**
* Get constant as null-terminated C-string
*/
fn as_native_cstr(&self) -> *const c_char {
self.as_native_str().as_ptr() as *const _
}
/**
* Get constant as rust string slice
*/
fn as_rust_str(&self) -> &'static str {
let nt_str = self.as_native_str();
&nt_str[..nt_str.len() - 1]
}
}
pub(crate) fn check_init<T>(ptr: *mut T) -> Status {
if ptr.is_null() {
Err(Error::FailedInit)
} else {
Ok(())
}
}