summaryrefslogtreecommitdiffstats
path: root/rust/kernel/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/kernel/src/error.rs')
-rw-r--r--rust/kernel/src/error.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/rust/kernel/src/error.rs b/rust/kernel/src/error.rs
new file mode 100644
index 000000000000..95e322e39a88
--- /dev/null
+++ b/rust/kernel/src/error.rs
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+use core::num::TryFromIntError;
+
+use crate::bindings;
+use crate::c_types;
+
+pub struct Error(c_types::c_int);
+
+impl Error {
+ pub const EINVAL: Self = Error(-(bindings::EINVAL as i32));
+ pub const ENOMEM: Self = Error(-(bindings::ENOMEM as i32));
+ pub const EFAULT: Self = Error(-(bindings::EFAULT as i32));
+ pub const ESPIPE: Self = Error(-(bindings::ESPIPE as i32));
+ pub const EAGAIN: Self = Error(-(bindings::EAGAIN as i32));
+
+ pub fn from_kernel_errno(errno: c_types::c_int) -> Error {
+ Error(errno)
+ }
+
+ pub fn to_kernel_errno(&self) -> c_types::c_int {
+ self.0
+ }
+}
+
+impl From<TryFromIntError> for Error {
+ fn from(_: TryFromIntError) -> Error {
+ Error::EINVAL
+ }
+}
+
+pub type KernelResult<T> = Result<T, Error>;