xref: /aosp_15_r20/external/crosvm/base/src/errno.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
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 
5 use std::convert::From;
6 use std::convert::TryInto;
7 use std::fmt;
8 use std::fmt::Display;
9 use std::io;
10 use std::result;
11 
12 use serde::Deserialize;
13 use serde::Serialize;
14 use 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)]
23 pub struct Error(i32);
24 pub type Result<T> = result::Result<T, Error>;
25 
26 impl Error {
27     /// Constructs a new error with the given error number.
new<T: TryInto<i32>>(e: T) -> Error28     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.
last() -> Error38     pub fn last() -> Error {
39         Error(io::Error::last_os_error().raw_os_error().unwrap())
40     }
41 
42     /// Gets the errno for this error
errno(self) -> i3243     pub fn errno(self) -> i32 {
44         self.0
45     }
46 }
47 
48 impl From<io::Error> for Error {
from(e: io::Error) -> Self49     fn from(e: io::Error) -> Self {
50         Error(e.raw_os_error().unwrap_or_default())
51     }
52 }
53 
54 impl From<Error> for io::Error {
from(e: Error) -> io::Error55     fn from(e: Error) -> io::Error {
56         io::Error::from_raw_os_error(e.0)
57     }
58 }
59 
60 impl From<Error> for Box<dyn std::error::Error + Send> {
from(e: Error) -> Self61     fn from(e: Error) -> Self {
62         Box::new(e)
63     }
64 }
65 
66 impl Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result67     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.
errno_result<T>() -> Result<T>73 pub fn errno_result<T>() -> Result<T> {
74     Err(Error::last())
75 }
76