base/
errno.rs

1// Copyright 2017 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::convert::From;
6use std::convert::TryInto;
7use std::fmt;
8use std::fmt::Display;
9use std::io;
10use std::result;
11
12use serde::Deserialize;
13use serde::Serialize;
14use thiserror::Error;
15
16/// A system error
17/// In Unix systems, retrieved from errno (man 3 errno), set by a libc
18/// function that returned an error.
19/// On Windows, retrieved from GetLastError, set by a Windows function
20/// that returned an error
21#[derive(Error, Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
22#[serde(transparent)]
23pub struct Error(i32);
24pub type Result<T> = result::Result<T, Error>;
25
26impl Error {
27    /// Constructs a new error with the given error number.
28    pub fn new<T: TryInto<i32>>(e: T) -> Error {
29        // A value outside the bounds of an i32 will never be a valid
30        // errno/GetLastError
31        Error(e.try_into().unwrap_or_default())
32    }
33
34    /// Constructs an Error from the most recent system error.
35    ///
36    /// The result of this only has any meaning just after a libc/Windows call that returned
37    /// a value indicating errno was set.
38    pub fn last() -> Error {
39        Error(io::Error::last_os_error().raw_os_error().unwrap())
40    }
41
42    /// Gets the errno for this error
43    pub fn errno(self) -> i32 {
44        self.0
45    }
46}
47
48impl From<io::Error> for Error {
49    fn from(e: io::Error) -> Self {
50        Error(e.raw_os_error().unwrap_or_default())
51    }
52}
53
54impl From<Error> for io::Error {
55    fn from(e: Error) -> io::Error {
56        io::Error::from_raw_os_error(e.0)
57    }
58}
59
60impl From<Error> for Box<dyn std::error::Error + Send> {
61    fn from(e: Error) -> Self {
62        Box::new(e)
63    }
64}
65
66impl Display for Error {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        Into::<io::Error>::into(*self).fmt(f)
69    }
70}
71
72/// Returns the last errno as a Result that is always an error.
73pub fn errno_result<T>() -> Result<T> {
74    Err(Error::last())
75}