1 | //! The `Errno` type, which is a minimal wrapper around an error code. |
2 | //! |
3 | //! We define the error constants as individual `const`s instead of an enum |
4 | //! because we may not know about all of the host's error values and we don't |
5 | //! want unrecognized values to create undefined behavior. |
6 | |
7 | use crate::backend; |
8 | use core::{fmt, result}; |
9 | #[cfg (feature = "std" )] |
10 | use std::error; |
11 | |
12 | /// A specialized [`Result`] type for `rustix` APIs. |
13 | pub type Result<T> = result::Result<T, Errno>; |
14 | |
15 | pub use backend::io::errno::Errno; |
16 | |
17 | impl Errno { |
18 | /// Shorthand for `std::io::Error::from(self).kind()`. |
19 | #[cfg (feature = "std" )] |
20 | #[inline ] |
21 | pub fn kind(self) -> std::io::ErrorKind { |
22 | std::io::Error::from(self).kind() |
23 | } |
24 | } |
25 | |
26 | impl fmt::Display for Errno { |
27 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
28 | #[cfg (feature = "std" )] |
29 | { |
30 | std::io::Error::from(*self).fmt(fmt) |
31 | } |
32 | #[cfg (not(feature = "std" ))] |
33 | { |
34 | write!(fmt, "os error {}" , self.raw_os_error()) |
35 | } |
36 | } |
37 | } |
38 | |
39 | impl fmt::Debug for Errno { |
40 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
41 | #[cfg (feature = "std" )] |
42 | { |
43 | std::io::Error::from(*self).fmt(fmt) |
44 | } |
45 | #[cfg (not(feature = "std" ))] |
46 | { |
47 | write!(fmt, "os error {}" , self.raw_os_error()) |
48 | } |
49 | } |
50 | } |
51 | |
52 | #[cfg (feature = "std" )] |
53 | impl error::Error for Errno {} |
54 | |
55 | #[cfg (feature = "std" )] |
56 | impl From<Errno> for std::io::Error { |
57 | #[inline ] |
58 | fn from(err: Errno) -> Self { |
59 | Self::from_raw_os_error(code:err.raw_os_error() as _) |
60 | } |
61 | } |
62 | |
63 | /// Call `f` until it either succeeds or fails other than [`Errno::INTR`]. |
64 | #[inline ] |
65 | pub fn retry_on_intr<T, F: FnMut() -> Result<T>>(mut f: F) -> Result<T> { |
66 | loop { |
67 | match f() { |
68 | Err(Errno::INTR) => (), |
69 | result: Result => return result, |
70 | } |
71 | } |
72 | } |
73 | |