@@ -356,15 +356,25 @@ macro_rules! error_chain {
356
356
/// during error handling. The second field is internal state
357
357
/// that is mostly irrelevant for error handling purposes.
358
358
#[ derive( Debug ) ]
359
- pub struct $error_name( pub $error_kind_name,
360
- pub $crate:: State ) ;
359
+ pub struct $error_name {
360
+ pub kind: $error_kind_name,
361
+ pub state: $crate:: State ,
362
+ }
361
363
362
364
impl_error!( $error_name $error_kind_name $( $link_error_path) * ) ;
363
365
364
366
impl $error_name {
367
+ /// Constructs an error from a kind.
368
+ pub fn from_kind( kind: $error_kind_name) -> $error_name {
369
+ $error_name {
370
+ kind: kind,
371
+ state: $crate:: State :: default ( ) ,
372
+ }
373
+ }
374
+
365
375
/// Returns the kind of the error.
366
376
pub fn kind( & self ) -> & $error_kind_name {
367
- & self . 0
377
+ & self . kind
368
378
}
369
379
370
380
/// Iterates over the error chain.
@@ -374,12 +384,15 @@ macro_rules! error_chain {
374
384
}
375
385
376
386
impl :: std:: error:: Error for $error_name {
377
- fn description( & self ) -> & str { self . 0 . description( ) }
387
+ fn description( & self ) -> & str {
388
+ self . kind. description( )
389
+ }
390
+
378
391
fn cause( & self ) -> Option <& :: std:: error:: Error > {
379
- match self . 1 . next_error {
392
+ match self . state . next_error {
380
393
Some ( ref c) => Some ( & * * c) ,
381
394
None => {
382
- match self . 0 {
395
+ match self . kind {
383
396
$(
384
397
$( #[ $meta_foreign_links] ) *
385
398
$error_kind_name:: $foreign_link_variant( ref foreign_err) => {
@@ -395,15 +408,18 @@ macro_rules! error_chain {
395
408
396
409
impl :: std:: fmt:: Display for $error_name {
397
410
fn fmt( & self , f: & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
398
- :: std:: fmt:: Display :: fmt( & self . 0 , f)
411
+ :: std:: fmt:: Display :: fmt( & self . kind , f)
399
412
}
400
413
}
401
414
402
415
$(
403
416
$( #[ $meta_links] ) *
404
417
impl From <$link_error_path> for $error_name {
405
418
fn from( e: $link_error_path) -> Self {
406
- $error_name( $error_kind_name:: $link_variant( e. 0 ) , e. 1 )
419
+ $error_name {
420
+ kind: $error_kind_name:: $link_variant( e. kind) ,
421
+ state: e. state,
422
+ }
407
423
}
408
424
}
409
425
) *
@@ -412,36 +428,36 @@ macro_rules! error_chain {
412
428
$( #[ $meta_foreign_links] ) *
413
429
impl From <$foreign_link_error_path> for $error_name {
414
430
fn from( e: $foreign_link_error_path) -> Self {
415
- $error_name(
416
- $error_kind_name:: $foreign_link_variant( e) ,
417
- $crate :: State :: default ( ) )
431
+ $error_name:: from_kind (
432
+ $error_kind_name:: $foreign_link_variant( e)
433
+ )
418
434
}
419
435
}
420
436
) *
421
437
422
438
impl From <$error_kind_name> for $error_name {
423
439
fn from( e: $error_kind_name) -> Self {
424
- $error_name( e , $crate :: State :: default ( ) )
440
+ $error_name:: from_kind ( e )
425
441
}
426
442
}
427
443
428
444
impl <' a> From <& ' a str > for $error_name {
429
445
fn from( s: & ' a str ) -> Self {
430
- $error_name( s. into( ) , $crate :: State :: default ( ) )
446
+ $error_name:: from_kind ( s. into( ) )
431
447
}
432
448
}
433
449
434
450
impl From <String > for $error_name {
435
451
fn from( s: String ) -> Self {
436
- $error_name( s. into( ) , $crate :: State :: default ( ) )
452
+ $error_name:: from_kind ( s. into( ) )
437
453
}
438
454
}
439
455
440
456
impl :: std:: ops:: Deref for $error_name {
441
457
type Target = $error_kind_name;
442
458
443
459
fn deref( & self ) -> & Self :: Target {
444
- & self . 0
460
+ & self . kind
445
461
}
446
462
}
447
463
@@ -502,7 +518,7 @@ macro_rules! error_chain {
502
518
503
519
impl From <$error_name> for $error_kind_name {
504
520
fn from( e: $error_name) -> Self {
505
- e. 0
521
+ e. kind
506
522
}
507
523
}
508
524
@@ -636,25 +652,28 @@ macro_rules! impl_error {
636
652
impl $error_name {
637
653
/// Returns the backtrace associated with this error.
638
654
pub fn backtrace( & self ) -> Option <& $crate:: Backtrace > {
639
- self . 1 . backtrace. as_ref( ) . map( |v| & * * v)
655
+ self . state . backtrace. as_ref( ) . map( |v| & * * v)
640
656
}
641
657
}
642
658
643
659
impl $crate:: ChainedError for $error_name {
644
660
type ErrorKind = $error_kind_name;
645
661
646
662
fn new( kind: $error_kind_name, state: $crate:: State ) -> $error_name {
647
- $error_name( kind, state)
663
+ $error_name {
664
+ kind: kind,
665
+ state: state,
666
+ }
648
667
}
649
668
650
669
fn extract_backtrace( e: & ( :: std:: error:: Error + Send + ' static ) )
651
670
-> Option <Option <:: std:: sync:: Arc <$crate:: Backtrace >>> {
652
671
if let Some ( e) = e. downcast_ref:: <$error_name>( ) {
653
- Some ( e. 1 . backtrace. clone( ) )
672
+ Some ( e. state . backtrace. clone( ) )
654
673
}
655
674
$(
656
675
else if let Some ( e) = e. downcast_ref:: <$link_error_path>( ) {
657
- Some ( e. 1 . backtrace. clone( ) )
676
+ Some ( e. state . backtrace. clone( ) )
658
677
}
659
678
) *
660
679
else {
@@ -681,7 +700,10 @@ macro_rules! impl_error {
681
700
type ErrorKind = $error_kind_name;
682
701
683
702
fn new( kind: $error_kind_name, state: $crate:: State ) -> $error_name {
684
- $error_name( kind, state)
703
+ $error_name {
704
+ kind: kind,
705
+ state: state,
706
+ }
685
707
}
686
708
}
687
709
}
0 commit comments