@@ -359,8 +359,7 @@ macro_rules! error_chain {
359
359
/// that is mostly irrelevant for error handling purposes.
360
360
#[ derive( Debug ) ]
361
361
pub struct $error_name( pub $error_kind_name,
362
- pub ( Option <Box <:: std:: error:: Error + Send >>,
363
- Option <:: std:: sync:: Arc <$crate:: Backtrace >>) ) ;
362
+ pub $crate:: State ) ;
364
363
365
364
impl $error_name {
366
365
/// Returns the kind of the error.
@@ -375,14 +374,14 @@ macro_rules! error_chain {
375
374
376
375
/// Returns the backtrace associated with this error.
377
376
pub fn backtrace( & self ) -> Option <& $crate:: Backtrace > {
378
- ( self . 1 ) . 1 . as_ref( ) . map( |v| & * * v)
377
+ self . 1 . backtrace . as_ref( ) . map( |v| & * * v)
379
378
}
380
379
}
381
380
382
381
impl :: std:: error:: Error for $error_name {
383
382
fn description( & self ) -> & str { self . 0 . description( ) }
384
383
fn cause( & self ) -> Option <& :: std:: error:: Error > {
385
- match ( self . 1 ) . 0 {
384
+ match self . 1 . next_error {
386
385
Some ( ref c) => Some ( & * * c) ,
387
386
None => {
388
387
match self . 0 {
@@ -420,29 +419,26 @@ macro_rules! error_chain {
420
419
fn from( e: $foreign_link_error_path) -> Self {
421
420
$error_name(
422
421
$error_kind_name:: $foreign_link_variant( e) ,
423
- ( None , $crate:: make_backtrace ( ) ) )
422
+ $crate:: State :: default ( ) )
424
423
}
425
424
}
426
425
) *
427
426
428
427
impl From <$error_kind_name> for $error_name {
429
428
fn from( e: $error_kind_name) -> Self {
430
- $error_name( e,
431
- ( None , $crate:: make_backtrace( ) ) )
429
+ $error_name( e, $crate:: State :: default ( ) )
432
430
}
433
431
}
434
432
435
433
impl <' a> From <& ' a str > for $error_name {
436
434
fn from( s: & ' a str ) -> Self {
437
- $error_name( s. into( ) ,
438
- ( None , $crate:: make_backtrace( ) ) )
435
+ $error_name( s. into( ) , $crate:: State :: default ( ) )
439
436
}
440
437
}
441
438
442
439
impl From <String > for $error_name {
443
440
fn from( s: String ) -> Self {
444
- $error_name( s. into( ) ,
445
- ( None , $crate:: make_backtrace( ) ) )
441
+ $error_name( s. into( ) , $crate:: State :: default ( ) )
446
442
}
447
443
}
448
444
@@ -518,19 +514,18 @@ macro_rules! error_chain {
518
514
impl $crate:: ChainedError for $error_name {
519
515
type ErrorKind = $error_kind_name;
520
516
521
- fn new( kind: $error_kind_name, backtrace: ( Option <Box <:: std:: error:: Error + Send + ' static >>,
522
- Option <:: std:: sync:: Arc <$crate:: Backtrace >>) ) -> $error_name {
523
- $error_name( kind, backtrace)
517
+ fn new( kind: $error_kind_name, state: $crate:: State ) -> $error_name {
518
+ $error_name( kind, state)
524
519
}
525
520
526
521
fn extract_backtrace( e: & ( :: std:: error:: Error + Send + ' static ) )
527
522
-> Option <Option <:: std:: sync:: Arc <$crate:: Backtrace >>> {
528
523
if let Some ( e) = e. downcast_ref:: <$error_name>( ) {
529
- Some ( ( e. 1 ) . 1 . clone( ) )
524
+ Some ( e. 1 . backtrace . clone( ) )
530
525
}
531
526
$(
532
527
else if let Some ( e) = e. downcast_ref:: <$link_error_path>( ) {
533
- Some ( ( e. 1 ) . 1 . clone( ) )
528
+ Some ( e. 1 . backtrace . clone( ) )
534
529
}
535
530
) *
536
531
else {
@@ -695,9 +690,7 @@ pub trait ChainedError: error::Error + Send + 'static {
695
690
/// Associated kind type.
696
691
type ErrorKind ;
697
692
/// Creates an error from it's parts.
698
- fn new ( kind : Self :: ErrorKind ,
699
- state : ( Option < Box < error:: Error + Send + ' static > > ,
700
- Option < Arc < Backtrace > > ) ) -> Self ;
693
+ fn new ( kind : Self :: ErrorKind , state : State ) -> Self ;
701
694
/// Use downcasts to extract the backtrace from types we know,
702
695
/// to avoid generating a new one. It would be better to not
703
696
/// define this in the macro, but types need some additional
@@ -725,7 +718,28 @@ impl<T, E> ResultExt<T, E> for Result<T, E> where E: ChainedError {
725
718
let backtrace =
726
719
E :: extract_backtrace ( & e) . unwrap_or_else ( make_backtrace) ;
727
720
728
- E :: new ( callback ( ) . into ( ) , ( Some ( Box :: new ( e) ) , backtrace) )
721
+ E :: new ( callback ( ) . into ( ) , State {
722
+ next_error : Some ( Box :: new ( e) ) ,
723
+ backtrace : backtrace,
724
+ } )
729
725
} )
730
726
}
731
727
}
728
+
729
+ /// Common state between errors.
730
+ #[ derive( Debug ) ]
731
+ pub struct State {
732
+ /// Next error in the error chain.
733
+ pub next_error : Option < Box < error:: Error + Send > > ,
734
+ /// Backtrace for the current error.
735
+ pub backtrace : Option < Arc < Backtrace > > ,
736
+ }
737
+
738
+ impl Default for State {
739
+ fn default ( ) -> State {
740
+ State {
741
+ next_error : None ,
742
+ backtrace : make_backtrace ( ) ,
743
+ }
744
+ }
745
+ }
0 commit comments