@@ -153,6 +153,36 @@ pub trait Error: Debug + Display {
153153#[ stable( feature = "rust1" , since = "1.0.0" ) ]
154154impl < ' a , E : Error + ' a > From < E > for Box < dyn Error + ' a > {
155155 /// Converts a type of [`Error`] into a box of dyn [`Error`].
156+ ///
157+ /// # Examples
158+ ///
159+ /// ```
160+ /// use std::error::Error;
161+ /// use std::fmt;
162+ /// use std::mem;
163+ ///
164+ /// #[derive(Debug)]
165+ /// struct AnError;
166+ ///
167+ /// impl fmt::Display for AnError {
168+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169+ /// write!(f , "An error")
170+ /// }
171+ /// }
172+ ///
173+ /// impl Error for AnError {
174+ /// fn description(&self) -> &str {
175+ /// "Description of an error"
176+ /// }
177+ /// }
178+ ///
179+ /// fn main() {
180+ /// let an_error = AnError;
181+ /// assert!(0 == mem::size_of_val(&an_error));
182+ /// let a_boxed_error = Box::<Error>::from(an_error);
183+ /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
184+ /// }
185+ /// ```
156186 fn from ( err : E ) -> Box < dyn Error + ' a > {
157187 Box :: new ( err)
158188 }
@@ -162,6 +192,41 @@ impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
162192impl < ' a , E : Error + Send + Sync + ' a > From < E > for Box < dyn Error + Send + Sync + ' a > {
163193 /// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of dyn [`Error`] +
164194 /// [`Send`] + [`Sync`].
195+ ///
196+ /// # Examples
197+ ///
198+ /// ```
199+ /// use std::error::Error;
200+ /// use std::fmt;
201+ /// use std::mem;
202+ ///
203+ /// #[derive(Debug)]
204+ /// struct AnError;
205+ ///
206+ /// impl fmt::Display for AnError {
207+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
208+ /// write!(f , "An error")
209+ /// }
210+ /// }
211+ ///
212+ /// impl Error for AnError {
213+ /// fn description(&self) -> &str {
214+ /// "Description of an error"
215+ /// }
216+ /// }
217+ ///
218+ /// unsafe impl Send for AnError {}
219+ ///
220+ /// unsafe impl Sync for AnError {}
221+ ///
222+ /// fn main() {
223+ /// let an_error = AnError;
224+ /// assert!(0 == mem::size_of_val(&an_error));
225+ /// let a_boxed_error = Box::<Error + Send + Sync>::from(an_error);
226+ /// assert!(
227+ /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
228+ /// }
229+ /// ```
165230 fn from ( err : E ) -> Box < dyn Error + Send + Sync + ' a > {
166231 Box :: new ( err)
167232 }
@@ -170,6 +235,20 @@ impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync +
170235#[ stable( feature = "rust1" , since = "1.0.0" ) ]
171236impl From < String > for Box < dyn Error + Send + Sync > {
172237 /// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
238+ ///
239+ /// # Examples
240+ ///
241+ /// ```
242+ /// use std::error::Error;
243+ /// use std::mem;
244+ ///
245+ /// fn main() {
246+ /// let a_string_error = "a string error".to_string();
247+ /// let a_boxed_error = Box::<Error + Send + Sync>::from(a_string_error);
248+ /// assert!(
249+ /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
250+ /// }
251+ /// ```
173252 fn from ( err : String ) -> Box < dyn Error + Send + Sync > {
174253 #[ derive( Debug ) ]
175254 struct StringError ( String ) ;
@@ -191,6 +270,19 @@ impl From<String> for Box<dyn Error + Send + Sync> {
191270#[ stable( feature = "string_box_error" , since = "1.6.0" ) ]
192271impl From < String > for Box < dyn Error > {
193272 /// Converts a [`String`] into a box of dyn [`Error`].
273+ ///
274+ /// # Examples
275+ ///
276+ /// ```
277+ /// use std::error::Error;
278+ /// use std::mem;
279+ ///
280+ /// fn main() {
281+ /// let a_string_error = "a string error".to_string();
282+ /// let a_boxed_error = Box::<Error>::from(a_string_error);
283+ /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
284+ /// }
285+ /// ```
194286 fn from ( str_err : String ) -> Box < dyn Error > {
195287 let err1: Box < dyn Error + Send + Sync > = From :: from ( str_err) ;
196288 let err2: Box < dyn Error > = err1;
@@ -201,6 +293,20 @@ impl From<String> for Box<dyn Error> {
201293#[ stable( feature = "rust1" , since = "1.0.0" ) ]
202294impl < ' a , ' b > From < & ' b str > for Box < dyn Error + Send + Sync + ' a > {
203295 /// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
296+ ///
297+ /// # Examples
298+ ///
299+ /// ```
300+ /// use std::error::Error;
301+ /// use std::mem;
302+ ///
303+ /// fn main() {
304+ /// let a_str_error = "a str error";
305+ /// let a_boxed_error = Box::<Error + Send + Sync>::from(a_str_error);
306+ /// assert!(
307+ /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
308+ /// }
309+ /// ```
204310 fn from ( err : & ' b str ) -> Box < dyn Error + Send + Sync + ' a > {
205311 From :: from ( String :: from ( err) )
206312 }
@@ -209,6 +315,19 @@ impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
209315#[ stable( feature = "string_box_error" , since = "1.6.0" ) ]
210316impl < ' a > From < & ' a str > for Box < dyn Error > {
211317 /// Converts a [`str`] into a box of dyn [`Error`].
318+ ///
319+ /// # Examples
320+ ///
321+ /// ```
322+ /// use std::error::Error;
323+ /// use std::mem;
324+ ///
325+ /// fn main() {
326+ /// let a_str_error = "a str error";
327+ /// let a_boxed_error = Box::<Error>::from(a_str_error);
328+ /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
329+ /// }
330+ /// ```
212331 fn from ( err : & ' a str ) -> Box < dyn Error > {
213332 From :: from ( String :: from ( err) )
214333 }
@@ -217,6 +336,21 @@ impl<'a> From<&'a str> for Box<dyn Error> {
217336#[ stable( feature = "cow_box_error" , since = "1.22.0" ) ]
218337impl < ' a , ' b > From < Cow < ' b , str > > for Box < dyn Error + Send + Sync + ' a > {
219338 /// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
339+ ///
340+ /// # Examples
341+ ///
342+ /// ```
343+ /// use std::error::Error;
344+ /// use std::mem;
345+ /// use std::borrow::Cow;
346+ ///
347+ /// fn main() {
348+ /// let a_cow_str_error = Cow::from("a str error");
349+ /// let a_boxed_error = Box::<Error + Send + Sync>::from(a_cow_str_error);
350+ /// assert!(
351+ /// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
352+ /// }
353+ /// ```
220354 fn from ( err : Cow < ' b , str > ) -> Box < dyn Error + Send + Sync + ' a > {
221355 From :: from ( String :: from ( err) )
222356 }
@@ -225,6 +359,20 @@ impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
225359#[ stable( feature = "cow_box_error" , since = "1.22.0" ) ]
226360impl < ' a > From < Cow < ' a , str > > for Box < dyn Error > {
227361 /// Converts a [`Cow`] into a box of dyn [`Error`].
362+ ///
363+ /// # Examples
364+ ///
365+ /// ```
366+ /// use std::error::Error;
367+ /// use std::mem;
368+ /// use std::borrow::Cow;
369+ ///
370+ /// fn main() {
371+ /// let a_cow_str_error = Cow::from("a str error");
372+ /// let a_boxed_error = Box::<Error>::from(a_cow_str_error);
373+ /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
374+ /// }
375+ /// ```
228376 fn from ( err : Cow < ' a , str > ) -> Box < dyn Error > {
229377 From :: from ( String :: from ( err) )
230378 }
0 commit comments