539
539
use std:: error;
540
540
use std:: iter:: Iterator ;
541
541
use std:: fmt;
542
+ use std:: marker:: PhantomData ;
542
543
543
544
#[ macro_use]
544
545
mod impl_error_chain_kind;
@@ -581,18 +582,21 @@ impl<'a> Iterator for Iter<'a> {
581
582
582
583
/// This trait is implemented on all the errors generated by the `error_chain`
583
584
/// macro.
584
- pub trait ChainedError : error:: Error + Send + ' static {
585
+ pub trait ChainedError < S : ? Sized > : error:: Error + Send + ' static {
585
586
/// Associated kind type.
586
587
type ErrorKind ;
587
588
588
589
/// Constructs an error from a kind, and generates a backtrace.
589
- fn from_kind ( kind : Self :: ErrorKind ) -> Self where Self : Sized ;
590
+ fn from_kind ( kind : Self :: ErrorKind ) -> Self
591
+ where
592
+ Self : Sized ;
590
593
591
594
/// Constructs a chained error from another error and a kind, and generates a backtrace.
592
595
fn with_chain < E , K > ( error : E , kind : K ) -> Self
593
- where Self : Sized ,
594
- E : :: std:: error:: Error + Send + ' static ,
595
- K : Into < Self :: ErrorKind > ;
596
+ where
597
+ Self : Sized ,
598
+ E : :: std:: error:: Error + Send + ' static ,
599
+ K : Into < Self :: ErrorKind > ;
596
600
597
601
/// Returns the kind of the error.
598
602
fn kind ( & self ) -> & Self :: ErrorKind ;
@@ -608,31 +612,36 @@ pub trait ChainedError: error::Error + Send + 'static {
608
612
///
609
613
/// The full cause chain and backtrace, if present, will be printed.
610
614
fn display_chain < ' a > ( & ' a self ) -> DisplayChain < ' a , Self > {
611
- DisplayChain ( self )
615
+ DisplayChain ( self , PhantomData )
612
616
}
613
617
614
618
/// Extends the error chain with a new entry.
615
619
fn chain_err < F , EK > ( self , error : F ) -> Self
616
- where F : FnOnce ( ) -> EK ,
617
- EK : Into < Self :: ErrorKind > ;
620
+ where
621
+ F : FnOnce ( ) -> EK ,
622
+ EK : Into < Self :: ErrorKind > ;
618
623
619
624
/// Creates an error from its parts.
620
625
#[ doc( hidden) ]
621
- fn new ( kind : Self :: ErrorKind , state : State ) -> Self where Self : Sized ;
626
+ fn new ( kind : Self :: ErrorKind , state : State < S > ) -> Self
627
+ where
628
+ Self : Sized ;
622
629
623
630
/// Returns the first known backtrace, either from its State or from one
624
631
/// of the errors from `foreign_links`.
625
632
#[ doc( hidden) ]
626
633
fn extract_backtrace ( e : & ( error:: Error + Send + ' static ) ) -> Option < InternalBacktrace >
627
- where Self : Sized ;
634
+ where
635
+ Self : Sized ;
628
636
}
629
637
630
638
/// A struct which formats an error for output.
631
639
#[ derive( Debug ) ]
632
- pub struct DisplayChain < ' a , T : ' a + ?Sized > ( & ' a T ) ;
640
+ pub struct DisplayChain < ' a , T : ' a + ?Sized , S : ? Sized > ( & ' a T , PhantomData < S > ) ;
633
641
634
- impl < ' a , T > fmt:: Display for DisplayChain < ' a , T >
635
- where T : ChainedError
642
+ impl < ' a , T , S > fmt:: Display for DisplayChain < ' a , T , S >
643
+ where
644
+ T : ChainedError < S > ,
636
645
{
637
646
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
638
647
// Keep `try!` for 1.10 support
@@ -653,37 +662,35 @@ impl<'a, T> fmt::Display for DisplayChain<'a, T>
653
662
/// Common state between errors.
654
663
#[ derive( Debug ) ]
655
664
#[ doc( hidden) ]
656
- pub struct State {
665
+ pub struct State < T : ? Sized > {
657
666
/// Next error in the error chain.
658
- pub next_error : Option < Box < error :: Error + Send > > ,
667
+ pub next_error : Option < Box < T > > ,
659
668
/// Backtrace for the current error.
660
669
pub backtrace : InternalBacktrace ,
661
670
}
662
671
663
- impl Default for State {
664
- fn default ( ) -> State {
672
+ impl < T > Default for State < T > {
673
+ #[ cfg( feature = "backtrace" ) ]
674
+ fn default ( ) -> Self {
665
675
State {
666
676
next_error : None ,
667
677
backtrace : InternalBacktrace :: new ( ) ,
668
678
}
669
679
}
670
680
}
671
681
672
- impl State {
682
+ impl < T > State < T >
683
+ where
684
+ T : error:: Error + Send + ' static ,
685
+ {
673
686
/// Creates a new State type
674
687
pub fn new < CE : ChainedError > ( e : Box < error:: Error + Send > ) -> State {
675
- let backtrace = CE :: extract_backtrace ( & * e)
676
- . unwrap_or_else ( InternalBacktrace :: new) ;
688
+ let backtrace = CE :: extract_backtrace ( & * e) . unwrap_or_else ( InternalBacktrace :: new) ;
677
689
State {
678
690
next_error : Some ( e) ,
679
691
backtrace : backtrace,
680
692
}
681
693
}
682
-
683
- /// Returns the inner backtrace if present.
684
- pub fn backtrace ( & self ) -> Option < & Backtrace > {
685
- self . backtrace . as_backtrace ( )
686
- }
687
694
}
688
695
689
696
/// Exits a function early with an error
0 commit comments