/rust/registry/src/index.crates.io-6f17d22bba15001f/rustix-0.38.34/src/io/errno.rs
Line | Count | Source (jump to first uncovered line) |
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 | 0 | pub fn kind(self) -> std::io::ErrorKind { |
22 | 0 | std::io::Error::from(self).kind() |
23 | 0 | } |
24 | | } |
25 | | |
26 | | impl fmt::Display for Errno { |
27 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
28 | 0 | #[cfg(feature = "std")] |
29 | 0 | { |
30 | 0 | std::io::Error::from(*self).fmt(fmt) |
31 | 0 | } |
32 | 0 | #[cfg(not(feature = "std"))] |
33 | 0 | { |
34 | 0 | write!(fmt, "os error {}", self.raw_os_error()) |
35 | 0 | } |
36 | 0 | } |
37 | | } |
38 | | |
39 | | impl fmt::Debug for Errno { |
40 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
41 | 0 | #[cfg(feature = "std")] |
42 | 0 | { |
43 | 0 | std::io::Error::from(*self).fmt(fmt) |
44 | 0 | } |
45 | 0 | #[cfg(not(feature = "std"))] |
46 | 0 | { |
47 | 0 | write!(fmt, "os error {}", self.raw_os_error()) |
48 | 0 | } |
49 | 0 | } |
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 | 0 | fn from(err: Errno) -> Self { |
59 | 0 | Self::from_raw_os_error(err.raw_os_error() as _) |
60 | 0 | } Unexecuted instantiation: <std::io::error::Error as core::convert::From<rustix::backend::io::errno::Errno>>::from Unexecuted instantiation: <std::io::error::Error as core::convert::From<rustix::backend::io::errno::Errno>>::from |
61 | | } |
62 | | |
63 | | /// Call `f` until it either succeeds or fails other than [`Errno::INTR`]. |
64 | | #[inline] |
65 | 0 | pub fn retry_on_intr<T, F: FnMut() -> Result<T>>(mut f: F) -> Result<T> { |
66 | 0 | loop { |
67 | 0 | match f() { |
68 | 0 | Err(Errno::INTR) => (), |
69 | 0 | result => return result, |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } Unexecuted instantiation: rustix::io::errno::retry_on_intr::<usize, <rustix::backend::fs::dir::Dir>::read_more::{closure#0}> Unexecuted instantiation: rustix::io::errno::retry_on_intr::<u64, <rustix::backend::fs::dir::Dir>::read::{closure#0}> |