Skip to content

Commit 3706e6f

Browse files
committed
Use io_error_more on WASI
1 parent 1bc0463 commit 3706e6f

File tree

1 file changed

+92
-23
lines changed
  • library/std/src/sys/wasi

1 file changed

+92
-23
lines changed

library/std/src/sys/wasi/mod.rs

+92-23
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,99 @@ pub fn is_interrupted(errno: i32) -> bool {
8282
}
8383

8484
pub fn decode_error_kind(errno: i32) -> std_io::ErrorKind {
85-
use std_io::ErrorKind::*;
86-
if errno > u16::MAX as i32 || errno < 0 {
87-
return Uncategorized;
85+
use std_io::ErrorKind;
86+
87+
let Ok(errno) = u16::try_from(errno) else {
88+
return ErrorKind::Uncategorized;
89+
};
90+
91+
macro_rules! match_errno {
92+
($($($errno:ident)|+ => $errkind:ident),*, _ => $wildcard:ident $(,)?) => {
93+
match errno {
94+
$(e if $(e == ::wasi::$errno.raw())||+ => ErrorKind::$errkind),*,
95+
_ => ErrorKind::$wildcard,
96+
}
97+
};
8898
}
8999

90-
match errno {
91-
e if e == wasi::ERRNO_CONNREFUSED.raw().into() => ConnectionRefused,
92-
e if e == wasi::ERRNO_CONNRESET.raw().into() => ConnectionReset,
93-
e if e == wasi::ERRNO_PERM.raw().into() || e == wasi::ERRNO_ACCES.raw().into() => {
94-
PermissionDenied
95-
}
96-
e if e == wasi::ERRNO_PIPE.raw().into() => BrokenPipe,
97-
e if e == wasi::ERRNO_NOTCONN.raw().into() => NotConnected,
98-
e if e == wasi::ERRNO_CONNABORTED.raw().into() => ConnectionAborted,
99-
e if e == wasi::ERRNO_ADDRNOTAVAIL.raw().into() => AddrNotAvailable,
100-
e if e == wasi::ERRNO_ADDRINUSE.raw().into() => AddrInUse,
101-
e if e == wasi::ERRNO_NOENT.raw().into() => NotFound,
102-
e if e == wasi::ERRNO_INTR.raw().into() => Interrupted,
103-
e if e == wasi::ERRNO_INVAL.raw().into() => InvalidInput,
104-
e if e == wasi::ERRNO_TIMEDOUT.raw().into() => TimedOut,
105-
e if e == wasi::ERRNO_EXIST.raw().into() => AlreadyExists,
106-
e if e == wasi::ERRNO_AGAIN.raw().into() => WouldBlock,
107-
e if e == wasi::ERRNO_NOSYS.raw().into() => Unsupported,
108-
e if e == wasi::ERRNO_NOMEM.raw().into() => OutOfMemory,
109-
_ => Uncategorized,
100+
match_errno! {
101+
ERRNO_2BIG => ArgumentListTooLong,
102+
ERRNO_ACCES => PermissionDenied,
103+
ERRNO_ADDRINUSE => AddrInUse,
104+
ERRNO_ADDRNOTAVAIL => AddrNotAvailable,
105+
ERRNO_AFNOSUPPORT => Unsupported,
106+
ERRNO_AGAIN => WouldBlock,
107+
// ALREADY => "connection already in progress",
108+
// BADF => "bad file descriptor",
109+
// BADMSG => "bad message",
110+
ERRNO_BUSY => ResourceBusy,
111+
// CANCELED => "operation canceled",
112+
// CHILD => "no child processes",
113+
ERRNO_CONNABORTED => ConnectionAborted,
114+
ERRNO_CONNREFUSED => ConnectionRefused,
115+
ERRNO_CONNRESET => ConnectionReset,
116+
ERRNO_DEADLK => Deadlock,
117+
// DESTADDRREQ => "destination address required",
118+
ERRNO_DOM => InvalidInput,
119+
// DQUOT => /* reserved */,
120+
ERRNO_EXIST => AlreadyExists,
121+
// FAULT => "bad address",
122+
ERRNO_FBIG => FileTooLarge,
123+
ERRNO_HOSTUNREACH => HostUnreachable,
124+
// IDRM => "identifier removed",
125+
// ILSEQ => "illegal byte sequence",
126+
// INPROGRESS => "operation in progress",
127+
ERRNO_INTR => Interrupted,
128+
ERRNO_INVAL => InvalidInput,
129+
ERRNO_IO => Uncategorized,
130+
// ISCONN => "socket is connected",
131+
ERRNO_ISDIR => IsADirectory,
132+
ERRNO_LOOP => FilesystemLoop,
133+
// MFILE => "file descriptor value too large",
134+
ERRNO_MLINK => TooManyLinks,
135+
// MSGSIZE => "message too large",
136+
// MULTIHOP => /* reserved */,
137+
ERRNO_NAMETOOLONG => InvalidFilename,
138+
ERRNO_NETDOWN => NetworkDown,
139+
// NETRESET => "connection aborted by network",
140+
ERRNO_NETUNREACH => NetworkUnreachable,
141+
// NFILE => "too many files open in system",
142+
// NOBUFS => "no buffer space available",
143+
ERRNO_NODEV => NotFound,
144+
ERRNO_NOENT => NotFound,
145+
// NOEXEC => "executable file format error",
146+
// NOLCK => "no locks available",
147+
// NOLINK => /* reserved */,
148+
ERRNO_NOMEM => OutOfMemory,
149+
// NOMSG => "no message of the desired type",
150+
// NOPROTOOPT => "protocol not available",
151+
ERRNO_NOSPC => StorageFull,
152+
ERRNO_NOSYS => Unsupported,
153+
ERRNO_NOTCONN => NotConnected,
154+
ERRNO_NOTDIR => NotADirectory,
155+
ERRNO_NOTEMPTY => DirectoryNotEmpty,
156+
// NOTRECOVERABLE => "state not recoverable",
157+
// NOTSOCK => "not a socket",
158+
ERRNO_NOTSUP => Unsupported,
159+
// NOTTY => "inappropriate I/O control operation",
160+
ERRNO_NXIO => NotFound,
161+
// OVERFLOW => "value too large to be stored in data type",
162+
// OWNERDEAD => "previous owner died",
163+
ERRNO_PERM => PermissionDenied,
164+
ERRNO_PIPE => BrokenPipe,
165+
// PROTO => "protocol error",
166+
ERRNO_PROTONOSUPPORT => Unsupported,
167+
// PROTOTYPE => "protocol wrong type for socket",
168+
// RANGE => "result too large",
169+
ERRNO_ROFS => ReadOnlyFilesystem,
170+
ERRNO_SPIPE => NotSeekable,
171+
ERRNO_SRCH => NotFound,
172+
// STALE => /* reserved */,
173+
ERRNO_TIMEDOUT => TimedOut,
174+
ERRNO_TXTBSY => ResourceBusy,
175+
ERRNO_XDEV => CrossesDevices,
176+
ERRNO_NOTCAPABLE => PermissionDenied,
177+
_ => Uncategorized,
110178
}
111179
}
112180

@@ -124,6 +192,7 @@ pub fn hashmap_random_keys() -> (u64, u64) {
124192
return ret;
125193
}
126194

195+
#[inline]
127196
fn err2io(err: wasi::Errno) -> std_io::Error {
128197
std_io::Error::from_raw_os_error(err.raw().into())
129198
}

0 commit comments

Comments
 (0)