sys_util/
errno.rs

1// Copyright 2017 The Chromium OS Authors. All rights reserved.
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::error;
6use std::fmt::{self, Display};
7use std::io;
8use std::result;
9
10use libc::__errno_location;
11
12/// An error number, retrieved from errno (man 3 errno), set by a libc
13/// function that returned an error.
14#[derive(Clone, Copy, Debug, PartialEq)]
15pub struct Error(i32);
16pub type Result<T> = result::Result<T, Error>;
17
18impl Error {
19    /// Constructs a new error with the given errno.
20    pub fn new(e: i32) -> Error {
21        Error(e)
22    }
23
24    /// Constructs an error from the current errno.
25    ///
26    /// The result of this only has any meaning just after a libc call that returned a value
27    /// indicating errno was set.
28    pub fn last() -> Error {
29        Error(unsafe { *__errno_location() })
30    }
31
32    /// Gets the errno for this error
33    pub fn errno(&self) -> i32 {
34        self.0
35    }
36}
37
38impl From<io::Error> for Error {
39    fn from(e: io::Error) -> Self {
40        Error::new(e.raw_os_error().unwrap_or_default())
41    }
42}
43
44impl error::Error for Error {
45    fn description(&self) -> &str {
46        "System returned an error code"
47    }
48}
49
50impl Display for Error {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        write!(f, "Error: {}", self.errno())
53    }
54}
55
56/// Returns the last errno as a Result that is always an error.
57pub fn errno_result<T>() -> Result<T> {
58    Err(Error::last())
59}
60
61/// Sets errno to given error code.
62/// Only defined when we compile tests as normal code does not
63/// normally need set errno.
64#[cfg(test)]
65pub fn set_errno(e: i32) {
66    unsafe {
67        *__errno_location() = e;
68    }
69}