@@ -502,6 +502,7 @@ use std::iter::Iterator;
502
502
#[ cfg( feature = "backtrace" ) ]
503
503
use std:: sync:: Arc ;
504
504
use std:: fmt;
505
+ use std:: marker:: PhantomData ;
505
506
506
507
#[ cfg( feature = "backtrace" ) ]
507
508
pub use backtrace:: Backtrace ;
@@ -558,7 +559,7 @@ pub fn make_backtrace() -> Option<Arc<Backtrace>> {
558
559
559
560
/// This trait is implemented on all the errors generated by the `error_chain`
560
561
/// macro.
561
- pub trait ChainedError : error:: Error + Send + ' static {
562
+ pub trait ChainedError < S : ? Sized > : error:: Error + Send + ' static {
562
563
/// Associated kind type.
563
564
type ErrorKind ;
564
565
@@ -585,7 +586,7 @@ pub trait ChainedError: error::Error + Send + 'static {
585
586
///
586
587
/// The full cause chain and backtrace, if present, will be printed.
587
588
fn display_chain < ' a > ( & ' a self ) -> DisplayChain < ' a , Self > {
588
- DisplayChain ( self )
589
+ DisplayChain ( self , PhantomData )
589
590
}
590
591
591
592
/// Extends the error chain with a new entry.
@@ -595,7 +596,7 @@ pub trait ChainedError: error::Error + Send + 'static {
595
596
596
597
/// Creates an error from its parts.
597
598
#[ doc( hidden) ]
598
- fn new ( kind : Self :: ErrorKind , state : State ) -> Self where Self : Sized ;
599
+ fn new ( kind : Self :: ErrorKind , state : State < S > ) -> Self where Self : Sized ;
599
600
600
601
/// Returns the first known backtrace, either from its State or from one
601
602
/// of the errors from `foreign_links`.
@@ -607,10 +608,17 @@ pub trait ChainedError: error::Error + Send + 'static {
607
608
608
609
/// A struct which formats an error for output.
609
610
#[ derive( Debug ) ]
611
+ <<<<<<< HEAD
610
612
pub struct DisplayChain < ' a , T : ' a + ?Sized > ( & ' a T ) ;
611
613
612
614
impl <' a , T > fmt:: Display for DisplayChain < ' a , T >
613
615
where T : ChainedError
616
+ =======
617
+ pub struct Display < ' a , T : ' a + ?Sized , S : ?Sized > ( & ' a T , PhantomData < S > ) ;
618
+
619
+ impl <' a , T , S > fmt:: Display for Display < ' a , T , S >
620
+ where T : ChainedError < S >
621
+ >>>>>>> Add generic parameter to some types ( errors can now add trait bounds)
614
622
{
615
623
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
616
624
// Keep `try!` for 1.10 support
@@ -631,33 +639,35 @@ impl<'a, T> fmt::Display for DisplayChain<'a, T>
631
639
/// Common state between errors.
632
640
#[ derive( Debug ) ]
633
641
#[ doc( hidden) ]
634
- pub struct State {
642
+ pub struct State < T : ? Sized > {
635
643
/// Next error in the error chain.
636
- pub next_error : Option < Box < error :: Error + Send > > ,
644
+ pub next_error : Option < Box < T > > ,
637
645
/// Backtrace for the current error.
638
646
#[ cfg( feature = "backtrace" ) ]
639
647
pub backtrace : Option < Arc < Backtrace > > ,
640
648
}
641
649
642
- impl Default for State {
650
+ impl < T > Default for State < T > {
643
651
#[ cfg( feature = "backtrace" ) ]
644
- fn default ( ) -> State {
652
+ fn default( ) -> Self {
645
653
State {
646
654
next_error : None ,
647
655
backtrace : make_backtrace( ) ,
648
656
}
649
657
}
650
658
651
659
#[ cfg( not( feature = "backtrace" ) ) ]
652
- fn default ( ) -> State {
660
+ fn default( ) -> Self {
653
661
State { next_error : None }
654
662
}
655
663
}
656
664
657
- impl State {
665
+ impl <T > State < T >
666
+ where T : error:: Error + Send + ' static
667
+ {
658
668
/// Creates a new State type
659
669
#[ cfg( feature = "backtrace" ) ]
660
- pub fn new < CE : ChainedError > ( e : Box < error :: Error + Send > ) -> State {
670
+ pub fn new< CE : ChainedError < T > > ( e : Box < T > ) -> Self {
661
671
let backtrace = CE :: extract_backtrace ( & * e ) . or_else( make_backtrace) ;
662
672
State {
663
673
next_error : Some ( e) ,
@@ -667,7 +677,7 @@ impl State {
667
677
668
678
/// Creates a new State type
669
679
#[ cfg( not( feature = "backtrace" ) ) ]
670
- pub fn new < CE : ChainedError > ( e : Box < error:: Error + Send > ) -> State {
680
+ pub fn new < CE : ChainedError > ( e : Box < error:: Error + Send > ) -> Self {
671
681
State { next_error: Some ( e) }
672
682
}
673
683
0 commit comments