1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
macro_rules! def_errno {
() => {
/// Error number representation.
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct Errno(pub(crate) i32);
impl Errno {
/// Returns a new `Errno` from the given integer.
#[inline]
pub fn new(num: i32) -> Self {
Self(num)
}
/// Converts `Errno` into the under underlining integer.
#[inline]
pub fn into_raw(self) -> i32 {
self.0
}
/// Returns `true` if the error code is in valid range (lower than 4096)
#[inline]
pub fn is_valid(&self) -> bool {
self.0 < 4096
}
/// Returns a new `Errno` from a syscall's result.
#[inline(always)]
pub fn from_ret(value: usize) -> ::core::result::Result<usize, Errno> {
if value > -4096isize as usize {
::core::result::Result::Err(Self(-(value as i32)))
} else {
::core::result::Result::Ok(value)
}
}
/// Returns the name of the error if it's known. Generally the name of the constant.
pub fn name(&self) -> ::core::option::Option<&'static str> {
self.name_and_description().map(|x| x.0)
}
/// Returns a description of the error if it's known.
pub fn description(&self) -> ::core::option::Option<&'static str> {
self.name_and_description().map(|x| x.1)
}
/// Returns a new `Errno` if the given error is generated from a system error.
/// None otherwise.
#[cfg(any(doc, feature = "std"))]
#[inline]
pub fn from_io_error(err: ::std::io::Error) -> ::core::option::Option<Self> {
err.raw_os_error().map(Self)
}
/// Returns an iterator `ErrnoIter` over all the known error numbers.
#[cfg(any(doc, feature = "iter"))]
#[inline]
pub fn iter() -> ErrnoIter {
ErrnoIter(Self::ALL.iter())
}
}
impl ::core::fmt::Display for Errno {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self.name_and_description() {
Some((name, desc)) => {
write!(f, "{} {} ({})", -self.0, name, desc)
}
None => {
if self.is_valid() {
write!(f, "{}", -self.0)
} else {
write!(f, "Unknown errno {:#x}", self.0)
}
}
}
}
}
impl ::core::fmt::Debug for Errno {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self.name() {
::core::option::Option::Some(name) => f.write_str(name),
::core::option::Option::None => write!(f, "Errno({})", self.0),
}
}
}
#[cfg(any(doc, feature = "std"))]
impl ::core::convert::From<Errno> for ::std::io::Error {
#[inline]
fn from(value: Errno) -> Self {
::std::io::Error::from_raw_os_error(value.into_raw())
}
}
#[cfg(any(doc, feature = "std"))]
impl ::std::error::Error for Errno {}
#[cfg(any(doc, feature = "no_std_io-compat"))]
impl ::core::convert::From<Errno> for ::no_std_io::io::Error {
#[inline]
fn from(value: Errno) -> Self {
use ::no_std_io::io::ErrorKind::*;
match value {
// Errno::E2BIG => ArgumentListTooLong.into(),
Errno::EADDRINUSE => AddrInUse.into(),
Errno::EADDRNOTAVAIL => AddrNotAvailable.into(),
// Errno::EBUSY => ResourceBusy.into(),
Errno::ECONNABORTED => ConnectionAborted.into(),
Errno::ECONNREFUSED => ConnectionRefused.into(),
Errno::ECONNRESET => ConnectionReset.into(),
// Errno::EDEADLK => Deadlock.into(),
// Errno::EDQUOT => FilesystemQuotaExceeded.into(),
Errno::EEXIST => AlreadyExists.into(),
// Errno::EFBIG => FileTooLarge.into(),
// Errno::EHOSTUNREACH => HostUnreachable.into(),
Errno::EINTR => Interrupted.into(),
Errno::EINVAL => InvalidInput.into(),
// Errno::EISDIR => IsADirectory.into(),
// Errno::ELOOP => FilesystemLoop.into(),
Errno::ENOENT => NotFound.into(),
// Errno::ENOMEM => OutOfMemory.into(),
// Errno::ENOSPC => StorageFull.into(),
// Errno::ENOSYS => Unsupported.into(),
// Errno::EMLINK => TooManyLinks.into(),
// Errno::ENAMETOOLONG => InvalidFilename.into(),
// Errno::ENETDOWN => NetworkDown.into(),
// Errno::ENETUNREACH => NetworkUnreachable.into(),
Errno::ENOTCONN => NotConnected.into(),
// Errno::ENOTDIR => NotADirectory.into(),
// Errno::ENOTEMPTY => DirectoryNotEmpty.into(),
Errno::EPIPE => BrokenPipe.into(),
// Errno::EROFS => ReadOnlyFilesystem.into(),
// Errno::ESPIPE => NotSeekable.into(),
// Errno::ESTALE => StaleNetworkFileHandle.into(),
Errno::ETIMEDOUT => TimedOut.into(),
// Errno::ETXTBSY => ExecutableFileBusy.into(),
// Errno::EXDEV => CrossesDevices.into(),
Errno::EACCES | Errno::EPERM => PermissionDenied.into(),
// These two constants can have the same value on some systems,
// but different values on others, so we can't use a match
// clause
x if x == Errno::EAGAIN || x == Errno::EWOULDBLOCK => WouldBlock.into(),
x => ::no_std_io::io::Error::new(
Uncategorized,
x.description().unwrap_or("Unknown error"),
),
}
}
}
#[cfg(any(doc, feature = "no_std_io-compat"))]
impl ::no_std_io::error::Error for Errno {}
#[cfg(any(doc, feature = "iter"))]
/// Iterator over all possible error numbers.
pub struct ErrnoIter(::core::slice::Iter<'static, i32>);
#[cfg(any(doc, feature = "iter"))]
impl ::core::iter::Iterator for ErrnoIter {
type Item = Errno;
#[inline]
fn next(&mut self) -> ::core::option::Option<Self::Item> {
self.0.next().copied().map(Errno)
}
#[inline]
fn size_hint(&self) -> (usize, ::core::option::Option<usize>) {
self.0.size_hint()
}
#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.0.count()
}
#[inline]
fn last(self) -> ::core::option::Option<Self::Item>
where
Self: Sized,
{
self.0.last().copied().map(Errno)
}
fn nth(&mut self, n: usize) -> ::core::option::Option<Self::Item> {
self.0.nth(n).copied().map(Errno)
}
}
#[cfg(any(doc, feature = "iter"))]
impl ::core::iter::ExactSizeIterator for ErrnoIter {
#[inline]
fn len(&self) -> usize {
self.0.len()
}
}
#[cfg(any(doc, feature = "iter"))]
impl ::core::iter::DoubleEndedIterator for ErrnoIter {
#[inline]
fn next_back(&mut self) -> ::core::option::Option<Self::Item> {
self.0.next_back().copied().map(Errno)
}
#[inline]
fn nth_back(&mut self, n: usize) -> ::core::option::Option<Self::Item> {
self.0.nth_back(n).copied().map(Errno)
}
}
};
}
pub(crate) use def_errno;