diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 29534696abc5b..1b872664e8d13 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -152,6 +152,35 @@ pub trait Error: Debug + Display { #[stable(feature = "rust1", since = "1.0.0")] impl<'a, E: Error + 'a> From for Box { + /// Converts a type of [`Error`] into a box of dyn [`Error`]. + /// + /// # 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 { + /// fn description(&self) -> &str { + /// "Description of an error" + /// } + /// } + /// + /// 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) } @@ -159,6 +188,41 @@ impl<'a, E: Error + 'a> From for Box { #[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 dyn [`Error`] + + /// [`Send`] + [`Sync`]. + /// + /// # 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 { + /// fn description(&self) -> &str { + /// "Description of an error" + /// } + /// } + /// + /// unsafe impl Send for AnError {} + /// + /// unsafe impl Sync 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) } @@ -166,6 +230,19 @@ impl<'a, E: Error + Send + Sync + 'a> From for Box for Box { + /// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`]. + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::mem; + /// + /// let a_string_error = "a string error".to_string(); + /// let a_boxed_error = Box::::from(a_string_error); + /// assert!( + /// mem::size_of::>() == mem::size_of_val(&a_boxed_error)) + /// ``` fn from(err: String) -> Box { #[derive(Debug)] struct StringError(String); @@ -186,6 +263,18 @@ impl From for Box { #[stable(feature = "string_box_error", since = "1.6.0")] impl From for Box { + /// Converts a [`String`] into a box of dyn [`Error`]. + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::mem; + /// + /// let a_string_error = "a string error".to_string(); + /// let a_boxed_error = Box::::from(a_string_error); + /// assert!(mem::size_of::>() == mem::size_of_val(&a_boxed_error)) + /// ``` fn from(str_err: String) -> Box { let err1: Box = From::from(str_err); let err2: Box = err1; @@ -195,6 +284,19 @@ impl From for Box { #[stable(feature = "rust1", since = "1.0.0")] impl<'a, 'b> From<&'b str> for Box { + /// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`]. + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::mem; + /// + /// let a_str_error = "a str error"; + /// let a_boxed_error = Box::::from(a_str_error); + /// assert!( + /// mem::size_of::>() == mem::size_of_val(&a_boxed_error)) + /// ``` fn from(err: &'b str) -> Box { From::from(String::from(err)) } @@ -202,6 +304,18 @@ impl<'a, 'b> From<&'b str> for Box { #[stable(feature = "string_box_error", since = "1.6.0")] impl<'a> From<&'a str> for Box { + /// Converts a [`str`] into a box of dyn [`Error`]. + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::mem; + /// + /// let a_str_error = "a str error"; + /// let a_boxed_error = Box::::from(a_str_error); + /// assert!(mem::size_of::>() == mem::size_of_val(&a_boxed_error)) + /// ``` fn from(err: &'a str) -> Box { From::from(String::from(err)) } @@ -209,6 +323,20 @@ impl<'a> From<&'a str> for Box { #[stable(feature = "cow_box_error", since = "1.22.0")] impl<'a, 'b> From> for Box { + /// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`]. + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::mem; + /// use std::borrow::Cow; + /// + /// let a_cow_str_error = Cow::from("a str error"); + /// let a_boxed_error = Box::::from(a_cow_str_error); + /// assert!( + /// mem::size_of::>() == mem::size_of_val(&a_boxed_error)) + /// ``` fn from(err: Cow<'b, str>) -> Box { From::from(String::from(err)) } @@ -216,6 +344,19 @@ impl<'a, 'b> From> for Box { #[stable(feature = "cow_box_error", since = "1.22.0")] impl<'a> From> for Box { + /// Converts a [`Cow`] into a box of dyn [`Error`]. + /// + /// # Examples + /// + /// ``` + /// use std::error::Error; + /// use std::mem; + /// use std::borrow::Cow; + /// + /// let a_cow_str_error = Cow::from("a str error"); + /// let a_boxed_error = Box::::from(a_cow_str_error); + /// assert!(mem::size_of::>() == mem::size_of_val(&a_boxed_error)) + /// ``` fn from(err: Cow<'a, str>) -> Box { From::from(String::from(err)) }