Skip to content

Commit 8604161

Browse files
authored
Rollup merge of #93090 - jyn514:errorkind-asstr, r=dtolnay
`impl Display for io::ErrorKind` This avoids having to convert from `ErrorKind` to `Error` just to print the error message.
2 parents d7c0b4f + f8ee57b commit 8604161

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

library/std/src/io/error.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,29 @@ impl ErrorKind {
361361
}
362362
}
363363

364+
#[stable(feature = "io_errorkind_display", since = "1.60.0")]
365+
impl fmt::Display for ErrorKind {
366+
/// Shows a human-readable description of the `ErrorKind`.
367+
///
368+
/// This is similar to `impl Display for Error`, but doesn't require first converting to Error.
369+
///
370+
/// # Examples
371+
/// ```
372+
/// use std::io::ErrorKind;
373+
/// assert_eq!("entity not found", ErrorKind::NotFound.to_string());
374+
/// ```
375+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
376+
fmt.write_str(self.as_str())
377+
}
378+
}
379+
364380
/// Intended for use for errors not exposed to the user, where allocating onto
365381
/// the heap (for normal construction via Error::new) is too costly.
366382
#[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
367383
impl From<ErrorKind> for Error {
368384
/// Converts an [`ErrorKind`] into an [`Error`].
369385
///
370-
/// This conversion allocates a new error with a simple representation of error kind.
386+
/// This conversion creates a new error with a simple representation of error kind.
371387
///
372388
/// # Examples
373389
///

0 commit comments

Comments
 (0)