Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit 8868c26

Browse files
committed
Error is now a struct.
1 parent abc3d1a commit 8868c26

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Remove need to specify `ErrorKind` in `links {}`.
88
- Add ResultExt trait.
99
- Error.1 is a struct instead of a tuple.
10+
- Error is now a struct.
1011

1112
# 0.5.0
1213

src/lib.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,25 @@ macro_rules! error_chain {
356356
/// during error handling. The second field is internal state
357357
/// that is mostly irrelevant for error handling purposes.
358358
#[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+
}
361363

362364
impl_error!($error_name $error_kind_name $($link_error_path)*);
363365

364366
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+
365375
/// Returns the kind of the error.
366376
pub fn kind(&self) -> &$error_kind_name {
367-
&self.0
377+
&self.kind
368378
}
369379

370380
/// Iterates over the error chain.
@@ -374,12 +384,15 @@ macro_rules! error_chain {
374384
}
375385

376386
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+
378391
fn cause(&self) -> Option<&::std::error::Error> {
379-
match self.1.next_error {
392+
match self.state.next_error {
380393
Some(ref c) => Some(&**c),
381394
None => {
382-
match self.0 {
395+
match self.kind {
383396
$(
384397
$(#[$meta_foreign_links])*
385398
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
@@ -395,15 +408,18 @@ macro_rules! error_chain {
395408

396409
impl ::std::fmt::Display for $error_name {
397410
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)
399412
}
400413
}
401414

402415
$(
403416
$(#[$meta_links])*
404417
impl From<$link_error_path> for $error_name {
405418
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+
}
407423
}
408424
}
409425
) *
@@ -412,36 +428,36 @@ macro_rules! error_chain {
412428
$(#[$meta_foreign_links])*
413429
impl From<$foreign_link_error_path> for $error_name {
414430
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+
)
418434
}
419435
}
420436
) *
421437

422438
impl From<$error_kind_name> for $error_name {
423439
fn from(e: $error_kind_name) -> Self {
424-
$error_name(e, $crate::State::default())
440+
$error_name::from_kind(e)
425441
}
426442
}
427443

428444
impl<'a> From<&'a str> for $error_name {
429445
fn from(s: &'a str) -> Self {
430-
$error_name(s.into(), $crate::State::default())
446+
$error_name::from_kind(s.into())
431447
}
432448
}
433449

434450
impl From<String> for $error_name {
435451
fn from(s: String) -> Self {
436-
$error_name(s.into(), $crate::State::default())
452+
$error_name::from_kind(s.into())
437453
}
438454
}
439455

440456
impl ::std::ops::Deref for $error_name {
441457
type Target = $error_kind_name;
442458

443459
fn deref(&self) -> &Self::Target {
444-
&self.0
460+
&self.kind
445461
}
446462
}
447463

@@ -502,7 +518,7 @@ macro_rules! error_chain {
502518

503519
impl From<$error_name> for $error_kind_name {
504520
fn from(e: $error_name) -> Self {
505-
e.0
521+
e.kind
506522
}
507523
}
508524

@@ -636,25 +652,28 @@ macro_rules! impl_error {
636652
impl $error_name {
637653
/// Returns the backtrace associated with this error.
638654
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)
640656
}
641657
}
642658

643659
impl $crate::ChainedError for $error_name {
644660
type ErrorKind = $error_kind_name;
645661

646662
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+
}
648667
}
649668

650669
fn extract_backtrace(e: &(::std::error::Error + Send + 'static))
651670
-> Option<Option<::std::sync::Arc<$crate::Backtrace>>> {
652671
if let Some(e) = e.downcast_ref::<$error_name>() {
653-
Some(e.1.backtrace.clone())
672+
Some(e.state.backtrace.clone())
654673
}
655674
$(
656675
else if let Some(e) = e.downcast_ref::<$link_error_path>() {
657-
Some(e.1.backtrace.clone())
676+
Some(e.state.backtrace.clone())
658677
}
659678
) *
660679
else {
@@ -681,7 +700,10 @@ macro_rules! impl_error {
681700
type ErrorKind = $error_kind_name;
682701

683702
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+
}
685707
}
686708
}
687709
}

0 commit comments

Comments
 (0)