@@ -509,32 +509,19 @@ macro_rules! error_chain {
509
509
$error_name( kind, backtrace)
510
510
}
511
511
512
- fn backtrace_from_box( mut e: Box <:: std:: error:: Error + Send + ' static >)
513
- -> ( Box <:: std:: error:: Error + Send + ' static >,
514
- Option <Option <:: std:: sync:: Arc <$crate:: Backtrace >>>) {
515
- let mut backtrace = None ;
516
-
517
- e = match e. downcast:: <$error_name>( ) {
518
- Err ( e) => e,
519
- Ok ( e) => {
520
- backtrace = Some ( ( e. 1 ) . 1 . clone( ) ) ;
521
- e as Box <:: std:: error:: Error + Send + ' static >
522
- }
523
- } ;
524
-
512
+ fn backtrace_from_box( e: & ( :: std:: error:: Error + Send + ' static ) )
513
+ -> Option <Option <:: std:: sync:: Arc <$crate:: Backtrace >>> {
514
+ if let Some ( e) = e. downcast_ref:: <$error_name>( ) {
515
+ Some ( ( e. 1 ) . 1 . clone( ) )
516
+ }
525
517
$(
526
-
527
- e = match e. downcast:: <$link_error_path>( ) {
528
- Err ( e) => e,
529
- Ok ( e) => {
530
- backtrace = Some ( ( e. 1 ) . 1 . clone( ) ) ;
531
- e as Box <:: std:: error:: Error + Send + ' static >
532
- }
533
- } ;
534
-
518
+ else if let Some ( e) = e. downcast_ref:: <$link_error_path>( ) {
519
+ Some ( ( e. 1 ) . 1 . clone( ) )
520
+ }
535
521
) *
536
-
537
- ( e, backtrace)
522
+ else {
523
+ None
524
+ }
538
525
}
539
526
}
540
527
@@ -700,8 +687,8 @@ pub trait Error: error::Error + Send + 'static {
700
687
/// to avoid generating a new one. It would be better to not
701
688
/// define this in the macro, but types need some additional
702
689
/// machinery to make it work.
703
- fn backtrace_from_box ( e : Box < error:: Error + Send + ' static > )
704
- -> ( Box < error :: Error + Send + ' static > , Option < Option < Arc < Backtrace > > > ) ;
690
+ fn backtrace_from_box ( e : & ( error:: Error + Send + ' static ) )
691
+ -> Option < Option < Arc < Backtrace > > > ;
705
692
}
706
693
707
694
/// Additionnal methods for `Result`, for easy interaction with this crate.
@@ -720,11 +707,10 @@ impl<T, E> ResultExt<T, E> for Result<T, E> where E: Error {
720
707
where F : FnOnce ( ) -> EK ,
721
708
EK : Into < E :: ErrorKind > {
722
709
self . map_err ( move |e| {
723
- let e = Box :: new ( e) as Box < error:: Error + Send + ' static > ;
724
- let ( e, backtrace) = E :: backtrace_from_box ( e) ;
725
- let backtrace = backtrace. unwrap_or_else ( make_backtrace) ;
710
+ let backtrace =
711
+ E :: backtrace_from_box ( & e) . unwrap_or_else ( make_backtrace) ;
726
712
727
- E :: new ( callback ( ) . into ( ) , ( Some ( e ) , backtrace) )
713
+ E :: new ( callback ( ) . into ( ) , ( Some ( Box :: new ( e ) ) , backtrace) )
728
714
} )
729
715
}
730
716
}
0 commit comments