@@ -429,6 +429,7 @@ use std::iter::Iterator;
429
429
#[ cfg( feature = "backtrace" ) ]
430
430
use std:: sync:: Arc ;
431
431
use std:: fmt;
432
+ use std:: marker:: PhantomData ;
432
433
433
434
#[ cfg( feature = "backtrace" ) ]
434
435
pub use backtrace:: Backtrace ;
@@ -477,7 +478,7 @@ pub fn make_backtrace() -> Option<Arc<Backtrace>> {
477
478
478
479
/// This trait is implemented on all the errors generated by the `error_chain`
479
480
/// macro.
480
- pub trait ChainedError : error:: Error + Send + ' static {
481
+ pub trait ChainedError < S : ? Sized > : error:: Error + Send + ' static {
481
482
/// Associated kind type.
482
483
type ErrorKind ;
483
484
@@ -503,8 +504,8 @@ pub trait ChainedError: error::Error + Send + 'static {
503
504
/// context of this error.
504
505
///
505
506
/// The full cause chain and backtrace, if present, will be printed.
506
- fn display < ' a > ( & ' a self ) -> Display < ' a , Self > {
507
- Display ( self )
507
+ fn display < ' a > ( & ' a self ) -> Display < ' a , Self , S > {
508
+ Display ( self , PhantomData )
508
509
}
509
510
510
511
/// Extends the error chain with a new entry.
@@ -514,7 +515,7 @@ pub trait ChainedError: error::Error + Send + 'static {
514
515
515
516
/// Creates an error from its parts.
516
517
#[ doc( hidden) ]
517
- fn new ( kind : Self :: ErrorKind , state : State ) -> Self where Self : Sized ;
518
+ fn new ( kind : Self :: ErrorKind , state : State < S > ) -> Self where Self : Sized ;
518
519
519
520
/// Returns the first known backtrace, either from its State or from one
520
521
/// of the errors from `foreign_links`.
@@ -526,10 +527,10 @@ pub trait ChainedError: error::Error + Send + 'static {
526
527
527
528
/// A struct which formats an error for output.
528
529
#[ derive( Debug ) ]
529
- pub struct Display < ' a , T : ' a + ?Sized > ( & ' a T ) ;
530
+ pub struct Display < ' a , T : ' a + ?Sized , S : ? Sized > ( & ' a T , PhantomData < S > ) ;
530
531
531
- impl < ' a , T > fmt:: Display for Display < ' a , T >
532
- where T : ChainedError
532
+ impl < ' a , T , S > fmt:: Display for Display < ' a , T , S >
533
+ where T : ChainedError < S >
533
534
{
534
535
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
535
536
try!( writeln ! ( fmt, "Error: {}" , self . 0 ) ) ;
@@ -549,33 +550,35 @@ impl<'a, T> fmt::Display for Display<'a, T>
549
550
/// Common state between errors.
550
551
#[ derive( Debug ) ]
551
552
#[ doc( hidden) ]
552
- pub struct State {
553
+ pub struct State < T : ? Sized > {
553
554
/// Next error in the error chain.
554
- pub next_error : Option < Box < error :: Error + Send > > ,
555
+ pub next_error : Option < Box < T > > ,
555
556
/// Backtrace for the current error.
556
557
#[ cfg( feature = "backtrace" ) ]
557
558
pub backtrace : Option < Arc < Backtrace > > ,
558
559
}
559
560
560
- impl Default for State {
561
+ impl < T > Default for State < T > {
561
562
#[ cfg( feature = "backtrace" ) ]
562
- fn default ( ) -> State {
563
+ fn default ( ) -> Self {
563
564
State {
564
565
next_error : None ,
565
566
backtrace : make_backtrace ( ) ,
566
567
}
567
568
}
568
569
569
570
#[ cfg( not( feature = "backtrace" ) ) ]
570
- fn default ( ) -> State {
571
+ fn default ( ) -> Self {
571
572
State { next_error : None }
572
573
}
573
574
}
574
575
575
- impl State {
576
+ impl < T > State < T >
577
+ where T : error:: Error + Send + ' static
578
+ {
576
579
/// Creates a new State type
577
580
#[ cfg( feature = "backtrace" ) ]
578
- pub fn new < CE : ChainedError > ( e : Box < error :: Error + Send > ) -> State {
581
+ pub fn new < CE : ChainedError < T > > ( e : Box < T > ) -> Self {
579
582
let backtrace = CE :: extract_backtrace ( & * e) . or_else ( make_backtrace) ;
580
583
State {
581
584
next_error : Some ( e) ,
@@ -585,7 +588,7 @@ impl State {
585
588
586
589
/// Creates a new State type
587
590
#[ cfg( not( feature = "backtrace" ) ) ]
588
- pub fn new < CE : ChainedError > ( e : Box < error:: Error + Send > ) -> State {
591
+ pub fn new < CE : ChainedError > ( e : Box < error:: Error + Send > ) -> Self {
589
592
State { next_error : Some ( e) }
590
593
}
591
594
0 commit comments