diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 3b4cb859dd425..9d4e4bbb93eb7 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -194,6 +194,43 @@ impl<'a, E: Error + 'a> From for Box { } } +#[unstable(feature = "box_error_send", issue = "none")] +impl<'a, E: Error + Send + 'a> From for Box { + /// Converts a type of [`Error`] + [`Send`] into a box of dyn [`Error`] + [`Send`]. + /// + /// [`Error`]: ../error/trait.Error.html + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::fmt; + /// use std::mem; + /// + /// #[derive(Debug)] + /// struct AnError; + /// + /// impl fmt::Display for AnError { + /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + /// write!(f , "An error") + /// } + /// } + /// + /// impl Error for AnError {} + /// + /// unsafe impl Send for AnError {} + /// + /// let an_error = AnError; + /// assert!(0 == mem::size_of_val(&an_error)); + /// let a_boxed_error = Box::::from(an_error); + /// assert!( + /// mem::size_of::>() == mem::size_of_val(&a_boxed_error)) + /// ``` + fn from(err: E) -> Box { + Box::new(err) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, E: Error + Send + Sync + 'a> From for Box { /// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of