use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
os::raw::c_char,
result::Result as StdResult,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
FailedInit,
MismatchSize,
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),
}
}
}
pub type Result<T> = StdResult<T, Error>;
pub type Status = Result<()>;
pub trait AsNativeStr {
fn as_native_str(&self) -> &'static str;
fn as_native_cstr(&self) -> *const c_char {
self.as_native_str().as_ptr() as *const _
}
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(())
}
}