1use alloc::string::{FromUtf8Error, FromUtf16Error, String, ToString};
4use core::{
5 ffi::FromBytesWithNulError,
6 fmt,
7 num::{ParseFloatError, ParseIntError},
8 str::Utf8Error,
9};
10
11pub type Result<T, E = Error> = core::result::Result<T, E>;
12
13#[allow(unused)]
14#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
15pub enum Error {
16 TimeoutExpired,
17 ExitRequested,
18 #[default]
19 NotImplemented,
20 PreconditionViolated,
21 HostMemoryExhausted,
22 DeviceMemoryExhausted,
23 SizeInsufficient,
24 Other(String),
25}
26
27#[allow(unused)]
28impl Error {
29 pub fn new(message: &str) -> Self {
30 Self::Other(message.to_string())
31 }
32}
33
34impl fmt::Display for Error {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 match self {
37 Self::TimeoutExpired => write!(f, "Timeout expired"),
38 Self::ExitRequested => write!(f, "Exit requested"),
39 Self::NotImplemented => write!(f, "Not implemented"),
40 Self::PreconditionViolated => write!(f, "Precondition violated"),
41 Self::HostMemoryExhausted => write!(f, "Host memory exhausted"),
42 Self::DeviceMemoryExhausted => write!(f, "Device memory exhausted"),
43 Self::SizeInsufficient => write!(f, "Size insufficient"),
44 Self::Other(message) => write!(f, "{}", message),
45 }
46 }
47}
48
49impl core::error::Error for Error {}
50
51impl From<ParseFloatError> for Error {
52 fn from(error: ParseFloatError) -> Self {
53 Self::Other(error.to_string())
54 }
55}
56
57impl From<ParseIntError> for Error {
58 fn from(error: ParseIntError) -> Self {
59 Self::Other(error.to_string())
60 }
61}
62
63impl From<Utf8Error> for Error {
64 fn from(error: Utf8Error) -> Self {
65 Self::Other(error.to_string())
66 }
67}
68
69impl From<FromUtf8Error> for Error {
70 fn from(error: FromUtf8Error) -> Self {
71 Self::Other(error.to_string())
72 }
73}
74
75impl From<FromUtf16Error> for Error {
76 fn from(error: FromUtf16Error) -> Self {
77 Self::Other(error.to_string())
78 }
79}
80
81impl From<FromBytesWithNulError> for Error {
82 fn from(error: FromBytesWithNulError) -> Self {
83 Self::Other(error.to_string())
84 }
85}
86
87#[cfg(feature = "std")]
88impl From<std::ffi::CString> for Error {
89 fn from(error: std::ffi::CString) -> Self {
90 Self::Other(error.to_string_lossy().to_string())
91 }
92}
93
94#[cfg(feature = "std")]
95impl From<std::ffi::IntoStringError> for Error {
96 fn from(error: std::ffi::IntoStringError) -> Self {
97 Self::Other(error.to_string())
98 }
99}
100
101#[cfg(feature = "std")]
102impl From<std::ffi::NulError> for Error {
103 fn from(error: std::ffi::NulError) -> Self {
104 Self::Other(error.to_string())
105 }
106}
107
108#[cfg(feature = "std")]
109impl From<std::ffi::OsString> for Error {
110 fn from(error: std::ffi::OsString) -> Self {
111 Self::Other(error.to_string_lossy().to_string())
112 }
113}
114
115#[cfg(feature = "std")]
116impl From<std::io::Error> for Error {
117 fn from(error: std::io::Error) -> Self {
118 Self::Other(error.to_string())
119 }
120}