diff options
| author | Joshua Nelson <jnelson@cloudflare.com> | 2022-01-19 13:43:27 -0600 |
|---|---|---|
| committer | Joshua Nelson <jnelson@cloudflare.com> | 2022-01-19 13:47:44 -0600 |
| commit | f8ee57be2c585c3fd8931eeb88994fbfcaa0f083 (patch) | |
| tree | ed6ae9eadc7b91f340678db49cd55b17a15a39da /library/std/src/io | |
| parent | 5e57faa78aa7661c6000204591558f6665f11abc (diff) | |
| download | rust-f8ee57be2c585c3fd8931eeb88994fbfcaa0f083.tar.gz rust-f8ee57be2c585c3fd8931eeb88994fbfcaa0f083.zip | |
`impl Display for io::ErrorKind`
This avoids having to convert from `ErrorKind` to `Error` just to print the error message.
Diffstat (limited to 'library/std/src/io')
| -rw-r--r-- | library/std/src/io/error.rs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 210a9ec7183..074d693b831 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -361,13 +361,29 @@ impl ErrorKind { } } +#[stable(feature = "io_errorkind_display", since = "1.60.0")] +impl fmt::Display for ErrorKind { + /// Shows a human-readable description of the `ErrorKind`. + /// + /// This is similar to `impl Display for Error`, but doesn't require first converting to Error. + /// + /// # Examples + /// ``` + /// use std::io::ErrorKind; + /// assert_eq!("entity not found", ErrorKind::NotFound.to_string()); + /// ``` + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str(self.as_str()) + } +} + /// Intended for use for errors not exposed to the user, where allocating onto /// the heap (for normal construction via Error::new) is too costly. #[stable(feature = "io_error_from_errorkind", since = "1.14.0")] impl From<ErrorKind> for Error { /// Converts an [`ErrorKind`] into an [`Error`]. /// - /// This conversion allocates a new error with a simple representation of error kind. + /// This conversion creates a new error with a simple representation of error kind. /// /// # Examples /// |
