rustix/backend/linux_raw/io/
errno.rs
1#![allow(unsafe_code)]
11#![cfg_attr(not(rustc_attrs), allow(unused_unsafe))]
12
13use crate::backend::c;
14use crate::backend::fd::RawFd;
15use crate::backend::reg::{RetNumber, RetReg};
16use crate::io;
17use linux_raw_sys::errno;
18
19#[repr(transparent)]
46#[doc(alias = "errno")]
47#[derive(Eq, PartialEq, Hash, Copy, Clone)]
48#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_start(0xf001))]
51#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_end(0xffff))]
52pub struct Errno(u16);
53
54impl Errno {
55 #[cfg(feature = "std")]
60 #[inline]
61 pub fn from_io_error(io_err: &std::io::Error) -> Option<Self> {
62 io_err.raw_os_error().and_then(|raw| {
63 if (1..4096).contains(&raw) {
66 Some(Self::from_errno(raw as u32))
67 } else {
68 None
69 }
70 })
71 }
72
73 #[inline]
75 pub const fn raw_os_error(self) -> i32 {
76 (self.0 as i16 as i32).wrapping_neg()
77 }
78
79 #[inline]
81 pub const fn from_raw_os_error(raw: i32) -> Self {
82 Self::from_errno(raw as u32)
83 }
84
85 const fn from_errno(raw: u32) -> Self {
87 let encoded = raw.wrapping_neg() as u16;
90
91 assert!(encoded >= 0xf001);
93
94 unsafe { Self(encoded) }
97 }
98}
99
100#[inline]
103pub(in crate::backend) fn try_decode_c_int<Num: RetNumber>(
104 raw: RetReg<Num>,
105) -> io::Result<c::c_int> {
106 if raw.is_in_range(-4095..0) {
107 return Err(unsafe { Errno(raw.decode_error_code()) });
110 }
111
112 Ok(raw.decode_c_int())
113}
114
115#[inline]
118pub(in crate::backend) fn try_decode_c_uint<Num: RetNumber>(
119 raw: RetReg<Num>,
120) -> io::Result<c::c_uint> {
121 if raw.is_in_range(-4095..0) {
122 return Err(unsafe { Errno(raw.decode_error_code()) });
125 }
126
127 Ok(raw.decode_c_uint())
128}
129
130#[inline]
133pub(in crate::backend) fn try_decode_usize<Num: RetNumber>(raw: RetReg<Num>) -> io::Result<usize> {
134 if raw.is_in_range(-4095..0) {
135 return Err(unsafe { Errno(raw.decode_error_code()) });
138 }
139
140 Ok(raw.decode_usize())
141}
142
143#[inline]
146pub(in crate::backend) fn try_decode_void_star<Num: RetNumber>(
147 raw: RetReg<Num>,
148) -> io::Result<*mut c::c_void> {
149 if raw.is_in_range(-4095..0) {
150 return Err(unsafe { Errno(raw.decode_error_code()) });
153 }
154
155 Ok(raw.decode_void_star())
156}
157
158#[cfg(target_pointer_width = "64")]
161#[inline]
162pub(in crate::backend) fn try_decode_u64<Num: RetNumber>(raw: RetReg<Num>) -> io::Result<u64> {
163 if raw.is_in_range(-4095..0) {
164 return Err(unsafe { Errno(raw.decode_error_code()) });
167 }
168
169 Ok(raw.decode_u64())
170}
171
172#[inline]
180pub(in crate::backend) unsafe fn try_decode_raw_fd<Num: RetNumber>(
181 raw: RetReg<Num>,
182) -> io::Result<RawFd> {
183 if raw.is_negative() {
187 debug_assert!(raw.is_in_range(-4095..0));
188
189 #[cfg(core_intrinsics)]
192 {
193 core::intrinsics::assume(raw.is_in_range(-4095..0));
194 }
195
196 return Err(Errno(raw.decode_error_code()));
197 }
198
199 Ok(raw.decode_raw_fd())
200}
201
202#[inline]
209pub(in crate::backend) unsafe fn try_decode_void<Num: RetNumber>(
210 raw: RetReg<Num>,
211) -> io::Result<()> {
212 if raw.is_nonzero() {
216 debug_assert!(raw.is_in_range(-4095..0));
217
218 #[cfg(core_intrinsics)]
221 {
222 core::intrinsics::assume(raw.is_in_range(-4095..0));
223 }
224
225 return Err(Errno(raw.decode_error_code()));
226 }
227
228 raw.decode_void();
229
230 Ok(())
231}
232
233#[cfg(feature = "runtime")]
240#[inline]
241pub(in crate::backend) unsafe fn try_decode_error<Num: RetNumber>(raw: RetReg<Num>) -> io::Errno {
242 debug_assert!(raw.is_in_range(-4095..0));
243
244 #[cfg(core_intrinsics)]
247 {
248 core::intrinsics::assume(raw.is_in_range(-4095..0));
249 }
250
251 Errno(raw.decode_error_code())
252}
253
254#[cfg(not(debug_assertions))]
256#[inline]
257pub(in crate::backend) fn decode_usize_infallible<Num: RetNumber>(raw: RetReg<Num>) -> usize {
258 raw.decode_usize()
259}
260
261#[cfg(not(debug_assertions))]
263#[inline]
264pub(in crate::backend) fn decode_c_int_infallible<Num: RetNumber>(raw: RetReg<Num>) -> c::c_int {
265 raw.decode_c_int()
266}
267
268#[cfg(not(debug_assertions))]
270#[inline]
271pub(in crate::backend) fn decode_c_uint_infallible<Num: RetNumber>(raw: RetReg<Num>) -> c::c_uint {
272 raw.decode_c_uint()
273}
274
275impl Errno {
276 #[doc(alias = "ACCES")]
278 pub const ACCESS: Self = Self::from_errno(errno::EACCES);
279 pub const ADDRINUSE: Self = Self::from_errno(errno::EADDRINUSE);
281 pub const ADDRNOTAVAIL: Self = Self::from_errno(errno::EADDRNOTAVAIL);
283 pub const ADV: Self = Self::from_errno(errno::EADV);
285 pub const AFNOSUPPORT: Self = Self::from_errno(errno::EAFNOSUPPORT);
287 pub const AGAIN: Self = Self::from_errno(errno::EAGAIN);
289 pub const ALREADY: Self = Self::from_errno(errno::EALREADY);
291 pub const BADE: Self = Self::from_errno(errno::EBADE);
293 pub const BADF: Self = Self::from_errno(errno::EBADF);
295 pub const BADFD: Self = Self::from_errno(errno::EBADFD);
297 pub const BADMSG: Self = Self::from_errno(errno::EBADMSG);
299 pub const BADR: Self = Self::from_errno(errno::EBADR);
301 pub const BADRQC: Self = Self::from_errno(errno::EBADRQC);
303 pub const BADSLT: Self = Self::from_errno(errno::EBADSLT);
305 pub const BFONT: Self = Self::from_errno(errno::EBFONT);
307 pub const BUSY: Self = Self::from_errno(errno::EBUSY);
309 pub const CANCELED: Self = Self::from_errno(errno::ECANCELED);
311 pub const CHILD: Self = Self::from_errno(errno::ECHILD);
313 pub const CHRNG: Self = Self::from_errno(errno::ECHRNG);
315 pub const COMM: Self = Self::from_errno(errno::ECOMM);
317 pub const CONNABORTED: Self = Self::from_errno(errno::ECONNABORTED);
319 pub const CONNREFUSED: Self = Self::from_errno(errno::ECONNREFUSED);
321 pub const CONNRESET: Self = Self::from_errno(errno::ECONNRESET);
323 pub const DEADLK: Self = Self::from_errno(errno::EDEADLK);
325 pub const DEADLOCK: Self = Self::from_errno(errno::EDEADLOCK);
327 pub const DESTADDRREQ: Self = Self::from_errno(errno::EDESTADDRREQ);
329 pub const DOM: Self = Self::from_errno(errno::EDOM);
331 pub const DOTDOT: Self = Self::from_errno(errno::EDOTDOT);
333 pub const DQUOT: Self = Self::from_errno(errno::EDQUOT);
335 pub const EXIST: Self = Self::from_errno(errno::EEXIST);
337 pub const FAULT: Self = Self::from_errno(errno::EFAULT);
339 pub const FBIG: Self = Self::from_errno(errno::EFBIG);
341 pub const HOSTDOWN: Self = Self::from_errno(errno::EHOSTDOWN);
343 pub const HOSTUNREACH: Self = Self::from_errno(errno::EHOSTUNREACH);
345 pub const HWPOISON: Self = Self::from_errno(errno::EHWPOISON);
347 pub const IDRM: Self = Self::from_errno(errno::EIDRM);
349 pub const ILSEQ: Self = Self::from_errno(errno::EILSEQ);
351 pub const INPROGRESS: Self = Self::from_errno(errno::EINPROGRESS);
353 pub const INTR: Self = Self::from_errno(errno::EINTR);
360 pub const INVAL: Self = Self::from_errno(errno::EINVAL);
362 pub const IO: Self = Self::from_errno(errno::EIO);
364 pub const ISCONN: Self = Self::from_errno(errno::EISCONN);
366 pub const ISDIR: Self = Self::from_errno(errno::EISDIR);
368 pub const ISNAM: Self = Self::from_errno(errno::EISNAM);
370 pub const KEYEXPIRED: Self = Self::from_errno(errno::EKEYEXPIRED);
372 pub const KEYREJECTED: Self = Self::from_errno(errno::EKEYREJECTED);
374 pub const KEYREVOKED: Self = Self::from_errno(errno::EKEYREVOKED);
376 pub const L2HLT: Self = Self::from_errno(errno::EL2HLT);
378 pub const L2NSYNC: Self = Self::from_errno(errno::EL2NSYNC);
380 pub const L3HLT: Self = Self::from_errno(errno::EL3HLT);
382 pub const L3RST: Self = Self::from_errno(errno::EL3RST);
384 pub const LIBACC: Self = Self::from_errno(errno::ELIBACC);
386 pub const LIBBAD: Self = Self::from_errno(errno::ELIBBAD);
388 pub const LIBEXEC: Self = Self::from_errno(errno::ELIBEXEC);
390 pub const LIBMAX: Self = Self::from_errno(errno::ELIBMAX);
392 pub const LIBSCN: Self = Self::from_errno(errno::ELIBSCN);
394 pub const LNRNG: Self = Self::from_errno(errno::ELNRNG);
396 pub const LOOP: Self = Self::from_errno(errno::ELOOP);
398 pub const MEDIUMTYPE: Self = Self::from_errno(errno::EMEDIUMTYPE);
400 pub const MFILE: Self = Self::from_errno(errno::EMFILE);
402 pub const MLINK: Self = Self::from_errno(errno::EMLINK);
404 pub const MSGSIZE: Self = Self::from_errno(errno::EMSGSIZE);
406 pub const MULTIHOP: Self = Self::from_errno(errno::EMULTIHOP);
408 pub const NAMETOOLONG: Self = Self::from_errno(errno::ENAMETOOLONG);
410 pub const NAVAIL: Self = Self::from_errno(errno::ENAVAIL);
412 pub const NETDOWN: Self = Self::from_errno(errno::ENETDOWN);
414 pub const NETRESET: Self = Self::from_errno(errno::ENETRESET);
416 pub const NETUNREACH: Self = Self::from_errno(errno::ENETUNREACH);
418 pub const NFILE: Self = Self::from_errno(errno::ENFILE);
420 pub const NOANO: Self = Self::from_errno(errno::ENOANO);
422 pub const NOBUFS: Self = Self::from_errno(errno::ENOBUFS);
424 pub const NOCSI: Self = Self::from_errno(errno::ENOCSI);
426 #[doc(alias = "NOATTR")]
428 pub const NODATA: Self = Self::from_errno(errno::ENODATA);
429 pub const NODEV: Self = Self::from_errno(errno::ENODEV);
431 pub const NOENT: Self = Self::from_errno(errno::ENOENT);
433 pub const NOEXEC: Self = Self::from_errno(errno::ENOEXEC);
435 pub const NOKEY: Self = Self::from_errno(errno::ENOKEY);
437 pub const NOLCK: Self = Self::from_errno(errno::ENOLCK);
439 pub const NOLINK: Self = Self::from_errno(errno::ENOLINK);
441 pub const NOMEDIUM: Self = Self::from_errno(errno::ENOMEDIUM);
443 pub const NOMEM: Self = Self::from_errno(errno::ENOMEM);
445 pub const NOMSG: Self = Self::from_errno(errno::ENOMSG);
447 pub const NONET: Self = Self::from_errno(errno::ENONET);
449 pub const NOPKG: Self = Self::from_errno(errno::ENOPKG);
451 pub const NOPROTOOPT: Self = Self::from_errno(errno::ENOPROTOOPT);
453 pub const NOSPC: Self = Self::from_errno(errno::ENOSPC);
455 pub const NOSR: Self = Self::from_errno(errno::ENOSR);
457 pub const NOSTR: Self = Self::from_errno(errno::ENOSTR);
459 pub const NOSYS: Self = Self::from_errno(errno::ENOSYS);
461 pub const NOTBLK: Self = Self::from_errno(errno::ENOTBLK);
463 pub const NOTCONN: Self = Self::from_errno(errno::ENOTCONN);
465 pub const NOTDIR: Self = Self::from_errno(errno::ENOTDIR);
467 pub const NOTEMPTY: Self = Self::from_errno(errno::ENOTEMPTY);
469 pub const NOTNAM: Self = Self::from_errno(errno::ENOTNAM);
471 pub const NOTRECOVERABLE: Self = Self::from_errno(errno::ENOTRECOVERABLE);
473 pub const NOTSOCK: Self = Self::from_errno(errno::ENOTSOCK);
475 pub const NOTSUP: Self = Self::from_errno(errno::EOPNOTSUPP);
478 pub const NOTTY: Self = Self::from_errno(errno::ENOTTY);
480 pub const NOTUNIQ: Self = Self::from_errno(errno::ENOTUNIQ);
482 pub const NXIO: Self = Self::from_errno(errno::ENXIO);
484 pub const OPNOTSUPP: Self = Self::from_errno(errno::EOPNOTSUPP);
486 pub const OVERFLOW: Self = Self::from_errno(errno::EOVERFLOW);
488 pub const OWNERDEAD: Self = Self::from_errno(errno::EOWNERDEAD);
490 pub const PERM: Self = Self::from_errno(errno::EPERM);
492 pub const PFNOSUPPORT: Self = Self::from_errno(errno::EPFNOSUPPORT);
494 pub const PIPE: Self = Self::from_errno(errno::EPIPE);
496 pub const PROTO: Self = Self::from_errno(errno::EPROTO);
498 pub const PROTONOSUPPORT: Self = Self::from_errno(errno::EPROTONOSUPPORT);
500 pub const PROTOTYPE: Self = Self::from_errno(errno::EPROTOTYPE);
502 pub const RANGE: Self = Self::from_errno(errno::ERANGE);
504 pub const REMCHG: Self = Self::from_errno(errno::EREMCHG);
506 pub const REMOTE: Self = Self::from_errno(errno::EREMOTE);
508 pub const REMOTEIO: Self = Self::from_errno(errno::EREMOTEIO);
510 pub const RESTART: Self = Self::from_errno(errno::ERESTART);
512 pub const RFKILL: Self = Self::from_errno(errno::ERFKILL);
514 pub const ROFS: Self = Self::from_errno(errno::EROFS);
516 pub const SHUTDOWN: Self = Self::from_errno(errno::ESHUTDOWN);
518 pub const SOCKTNOSUPPORT: Self = Self::from_errno(errno::ESOCKTNOSUPPORT);
520 pub const SPIPE: Self = Self::from_errno(errno::ESPIPE);
522 pub const SRCH: Self = Self::from_errno(errno::ESRCH);
524 pub const SRMNT: Self = Self::from_errno(errno::ESRMNT);
526 pub const STALE: Self = Self::from_errno(errno::ESTALE);
528 pub const STRPIPE: Self = Self::from_errno(errno::ESTRPIPE);
530 pub const TIME: Self = Self::from_errno(errno::ETIME);
532 pub const TIMEDOUT: Self = Self::from_errno(errno::ETIMEDOUT);
534 #[doc(alias = "2BIG")]
536 pub const TOOBIG: Self = Self::from_errno(errno::E2BIG);
537 pub const TOOMANYREFS: Self = Self::from_errno(errno::ETOOMANYREFS);
539 pub const TXTBSY: Self = Self::from_errno(errno::ETXTBSY);
541 pub const UCLEAN: Self = Self::from_errno(errno::EUCLEAN);
543 pub const UNATCH: Self = Self::from_errno(errno::EUNATCH);
545 pub const USERS: Self = Self::from_errno(errno::EUSERS);
547 pub const WOULDBLOCK: Self = Self::from_errno(errno::EWOULDBLOCK);
549 pub const XDEV: Self = Self::from_errno(errno::EXDEV);
551 pub const XFULL: Self = Self::from_errno(errno::EXFULL);
553}