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

Commit 95d537d

Browse files
committed
Simplify backtrace_from_box
1 parent 5aeefd7 commit 95d537d

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

src/lib.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -509,32 +509,19 @@ macro_rules! error_chain {
509509
$error_name(kind, backtrace)
510510
}
511511

512-
fn backtrace_from_box(mut e: Box<::std::error::Error + Send + 'static>)
513-
-> (Box<::std::error::Error + Send + 'static>,
514-
Option<Option<::std::sync::Arc<$crate::Backtrace>>>) {
515-
let mut backtrace = None;
516-
517-
e = match e.downcast::<$error_name>() {
518-
Err(e) => e,
519-
Ok(e) => {
520-
backtrace = Some((e.1).1.clone());
521-
e as Box<::std::error::Error + Send + 'static>
522-
}
523-
};
524-
512+
fn backtrace_from_box(e: &(::std::error::Error + Send + 'static))
513+
-> Option<Option<::std::sync::Arc<$crate::Backtrace>>> {
514+
if let Some(e) = e.downcast_ref::<$error_name>() {
515+
Some((e.1).1.clone())
516+
}
525517
$(
526-
527-
e = match e.downcast::<$link_error_path>() {
528-
Err(e) => e,
529-
Ok(e) => {
530-
backtrace = Some((e.1).1.clone());
531-
e as Box<::std::error::Error + Send + 'static>
532-
}
533-
};
534-
518+
else if let Some(e) = e.downcast_ref::<$link_error_path>() {
519+
Some((e.1).1.clone())
520+
}
535521
) *
536-
537-
(e, backtrace)
522+
else {
523+
None
524+
}
538525
}
539526
}
540527

@@ -700,8 +687,8 @@ pub trait Error: error::Error + Send + 'static {
700687
/// to avoid generating a new one. It would be better to not
701688
/// define this in the macro, but types need some additional
702689
/// machinery to make it work.
703-
fn backtrace_from_box(e: Box<error::Error + Send + 'static>)
704-
-> (Box<error::Error + Send + 'static>, Option<Option<Arc<Backtrace>>>);
690+
fn backtrace_from_box(e: &(error::Error + Send + 'static))
691+
-> Option<Option<Arc<Backtrace>>>;
705692
}
706693

707694
/// Additionnal methods for `Result`, for easy interaction with this crate.
@@ -720,11 +707,10 @@ impl<T, E> ResultExt<T, E> for Result<T, E> where E: Error {
720707
where F: FnOnce() -> EK,
721708
EK: Into<E::ErrorKind> {
722709
self.map_err(move |e| {
723-
let e = Box::new(e) as Box<error::Error + Send + 'static>;
724-
let (e, backtrace) = E::backtrace_from_box(e);
725-
let backtrace = backtrace.unwrap_or_else(make_backtrace);
710+
let backtrace =
711+
E::backtrace_from_box(&e).unwrap_or_else(make_backtrace);
726712

727-
E::new(callback().into(), (Some(e), backtrace))
713+
E::new(callback().into(), (Some(Box::new(e)), backtrace))
728714
})
729715
}
730716
}

0 commit comments

Comments
 (0)