diff options
| author | Ibraheem Ahmed <ibrah1440@gmail.com> | 2021-12-14 20:00:59 -0500 |
|---|---|---|
| committer | Ibraheem Ahmed <ibrah1440@gmail.com> | 2021-12-14 20:00:59 -0500 |
| commit | 85f786cc9c85473166fee1daec2dfc550f46bed8 (patch) | |
| tree | 93ce4f5e7f850d1f45c312f6aabcec675c0c6c1c | |
| parent | 477fd7038c235689913abf9208dfa9371cbacd88 (diff) | |
| download | rust-85f786cc9c85473166fee1daec2dfc550f46bed8.tar.gz rust-85f786cc9c85473166fee1daec2dfc550f46bed8.zip | |
add `io::Error::other` constructor
| -rw-r--r-- | library/std/src/io/error.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index da88c8c9261..210a9ec7183 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -417,6 +417,33 @@ impl Error { Self::_new(kind, error.into()) } + /// Creates a new I/O error from an arbitrary error payload. + /// + /// This function is used to generically create I/O errors which do not + /// originate from the OS itself. It is a shortcut for [`Error::new`] + /// with [`ErrorKind::Other`]. + /// + /// # Examples + /// + /// ``` + /// #![feature(io_error_other)] + /// + /// use std::io::Error; + /// + /// // errors can be created from strings + /// let custom_error = Error::other("oh no!"); + /// + /// // errors can also be created from other errors + /// let custom_error2 = Error::other(custom_error); + /// ``` + #[unstable(feature = "io_error_other", issue = "91946")] + pub fn other<E>(error: E) -> Error + where + E: Into<Box<dyn error::Error + Send + Sync>>, + { + Self::_new(ErrorKind::Other, error.into()) + } + fn _new(kind: ErrorKind, error: Box<dyn error::Error + Send + Sync>) -> Error { Error { repr: Repr::Custom(Box::new(Custom { kind, error })) } } |
