linux_io/
result.rs

1#[cfg(feature = "std")]
2extern crate std;
3
4/// Represents a result from a kernel call that might fail.
5pub type Result<T> = core::result::Result<T, Error>;
6
7/// Represents an error code directly from the kernel.
8///
9/// This is a lower-level representation of an error for `no_std` situations.
10/// If the crate feature `std` is enabled then `Error` can convert to
11/// `std::io::Error`.
12#[derive(Clone, Copy, PartialEq, Eq, Debug)]
13#[repr(transparent)]
14pub struct Error(pub i32);
15
16impl Error {
17    #[inline(always)]
18    pub const fn new(raw: i32) -> Self {
19        Self(raw)
20    }
21
22    #[cfg(feature = "std")]
23    #[inline(always)]
24    pub fn into_std_io_error(self) -> std::io::Error {
25        std::io::Error::from_raw_os_error(self.0)
26    }
27}
28
29impl From<i32> for Error {
30    #[inline(always)]
31    fn from(value: i32) -> Self {
32        Self::new(value)
33    }
34}
35
36impl From<linux_unsafe::result::Error> for Error {
37    #[inline(always)]
38    fn from(value: linux_unsafe::result::Error) -> Self {
39        Self::new(value.0)
40    }
41}
42
43impl Into<core::fmt::Error> for Error {
44    #[inline(always)]
45    fn into(self) -> core::fmt::Error {
46        core::fmt::Error
47    }
48}
49
50#[cfg(feature = "std")]
51impl Into<std::io::Error> for Error {
52    #[inline(always)]
53    fn into(self) -> std::io::Error {
54        self.into_std_io_error()
55    }
56}
57
58linux_unsafe::result::errno_derived_consts!(Error, Error);