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

Commit bf120fa

Browse files
committed
Improve ChainedError implementation.
1 parent 238ac30 commit bf120fa

File tree

1 file changed

+29
-40
lines changed

1 file changed

+29
-40
lines changed

src/error_chain.rs

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,20 @@ macro_rules! error_chain_processed {
6565
pub state: $crate::State,
6666
}
6767

68-
impl_error!($error_name
69-
$error_kind_name
70-
$([$link_error_path, $(#[$meta_links])*])*);
68+
impl $crate::ChainedError for $error_name {
69+
type ErrorKind = $error_kind_name;
70+
71+
fn new(kind: $error_kind_name, state: $crate::State) -> $error_name {
72+
$error_name {
73+
kind: kind,
74+
state: state,
75+
}
76+
}
77+
78+
impl_extract_backtrace!($error_name
79+
$error_kind_name
80+
$([$link_error_path, $(#[$meta_links])*])*);
81+
}
7182

7283
#[allow(dead_code)]
7384
impl $error_name {
@@ -311,35 +322,24 @@ macro_rules! error_chain {
311322
#[macro_export]
312323
#[doc(hidden)]
313324
#[cfg(feature = "backtrace")]
314-
macro_rules! impl_error {
325+
macro_rules! impl_extract_backtrace {
315326
($error_name: ident
316327
$error_kind_name: ident
317328
$([$link_error_path: path, $(#[$meta_links: meta])*])*) => {
318-
impl $crate::ChainedError for $error_name {
319-
type ErrorKind = $error_kind_name;
320-
321-
fn new(kind: $error_kind_name, state: $crate::State) -> $error_name {
322-
$error_name {
323-
kind: kind,
324-
state: state,
325-
}
329+
fn extract_backtrace(e: &(::std::error::Error + Send + 'static))
330+
-> Option<Option<::std::sync::Arc<$crate::Backtrace>>> {
331+
if let Some(e) = e.downcast_ref::<$error_name>() {
332+
return Some(e.state.backtrace.clone());
326333
}
327-
328-
fn extract_backtrace(e: &(::std::error::Error + Send + 'static))
329-
-> Option<Option<::std::sync::Arc<$crate::Backtrace>>> {
330-
if let Some(e) = e.downcast_ref::<$error_name>() {
331-
return Some(e.state.backtrace.clone());
332-
}
333-
$(
334-
$( #[$meta_links] )*
335-
{
336-
if let Some(e) = e.downcast_ref::<$link_error_path>() {
337-
return Some(e.state.backtrace.clone());
338-
}
334+
$(
335+
$( #[$meta_links] )*
336+
{
337+
if let Some(e) = e.downcast_ref::<$link_error_path>() {
338+
return Some(e.state.backtrace.clone());
339339
}
340-
) *
341-
None
342-
}
340+
}
341+
) *
342+
None
343343
}
344344
}
345345
}
@@ -352,19 +352,8 @@ macro_rules! impl_error {
352352
#[macro_export]
353353
#[doc(hidden)]
354354
#[cfg(not(feature = "backtrace"))]
355-
macro_rules! impl_error {
355+
macro_rules! impl_extract_backtrace {
356356
($error_name: ident
357357
$error_kind_name: ident
358-
$([$link_error_path: path, $(#[$meta_links: meta])*])*) => {
359-
impl $crate::ChainedError for $error_name {
360-
type ErrorKind = $error_kind_name;
361-
362-
fn new(kind: $error_kind_name, state: $crate::State) -> $error_name {
363-
$error_name {
364-
kind: kind,
365-
state: state,
366-
}
367-
}
368-
}
369-
}
358+
$([$link_error_path: path, $(#[$meta_links: meta])*])*) => {}
370359
}

0 commit comments

Comments
 (0)