@@ -22,6 +22,9 @@ pub trait StackError: fmt::Display + fmt::Debug + Send + Sync {
2222 /// Returns this error as a std error reference.
2323 fn as_std ( & self ) -> & ( dyn std:: error:: Error + Send + Sync + ' static ) ;
2424
25+ /// Returns this error as a std error.
26+ fn into_std ( self : Box < Self > ) -> Box < dyn std:: error:: Error + Send + Sync > ;
27+
2528 /// Returns this error as a `dyn StackError`.
2629 fn as_dyn ( & self ) -> & dyn StackError ;
2730
@@ -86,18 +89,21 @@ impl<T: StackError + Sized + 'static> StackErrorExt for T {}
8689#[ derive( Copy , Clone , Debug ) ]
8790pub enum ErrorRef < ' a > {
8891 /// Std error (no location info).
89- Std ( & ' a dyn std:: error:: Error , Option < & ' a Meta > ) ,
92+ Std ( & ' a ( dyn std:: error:: Error + ' static ) , Option < & ' a Meta > ) ,
9093 /// StackError (has location info).
9194 Stack ( & ' a dyn StackError ) ,
9295}
9396
9497impl < ' a > ErrorRef < ' a > {
9598 /// Creates a [`ErrorRef`] for a std error.
96- pub fn std ( err : & dyn std:: error:: Error ) -> ErrorRef < ' _ > {
99+ pub fn std ( err : & ' a ( dyn std:: error:: Error + ' static ) ) -> ErrorRef < ' a > {
97100 ErrorRef :: Std ( err, None )
98101 }
99102
100- pub ( crate ) fn std_with_meta ( err : & ' a dyn std:: error:: Error , meta : & ' a Meta ) -> ErrorRef < ' a > {
103+ pub ( crate ) fn std_with_meta (
104+ err : & ' a ( dyn std:: error:: Error + ' static ) ,
105+ meta : & ' a Meta ,
106+ ) -> ErrorRef < ' a > {
101107 ErrorRef :: Std ( err, Some ( meta) )
102108 }
103109
@@ -115,7 +121,7 @@ impl<'a> ErrorRef<'a> {
115121 }
116122
117123 /// Returns the error as a std error.
118- pub fn as_std ( & self ) -> & dyn std:: error:: Error {
124+ pub fn as_std ( self ) -> & ' a dyn std:: error:: Error {
119125 match self {
120126 ErrorRef :: Std ( error, _) => error,
121127 ErrorRef :: Stack ( error) => error. as_std ( ) ,
@@ -150,6 +156,14 @@ impl<'a> ErrorRef<'a> {
150156 ErrorRef :: Stack ( error) => error. fmt_message ( f) ,
151157 }
152158 }
159+
160+ /// Downcast this error object by reference.
161+ pub fn downcast_ref < T : std:: error:: Error + ' static > ( self ) -> Option < & ' a T > {
162+ match self {
163+ ErrorRef :: Std ( error, _) => error. downcast_ref ( ) ,
164+ ErrorRef :: Stack ( error) => error. as_std ( ) . downcast_ref ( ) ,
165+ }
166+ }
153167}
154168
155169impl < ' a > fmt:: Display for ErrorRef < ' a > {
@@ -306,6 +320,10 @@ macro_rules! impl_stack_error_for_std_error {
306320 self
307321 }
308322
323+ fn into_std( self : Box <Self >) -> Box <dyn std:: error:: Error + Send + Sync > {
324+ self
325+ }
326+
309327 fn as_dyn( & self ) -> & dyn StackError {
310328 self
311329 }
@@ -336,54 +354,54 @@ impl_stack_error_for_std_error!(std::string::FromUtf8Error);
336354impl_stack_error_for_std_error ! ( std:: net:: AddrParseError ) ;
337355impl_stack_error_for_std_error ! ( std:: array:: TryFromSliceError ) ;
338356
339- impl StackError for Box < dyn StackError > {
340- fn as_std ( & self ) -> & ( dyn std:: error:: Error + Send + Sync + ' static ) {
341- StackError :: as_std ( & * * self )
342- }
343-
344- fn as_dyn ( & self ) -> & dyn StackError {
345- StackError :: as_dyn ( & * * self )
346- }
347-
348- fn meta ( & self ) -> Option < & Meta > {
349- StackError :: meta ( & * * self )
350- }
351-
352- fn source ( & self ) -> Option < ErrorRef < ' _ > > {
353- StackError :: source ( & * * self )
354- }
355-
356- fn fmt_message ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
357- StackError :: fmt_message ( & * * self , f)
358- }
359-
360- fn is_transparent ( & self ) -> bool {
361- StackError :: is_transparent ( & * * self )
362- }
363- }
364-
365- impl StackError for std:: sync:: Arc < dyn StackError > {
366- fn as_std ( & self ) -> & ( dyn std:: error:: Error + Send + Sync + ' static ) {
367- StackError :: as_std ( & * * self )
368- }
369-
370- fn as_dyn ( & self ) -> & dyn StackError {
371- StackError :: as_dyn ( & * * self )
372- }
373-
374- fn meta ( & self ) -> Option < & Meta > {
375- StackError :: meta ( & * * self )
376- }
377-
378- fn source ( & self ) -> Option < ErrorRef < ' _ > > {
379- StackError :: source ( & * * self )
380- }
381-
382- fn fmt_message ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
383- StackError :: fmt_message ( & * * self , f)
384- }
385-
386- fn is_transparent ( & self ) -> bool {
387- StackError :: is_transparent ( & * * self )
388- }
389- }
357+ // impl StackError for Box<dyn StackError> {
358+ // fn as_std(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
359+ // StackError::as_std(&**self)
360+ // }
361+
362+ // fn as_dyn(&self) -> &dyn StackError {
363+ // StackError::as_dyn(&**self)
364+ // }
365+
366+ // fn meta(&self) -> Option<&Meta> {
367+ // StackError::meta(&**self)
368+ // }
369+
370+ // fn source(&self) -> Option<ErrorRef<'_>> {
371+ // StackError::source(&**self)
372+ // }
373+
374+ // fn fmt_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
375+ // StackError::fmt_message(&**self, f)
376+ // }
377+
378+ // fn is_transparent(&self) -> bool {
379+ // StackError::is_transparent(&**self)
380+ // }
381+ // }
382+
383+ // impl StackError for std::sync::Arc<dyn StackError> {
384+ // fn as_std(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
385+ // StackError::as_std(&**self)
386+ // }
387+
388+ // fn as_dyn(&self) -> &dyn StackError {
389+ // StackError::as_dyn(&**self)
390+ // }
391+
392+ // fn meta(&self) -> Option<&Meta> {
393+ // StackError::meta(&**self)
394+ // }
395+
396+ // fn source(&self) -> Option<ErrorRef<'_>> {
397+ // StackError::source(&**self)
398+ // }
399+
400+ // fn fmt_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
401+ // StackError::fmt_message(&**self, f)
402+ // }
403+
404+ // fn is_transparent(&self) -> bool {
405+ // StackError::is_transparent(&**self)
406+ // }
407+ // }
0 commit comments