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

Commit 1455999

Browse files
committed
Use State instead of big tuple.
1 parent fe5db6e commit 1455999

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

CHANGELOG.md

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

1011
# 0.5.0
1112

src/lib.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ macro_rules! error_chain {
359359
/// that is mostly irrelevant for error handling purposes.
360360
#[derive(Debug)]
361361
pub struct $error_name(pub $error_kind_name,
362-
pub (Option<Box<::std::error::Error + Send>>,
363-
Option<::std::sync::Arc<$crate::Backtrace>>));
362+
pub $crate::State);
364363

365364
impl $error_name {
366365
/// Returns the kind of the error.
@@ -375,14 +374,14 @@ macro_rules! error_chain {
375374

376375
/// Returns the backtrace associated with this error.
377376
pub fn backtrace(&self) -> Option<&$crate::Backtrace> {
378-
(self.1).1.as_ref().map(|v| &**v)
377+
self.1.backtrace.as_ref().map(|v| &**v)
379378
}
380379
}
381380

382381
impl ::std::error::Error for $error_name {
383382
fn description(&self) -> &str { self.0.description() }
384383
fn cause(&self) -> Option<&::std::error::Error> {
385-
match (self.1).0 {
384+
match self.1.next_error {
386385
Some(ref c) => Some(&**c),
387386
None => {
388387
match self.0 {
@@ -420,29 +419,26 @@ macro_rules! error_chain {
420419
fn from(e: $foreign_link_error_path) -> Self {
421420
$error_name(
422421
$error_kind_name::$foreign_link_variant(e),
423-
(None, $crate::make_backtrace()))
422+
$crate::State::default())
424423
}
425424
}
426425
) *
427426

428427
impl From<$error_kind_name> for $error_name {
429428
fn from(e: $error_kind_name) -> Self {
430-
$error_name(e,
431-
(None, $crate::make_backtrace()))
429+
$error_name(e, $crate::State::default())
432430
}
433431
}
434432

435433
impl<'a> From<&'a str> for $error_name {
436434
fn from(s: &'a str) -> Self {
437-
$error_name(s.into(),
438-
(None, $crate::make_backtrace()))
435+
$error_name(s.into(), $crate::State::default())
439436
}
440437
}
441438

442439
impl From<String> for $error_name {
443440
fn from(s: String) -> Self {
444-
$error_name(s.into(),
445-
(None, $crate::make_backtrace()))
441+
$error_name(s.into(), $crate::State::default())
446442
}
447443
}
448444

@@ -518,19 +514,18 @@ macro_rules! error_chain {
518514
impl $crate::ChainedError for $error_name {
519515
type ErrorKind = $error_kind_name;
520516

521-
fn new(kind: $error_kind_name, backtrace: (Option<Box<::std::error::Error + Send + 'static>>,
522-
Option<::std::sync::Arc<$crate::Backtrace>>)) -> $error_name {
523-
$error_name(kind, backtrace)
517+
fn new(kind: $error_kind_name, state: $crate::State) -> $error_name {
518+
$error_name(kind, state)
524519
}
525520

526521
fn extract_backtrace(e: &(::std::error::Error + Send + 'static))
527522
-> Option<Option<::std::sync::Arc<$crate::Backtrace>>> {
528523
if let Some(e) = e.downcast_ref::<$error_name>() {
529-
Some((e.1).1.clone())
524+
Some(e.1.backtrace.clone())
530525
}
531526
$(
532527
else if let Some(e) = e.downcast_ref::<$link_error_path>() {
533-
Some((e.1).1.clone())
528+
Some(e.1.backtrace.clone())
534529
}
535530
) *
536531
else {
@@ -695,9 +690,7 @@ pub trait ChainedError: error::Error + Send + 'static {
695690
/// Associated kind type.
696691
type ErrorKind;
697692
/// Creates an error from it's parts.
698-
fn new(kind: Self::ErrorKind,
699-
state: (Option<Box<error::Error + Send + 'static>>,
700-
Option<Arc<Backtrace>>)) -> Self;
693+
fn new(kind: Self::ErrorKind, state: State) -> Self;
701694
/// Use downcasts to extract the backtrace from types we know,
702695
/// to avoid generating a new one. It would be better to not
703696
/// define this in the macro, but types need some additional
@@ -725,7 +718,28 @@ impl<T, E> ResultExt<T, E> for Result<T, E> where E: ChainedError {
725718
let backtrace =
726719
E::extract_backtrace(&e).unwrap_or_else(make_backtrace);
727720

728-
E::new(callback().into(), (Some(Box::new(e)), backtrace))
721+
E::new(callback().into(), State {
722+
next_error: Some(Box::new(e)),
723+
backtrace: backtrace,
724+
})
729725
})
730726
}
731727
}
728+
729+
/// Common state between errors.
730+
#[derive(Debug)]
731+
pub struct State {
732+
/// Next error in the error chain.
733+
pub next_error: Option<Box<error::Error + Send>>,
734+
/// Backtrace for the current error.
735+
pub backtrace: Option<Arc<Backtrace>>,
736+
}
737+
738+
impl Default for State {
739+
fn default() -> State {
740+
State {
741+
next_error: None,
742+
backtrace: make_backtrace(),
743+
}
744+
}
745+
}

0 commit comments

Comments
 (0)