1use std::fmt;
2
3use crate::ffi;
4
5#[derive(Debug)]
7pub enum Error {
8 Io(std::io::Error),
10 Cuda {
12 call: &'static str,
14 code: i32,
16 message: String,
18 },
19 TensorRt {
21 call: &'static str,
23 message: String,
25 },
26 UnknownTensor {
28 name: String,
30 available: Vec<String>,
32 },
33 UnsupportedDataType {
35 tensor: String,
37 code: i32,
39 },
40 DynamicShape {
42 tensor: String,
44 shape: Vec<i64>,
46 },
47 SizeMismatch {
49 tensor: String,
51 expected: usize,
53 found: usize,
55 },
56 TooManyDimensions {
58 found: usize,
60 max: usize,
62 },
63 InvalidTensorName(String),
65}
66
67impl fmt::Display for Error {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 match self {
70 Error::Io(source) => write!(f, "io: {source}"),
71 Error::Cuda {
72 call,
73 code,
74 message,
75 } => write!(f, "cuda: {call} failed ({code}): {message}"),
76 Error::TensorRt { call, message } => {
77 if message.is_empty() {
78 write!(f, "tensorrt: {call} failed")
79 } else {
80 write!(f, "tensorrt: {call} failed: {message}")
81 }
82 }
83 Error::UnknownTensor { name, available } => {
84 let names: Vec<&str> = available.iter().map(String::as_str).collect();
85 write!(
86 f,
87 "no tensor named {name:?}; the engine has {}",
88 names.join(", ")
89 )
90 }
91 Error::UnsupportedDataType { tensor, code } => {
92 write!(
93 f,
94 "tensor {tensor:?} has data type code {code}, which this crate cannot size"
95 )
96 }
97 Error::DynamicShape { tensor, shape } => {
98 write!(
99 f,
100 "tensor {tensor:?} has dynamic dimensions {shape:?}; set an input shape first"
101 )
102 }
103 Error::SizeMismatch {
104 tensor,
105 expected,
106 found,
107 } => {
108 write!(
109 f,
110 "tensor {tensor:?} needs {expected} bytes, the buffer has {found}"
111 )
112 }
113 Error::TooManyDimensions { found, max } => {
114 write!(
115 f,
116 "shape has {found} dimensions, TensorRT allows at most {max}"
117 )
118 }
119 Error::InvalidTensorName(name) => write!(f, "tensor name {name:?} contains a NUL byte"),
120 }
121 }
122}
123
124impl std::error::Error for Error {
125 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126 match self {
127 Error::Io(source) => Some(source),
128 _ => None,
129 }
130 }
131}
132
133impl From<std::io::Error> for Error {
134 fn from(source: std::io::Error) -> Self {
135 Error::Io(source)
136 }
137}
138
139pub(crate) fn last_message() -> String {
142 let raw = unsafe { ffi::trt_last_error() };
143 if raw.is_null() {
144 return String::new();
145 }
146 unsafe { std::ffi::CStr::from_ptr(raw) }
147 .to_string_lossy()
148 .into_owned()
149}
150
151pub(crate) fn cuda_check(code: i32, call: &'static str) -> Result<(), Error> {
152 if code == 0 {
153 return Ok(());
154 }
155 let message = last_message();
156 Err(Error::Cuda {
157 call,
158 code,
159 message,
160 })
161}
162
163pub(crate) fn trt_check(code: i32, call: &'static str) -> Result<(), Error> {
164 if code == 0 {
165 return Ok(());
166 }
167 Err(Error::TensorRt {
168 call,
169 message: last_message(),
170 })
171}