mod error {
use rknpu_sys::rknn::error::*;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Errno(i32);
impl Errno {
pub fn is_ok(&self) -> bool {
self.0 == RKNN_SUCC as i32
}
pub fn is_err(&self) -> bool {
!self.is_ok()
}
pub fn as_i32(&self) -> i32 {
self.0
}
}
impl std::fmt::Display for Errno {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
const SUCC: i32 = RKNN_SUCC as i32;
match self.0 {
SUCC => write!(f, "ok"),
RKNN_ERR_FAIL => write!(f, "general failure"),
RKNN_ERR_TIMEOUT => write!(f, "timeout"),
RKNN_ERR_CTX_INVALID => write!(f, "invalid context"),
RKNN_ERR_MALLOC_FAIL => write!(f, "malloc failure"),
RKNN_ERR_INPUT_INVALID => write!(f, "invalid input"),
RKNN_ERR_MODEL_INVALID => write!(f, "invalid model"),
RKNN_ERR_PARAM_INVALID => write!(f, "invalid parameter"),
RKNN_ERR_DEVICE_UNMATCH => write!(f, "device unmatch"),
RKNN_ERR_OUTPUT_INVALID => write!(f, "invalid output"),
RKNN_ERR_DEVICE_UNAVAILABLE => write!(f, "device is not available"),
RKNN_ERR_INCOMPATILE_PRE_COMPILE_MODEL => write!(f, "incompatible pre-compiled model"),
RKNN_ERR_INCOMPATILE_OPTIMIZATION_LEVEL_VERSION => write!(f, "incompatible optimization level version"),
errno => write!(f, "unknown error {}", errno),
}
}
}
impl std::error::Error for Errno {}
impl From<std::ffi::c_int> for Errno {
fn from(value: std::ffi::c_int) -> Self {
Self(value)
}
}
impl From<Errno> for std::ffi::c_int {
fn from(value: Errno) -> Self {
value.0 as std::ffi::c_int
}
}
#[derive(Debug)]
pub enum Error {
EmptyModel,
AllocationFailure,
Npu(Errno),
Io(std::io::Error),
}
impl Error {
pub fn is_npu(&self) -> bool {
matches!(self, Self::Npu(_))
}
pub fn is_io(&self) -> bool {
matches!(self, Self::Io(_))
}
pub fn npu_errno(&self) -> Option<Errno> {
match self {
Self::Npu(e) => Some(*e),
_ => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::EmptyModel => write!(f, "empty model"),
Self::AllocationFailure => write!(f, "allocation failure"),
Self::Npu(e) => write!(f, "npu runtime error: {}", e),
Self::Io(e) => write!(f, "io error: {}", e),
}
}
}
impl std::error::Error for Error {}
impl From<Errno> for Error {
fn from(value: Errno) -> Self {
Self::Npu(value)
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
pub fn rknn_code_to_result(code: std::ffi::c_int) -> Result<(), Errno> {
let errno: Errno = code.into();
if errno.is_ok() {
Ok(())
} else {
Err(errno)
}
}
}
pub use error::Error;
pub mod rknn;