rknpu/
lib.rs

1//! # rknpu
2//!
3//! Rockchip NPU library
4
5mod error {
6    use rknpu_sys::rknn::error::*;
7
8    /// RKNPU library error code
9    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
10    pub struct Errno(i32);
11
12    impl Errno {
13        pub fn is_ok(&self) -> bool {
14            self.0 == RKNN_SUCC as i32
15        }
16
17        pub fn is_err(&self) -> bool {
18            !self.is_ok()
19        }
20
21        pub fn as_i32(&self) -> i32 {
22            self.0
23        }
24    }
25
26    impl std::fmt::Display for Errno {
27        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28            const SUCC: i32 = RKNN_SUCC as i32;
29            match self.0 {
30                SUCC => write!(f, "ok"),
31                RKNN_ERR_FAIL => write!(f, "general failure"),
32                RKNN_ERR_TIMEOUT => write!(f, "timeout"),
33                RKNN_ERR_CTX_INVALID => write!(f, "invalid context"),
34                RKNN_ERR_MALLOC_FAIL => write!(f, "malloc failure"),
35                RKNN_ERR_INPUT_INVALID => write!(f, "invalid input"),
36                RKNN_ERR_MODEL_INVALID => write!(f, "invalid model"),
37                RKNN_ERR_PARAM_INVALID => write!(f, "invalid parameter"),
38                RKNN_ERR_DEVICE_UNMATCH => write!(f, "device unmatch"),
39                RKNN_ERR_OUTPUT_INVALID => write!(f, "invalid output"),
40                RKNN_ERR_DEVICE_UNAVAILABLE => write!(f, "device is not available"),
41                RKNN_ERR_INCOMPATILE_PRE_COMPILE_MODEL => write!(f, "incompatible pre-compiled model"),
42                RKNN_ERR_INCOMPATILE_OPTIMIZATION_LEVEL_VERSION => write!(f, "incompatible optimization level version"),
43                errno => write!(f, "unknown error {}", errno),
44            }
45        }
46    }
47
48    impl std::error::Error for Errno {}
49
50    impl From<std::ffi::c_int> for Errno {
51        fn from(value: std::ffi::c_int) -> Self {
52            Self(value)
53        }
54    }
55
56    impl From<Errno> for std::ffi::c_int {
57        fn from(value: Errno) -> Self {
58            value.0 as std::ffi::c_int
59        }
60    }
61
62    /// Error
63    #[derive(Debug)]
64    pub enum Error {
65        /// A model with size 0 is passed in.
66        EmptyModel,
67
68        /// Allocation failure (the returned pointer is NULL)
69        AllocationFailure,
70
71        /// RKNPU library error
72        Npu(Errno),
73
74        /// I/O error
75        Io(std::io::Error),
76    }
77
78    impl Error {
79        pub fn is_npu(&self) -> bool {
80            matches!(self, Self::Npu(_))
81        }
82
83        pub fn is_io(&self) -> bool {
84            matches!(self, Self::Io(_))
85        }
86
87        pub fn npu_errno(&self) -> Option<Errno> {
88            match self {
89                Self::Npu(e) => Some(*e),
90                _ => None,
91            }
92        }
93    }
94
95    impl std::fmt::Display for Error {
96        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97            match self {
98                Self::EmptyModel => write!(f, "empty model"),
99                Self::AllocationFailure => write!(f, "allocation failure"),
100                Self::Npu(e) => write!(f, "npu runtime error: {}", e),
101                Self::Io(e) => write!(f, "io error: {}", e),
102            }
103        }
104    }
105
106    impl std::error::Error for Error {}
107
108    impl From<Errno> for Error {
109        fn from(value: Errno) -> Self {
110            Self::Npu(value)
111        }
112    }
113
114    impl From<std::io::Error> for Error {
115        fn from(value: std::io::Error) -> Self {
116            Self::Io(value)
117        }
118    }
119
120    pub fn rknn_code_to_result(code: std::ffi::c_int) -> Result<(), Errno> {
121        let errno: Errno = code.into();
122        if errno.is_ok() {
123            Ok(())
124        } else {
125            Err(errno)
126        }
127    }
128}
129
130pub use error::Error;
131
132pub mod rknn;