1use core::fmt;
4
5#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
7#[repr(transparent)]
8pub struct Errno(pub i32);
9
10pub type SysResult<T> = core::result::Result<T, Errno>;
12
13pub const EPERM: i32 = 1;
14pub const ENOENT: i32 = 2;
15pub const ESRCH: i32 = 3;
16pub const EINTR: i32 = 4;
17pub const EIO: i32 = 5;
18pub const ENXIO: i32 = 6;
19pub const E2BIG: i32 = 7;
20pub const ENOEXEC: i32 = 8;
21pub const EBADF: i32 = 9;
22pub const ECHILD: i32 = 10;
23pub const EAGAIN: i32 = 11;
24pub const EWOULDBLOCK: i32 = 11;
25pub const ENOMEM: i32 = 12;
26pub const EACCES: i32 = 13;
27pub const EFAULT: i32 = 14;
28pub const EBUSY: i32 = 16;
29pub const EEXIST: i32 = 17;
30pub const EXDEV: i32 = 18;
31pub const ENODEV: i32 = 19;
32pub const ENOTDIR: i32 = 20;
33pub const EISDIR: i32 = 21;
34pub const EINVAL: i32 = 22;
35pub const ENFILE: i32 = 23;
36pub const EMFILE: i32 = 24;
37pub const ENOTTY: i32 = 25;
38pub const ETXTBSY: i32 = 26;
39pub const EFBIG: i32 = 27;
40pub const ENOSPC: i32 = 28;
41pub const ESPIPE: i32 = 29;
42pub const EROFS: i32 = 30;
43pub const EMLINK: i32 = 31;
44pub const EPIPE: i32 = 32;
45pub const EDOM: i32 = 33;
46pub const ERANGE: i32 = 34;
47pub const EDEADLK: i32 = 35;
48pub const ENAMETOOLONG: i32 = 36;
49pub const ENOLCK: i32 = 37;
50pub const ENOSYS: i32 = 38;
51pub const ENOTEMPTY: i32 = 39;
52pub const ELOOP: i32 = 40;
53pub const EPROTO: i32 = 71;
54pub const EOVERFLOW: i32 = 75;
55pub const EILSEQ: i32 = 84;
56pub const ENOTSOCK: i32 = 88;
57pub const EDESTADDRREQ: i32 = 89;
58pub const EMSGSIZE: i32 = 90;
59pub const EPROTOTYPE: i32 = 91;
60pub const ENOPROTOOPT: i32 = 92;
61pub const EPROTONOSUPPORT: i32 = 93;
62pub const EOPNOTSUPP: i32 = 95;
63pub const ENOTSUP: i32 = 95;
64pub const EAFNOSUPPORT: i32 = 97;
65pub const EADDRINUSE: i32 = 98;
66pub const EADDRNOTAVAIL: i32 = 99;
67pub const ENETDOWN: i32 = 100;
68pub const ENETUNREACH: i32 = 101;
69pub const ENETRESET: i32 = 102;
70pub const ECONNABORTED: i32 = 103;
71pub const ECONNRESET: i32 = 104;
72pub const ENOBUFS: i32 = 105;
73pub const EISCONN: i32 = 106;
74pub const ENOTCONN: i32 = 107;
75pub const ESHUTDOWN: i32 = 108;
76pub const ETIMEDOUT: i32 = 110;
77pub const ECONNREFUSED: i32 = 111;
78pub const EHOSTUNREACH: i32 = 113;
79pub const EALREADY: i32 = 114;
80pub const EINPROGRESS: i32 = 115;
81pub const ECANCELED: i32 = 125;
82
83impl Errno {
84 #[must_use]
86 pub const fn raw(self) -> i32 {
87 self.0
88 }
89
90 #[must_use]
92 pub const fn would_block(self) -> bool {
93 self.0 == EAGAIN
94 }
95
96 #[must_use]
98 pub const fn interrupted(self) -> bool {
99 self.0 == EINTR
100 }
101
102 #[must_use]
104 pub const fn message(self) -> &'static str {
105 match self.0 {
106 EPERM => "operation not permitted",
107 ENOENT => "no such file or directory",
108 ESRCH => "no such process",
109 EINTR => "interrupted system call",
110 EIO => "input/output error",
111 ENXIO => "no such device or address",
112 E2BIG => "argument list too long",
113 ENOEXEC => "exec format error",
114 EBADF => "bad file descriptor",
115 ECHILD => "no child processes",
116 EAGAIN => "resource temporarily unavailable",
117 ENOMEM => "out of memory",
118 EACCES => "permission denied",
119 EFAULT => "bad address",
120 EBUSY => "device or resource busy",
121 EEXIST => "file exists",
122 EXDEV => "Invalid cross-device link",
123 ENODEV => "no such device",
124 ENOTDIR => "not a directory",
125 EISDIR => "is a directory",
126 EINVAL => "invalid argument",
127 ENFILE => "too many open files in system",
128 EMFILE => "too many open files",
129 ENOTTY => "inappropriate ioctl for device",
130 ETXTBSY => "text file busy",
131 EFBIG => "file too large",
132 ENOSPC => "no space left on device",
133 ESPIPE => "illegal seek",
134 EROFS => "read-only file system",
135 EMLINK => "too many links",
136 EPIPE => "broken pipe",
137 EDOM => "numerical argument out of domain",
138 ERANGE => "numerical result out of range",
139 EDEADLK => "resource deadlock avoided",
140 ENAMETOOLONG => "file name too long",
141 ENOLCK => "no locks available",
142 ENOSYS => "function not implemented",
143 ENOTEMPTY => "directory not empty",
144 ELOOP => "too many levels of symbolic links",
145 EPROTO => "protocol error",
146 EOVERFLOW => "value too large for defined data type",
147 EILSEQ => "invalid or incomplete multibyte or wide character",
148 ENOTSOCK => "socket operation on non-socket",
149 EDESTADDRREQ => "destination address required",
150 EMSGSIZE => "message too long",
151 EPROTOTYPE => "protocol wrong type for socket",
152 ENOPROTOOPT => "protocol not available",
153 EPROTONOSUPPORT => "protocol not supported",
154 EOPNOTSUPP => "operation not supported",
155 EAFNOSUPPORT => "address family not supported by protocol",
156 EADDRINUSE => "address already in use",
157 EADDRNOTAVAIL => "cannot assign requested address",
158 ENETDOWN => "network is down",
159 ENETUNREACH => "network is unreachable",
160 ENETRESET => "network dropped connection on reset",
161 ECONNABORTED => "software caused connection abort",
162 ECONNRESET => "connection reset by peer",
163 ENOBUFS => "no buffer space available",
164 EISCONN => "transport endpoint is already connected",
165 ENOTCONN => "transport endpoint is not connected",
166 ESHUTDOWN => "cannot send after transport endpoint shutdown",
167 ETIMEDOUT => "connection timed out",
168 ECONNREFUSED => "connection refused",
169 EHOSTUNREACH => "no route to host",
170 EALREADY => "operation already in progress",
171 EINPROGRESS => "operation now in progress",
172 ECANCELED => "operation canceled",
173 _ => "unknown error",
174 }
175 }
176}
177
178impl fmt::Display for Errno {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 write!(f, "{} (errno {})", self.message(), self.0)
181 }
182}
183
184impl core::error::Error for Errno {}
185
186#[cfg(test)]
187mod tests {
188 use super::*;
189 use core::fmt::Write;
190
191 struct StackBuf<const N: usize> {
192 buf: [u8; N],
193 len: usize,
194 }
195
196 impl<const N: usize> StackBuf<N> {
197 const fn new() -> Self {
198 Self {
199 buf: [0; N],
200 len: 0,
201 }
202 }
203
204 fn as_str(&self) -> &str {
205 core::str::from_utf8(&self.buf[..self.len]).unwrap_or("")
206 }
207 }
208
209 impl<const N: usize> Write for StackBuf<N> {
210 fn write_str(&mut self, s: &str) -> fmt::Result {
211 let bytes = s.as_bytes();
212 if self.len + bytes.len() > N {
213 return Err(fmt::Error);
214 }
215 self.buf[self.len..self.len + bytes.len()].copy_from_slice(bytes);
216 self.len += bytes.len();
217 Ok(())
218 }
219 }
220
221 #[test]
222 fn errno_helpers_and_messages() {
223 let eagain = Errno(EAGAIN);
224 assert_eq!(eagain.raw(), 11);
225 assert!(eagain.would_block());
226 assert!(!eagain.interrupted());
227 assert_eq!(Errno(EWOULDBLOCK), eagain);
228
229 let eintr = Errno(EINTR);
230 assert!(eintr.interrupted());
231 assert!(!eintr.would_block());
232
233 assert_eq!(Errno(ENOENT).message(), "no such file or directory");
234 assert_eq!(Errno(ENOSYS).message(), "function not implemented");
235 assert_eq!(Errno(EOPNOTSUPP).message(), "operation not supported");
236 assert_eq!(Errno(ENOTSUP).message(), "operation not supported");
237 assert_eq!(Errno(9999).message(), "unknown error");
238
239 let mut out = StackBuf::<64>::new();
240 write!(&mut out, "{}", Errno(EINVAL)).unwrap();
241 assert_eq!(out.as_str(), "invalid argument (errno 22)");
242 }
243}