Skip to content

Commit 6960e7c

Browse files
committed
Adjust usage of std::io::ErrorKind to be core compatible
* Checking exhaustion will no longer be possible for `repr_bitpacked`. Moving `kind_from_prim` into an associated function, and setting it up to be moved into `core::io` as well. * `ErrorKind::as_str` is private, but it's only usage is trivially replaced with `Display::fmt` * The features io_error_inprogress, io_error_more, and io_error_uncategorized will all need to be enabled
1 parent 08bd077 commit 6960e7c

4 files changed

Lines changed: 74 additions & 63 deletions

File tree

library/std/src/io/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ impl fmt::Display for Error {
10381038
write!(fmt, "{detail} (os error {code})")
10391039
}
10401040
ErrorData::Custom(ref c) => c.error.fmt(fmt),
1041-
ErrorData::Simple(kind) => write!(fmt, "{}", kind.as_str()),
1041+
ErrorData::Simple(kind) => kind.fmt(fmt),
10421042
ErrorData::SimpleMessage(msg) => msg.message.fmt(fmt),
10431043
}
10441044
}

library/std/src/io/error/repr_bitpacked.rs

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ where
253253
}
254254
TAG_SIMPLE => {
255255
let kind_bits = (bits >> 32) as u32;
256-
let kind = kind_from_prim(kind_bits).unwrap_or_else(|| {
256+
let kind = ErrorKind::from_prim(kind_bits).unwrap_or_else(|| {
257257
debug_assert!(false, "Invalid io::error::Repr bits: `Repr({:#018x})`", bits);
258258
// This means the `ptr` passed in was not valid, which violates
259259
// the unsafe contract of `decode_repr`.
@@ -283,67 +283,75 @@ where
283283
}
284284
}
285285

286-
// This compiles to the same code as the check+transmute, but doesn't require
287-
// unsafe, or to hard-code max ErrorKind or its size in a way the compiler
288-
// couldn't verify.
289-
#[inline]
290-
fn kind_from_prim(ek: u32) -> Option<ErrorKind> {
291-
macro_rules! from_prim {
292-
($prim:expr => $Enum:ident { $($Variant:ident),* $(,)? }) => {{
293-
// Force a compile error if the list gets out of date.
294-
const _: fn(e: $Enum) = |e: $Enum| match e {
295-
$($Enum::$Variant => ()),*
296-
};
297-
match $prim {
298-
$(v if v == ($Enum::$Variant as _) => Some($Enum::$Variant),)*
299-
_ => None,
300-
}
301-
}}
286+
impl ErrorKind {
287+
// This compiles to the same code as the check+transmute, but doesn't require
288+
// unsafe, or to hard-code max ErrorKind or its size in a way the compiler
289+
// couldn't verify.
290+
#[inline]
291+
#[unstable(
292+
feature = "core_io_internals",
293+
reason = "exposed only for libstd",
294+
issue = "none"
295+
)]
296+
#[doc(hidden)]
297+
pub const fn from_prim(ek: u32) -> Option<Self> {
298+
macro_rules! from_prim {
299+
($prim:expr => $Enum:ident { $($Variant:ident),* $(,)? }) => {{
300+
// Force a compile error if the list gets out of date.
301+
const _: fn(e: $Enum) = |e: $Enum| match e {
302+
$($Enum::$Variant => (),)*
303+
};
304+
match $prim {
305+
$(v if v == ($Enum::$Variant as _) => Some($Enum::$Variant),)*
306+
_ => None,
307+
}
308+
}}
309+
}
310+
from_prim!(ek => ErrorKind {
311+
NotFound,
312+
PermissionDenied,
313+
ConnectionRefused,
314+
ConnectionReset,
315+
HostUnreachable,
316+
NetworkUnreachable,
317+
ConnectionAborted,
318+
NotConnected,
319+
AddrInUse,
320+
AddrNotAvailable,
321+
NetworkDown,
322+
BrokenPipe,
323+
AlreadyExists,
324+
WouldBlock,
325+
NotADirectory,
326+
IsADirectory,
327+
DirectoryNotEmpty,
328+
ReadOnlyFilesystem,
329+
FilesystemLoop,
330+
StaleNetworkFileHandle,
331+
InvalidInput,
332+
InvalidData,
333+
TimedOut,
334+
WriteZero,
335+
StorageFull,
336+
NotSeekable,
337+
QuotaExceeded,
338+
FileTooLarge,
339+
ResourceBusy,
340+
ExecutableFileBusy,
341+
Deadlock,
342+
CrossesDevices,
343+
TooManyLinks,
344+
InvalidFilename,
345+
ArgumentListTooLong,
346+
Interrupted,
347+
Other,
348+
UnexpectedEof,
349+
Unsupported,
350+
OutOfMemory,
351+
InProgress,
352+
Uncategorized,
353+
})
302354
}
303-
from_prim!(ek => ErrorKind {
304-
NotFound,
305-
PermissionDenied,
306-
ConnectionRefused,
307-
ConnectionReset,
308-
HostUnreachable,
309-
NetworkUnreachable,
310-
ConnectionAborted,
311-
NotConnected,
312-
AddrInUse,
313-
AddrNotAvailable,
314-
NetworkDown,
315-
BrokenPipe,
316-
AlreadyExists,
317-
WouldBlock,
318-
NotADirectory,
319-
IsADirectory,
320-
DirectoryNotEmpty,
321-
ReadOnlyFilesystem,
322-
FilesystemLoop,
323-
StaleNetworkFileHandle,
324-
InvalidInput,
325-
InvalidData,
326-
TimedOut,
327-
WriteZero,
328-
StorageFull,
329-
NotSeekable,
330-
QuotaExceeded,
331-
FileTooLarge,
332-
ResourceBusy,
333-
ExecutableFileBusy,
334-
Deadlock,
335-
CrossesDevices,
336-
TooManyLinks,
337-
InvalidFilename,
338-
ArgumentListTooLong,
339-
Interrupted,
340-
Other,
341-
UnexpectedEof,
342-
Unsupported,
343-
OutOfMemory,
344-
InProgress,
345-
Uncategorized,
346-
})
347355
}
348356

349357
// Some static checking to alert us if a change breaks any of the assumptions

library/std/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@
344344
#![feature(hashmap_internals)]
345345
#![feature(hint_must_use)]
346346
#![feature(int_from_ascii)]
347+
#![feature(io_error_inprogress)]
348+
#![feature(io_error_more)]
349+
#![feature(io_error_uncategorized)]
347350
#![feature(ip)]
348351
#![feature(iter_advance_by)]
349352
#![feature(iter_next_chunk)]

library/std/src/sys/io/error/sgx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ pub fn error_string(errno: i32) -> String {
6060
} else if ((Error::UserRangeStart as _)..=(Error::UserRangeEnd as _)).contains(&errno) {
6161
format!("user-specified error {errno:08x}")
6262
} else {
63-
decode_error_kind(errno).as_str().into()
63+
format!("{}", decode_error_kind(errno))
6464
}
6565
}

0 commit comments

Comments
 (0)