summaryrefslogtreecommitdiffstats
path: root/rust/kernel/src/error.rs
blob: 95e322e39a88e717d94cc28edf5a1188f1a46851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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>;