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

Commit 2d30426

Browse files
committed
Change derive block to take the derive macro name and path to the trait
1 parent 6a44d4b commit 2d30426

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

src/error_chain.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ macro_rules! error_chain_processed {
5454
}
5555

5656
derive {
57-
$($bound:ident),*
57+
$($derive:ident, $bound:path);*
5858
}
5959
) => {
60-
create_super_trait!(Trait: ::std::fmt::Debug, ::std::error::Error, Send $(, $bound)*);
60+
create_super_trait!(Trait: ::std::fmt::Debug, ::std::error::Error, $crate::ToError , Send $(, $bound)*);
6161

6262
/// The Error type.
6363
///
@@ -68,7 +68,7 @@ macro_rules! error_chain_processed {
6868
/// internals, containing:
6969
/// - a backtrace, generated when the error is created.
7070
/// - an error chain, used for the implementation of `Error::cause()`.
71-
#[derive(Debug, $($bound),*)]
71+
#[derive(Debug, $($derive),*)]
7272
pub struct $error_name(
7373
// The members must be `pub` for `links`.
7474
/// The kind of the error.
@@ -78,6 +78,12 @@ macro_rules! error_chain_processed {
7878
pub $crate::State<Trait>,
7979
);
8080

81+
impl $crate::ToError for $error_name {
82+
fn to_error(&self) -> &(::std::error::Error + Send + 'static) {
83+
self
84+
}
85+
}
86+
8187
impl $crate::ChainedError<Trait> for $error_name {
8288
type ErrorKind = $error_kind_name;
8389

@@ -91,7 +97,7 @@ macro_rules! error_chain_processed {
9197

9298
fn with_chain<E, K>(error: E, kind: K)
9399
-> Self
94-
where E: ::std::error::Error + Send + 'static,
100+
where E: $crate::ToError + ::std::error::Error + Send + 'static,
95101
K: Into<Self::ErrorKind>
96102
{
97103
Self::with_chain(error, kind)
@@ -133,12 +139,12 @@ macro_rules! error_chain_processed {
133139
/// Constructs a chained error from another error and a kind, and generates a backtrace.
134140
pub fn with_chain<E, K>(error: E, kind: K)
135141
-> $error_name
136-
where E: ::std::error::Error + Send + 'static,
142+
where E: Trait + 'static,
137143
K: Into<$error_kind_name>
138144
{
139145
$error_name(
140146
kind.into(),
141-
$crate::State::new::<$error_name>(Box::new(error), ),
147+
$crate::State::new::<$error_name>(Box::new(error)),
142148
)
143149
}
144150

@@ -171,7 +177,7 @@ macro_rules! error_chain_processed {
171177

172178
fn cause(&self) -> Option<&::std::error::Error> {
173179
match self.1.next_error {
174-
Some(ref c) => Some(&**c),
180+
Some(ref c) => Some(c.to_error()),
175181
None => {
176182
match self.0 {
177183
$(
@@ -248,7 +254,7 @@ macro_rules! error_chain_processed {
248254

249255
quick_error! {
250256
/// The kind of an error.
251-
#[derive(Debug, $($bound),*)]
257+
#[derive(Debug, $($derive),*)]
252258
pub enum $error_kind_name {
253259

254260
/// A convenient variant for String.
@@ -317,12 +323,14 @@ macro_rules! error_chain_processed {
317323
EK: Into<$error_kind_name>;
318324
}
319325

320-
impl<T, E> $result_ext_name<T, E> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
326+
impl<T, E> $result_ext_name<T, E> for ::std::result::Result<T, E>
327+
where E: Trait + 'static
328+
{
321329
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
322330
where F: FnOnce() -> EK,
323331
EK: Into<$error_kind_name> {
324332
self.map_err(move |e| {
325-
let state = $crate::State::new::<$error_name>(Box::new(e), );
333+
let state = $crate::State::new::<$error_name>(Box::new(e));
326334
$crate::ChainedError::new(callback().into(), state)
327335
})
328336
}

src/example_generated.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
pub mod inner {
2525
error_chain! {
2626
derive {
27-
PartialEq
27+
PartialEq, PartialEq<Error>
2828
}
2929
}
3030
}
@@ -34,18 +34,18 @@ error_chain! {
3434
Inner(inner::Error, inner::ErrorKind) #[doc = "Link to another `ErrorChain`."];
3535
}
3636
foreign_links {
37-
Io(::std::io::Error) #[doc = "Link to a `std::error::Error` type."];
37+
//Io(::std::io::Error) #[doc = "Link to a `std::error::Error` type."];
3838
}
3939
errors {
4040
#[doc = "A custom error kind."]
4141
Custom
4242
}
4343
derive {
44-
PartialEq
44+
PartialEq, PartialEq<Error>
4545
}
4646
}
4747

48-
fn foo<T: PartialEq>() {}
49-
fn bar() {
50-
foo::<Error>();
51-
}
48+
//fn foo<T: PartialEq>() {}
49+
//fn bar() {
50+
//foo::<Error>();
51+
//}

src/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@
359359
//! mod utils {
360360
//! error_chain! {
361361
//! errors {
362-
//! BadStuff {
363362
//! description("bad stuff")
364363
//! }
365364
//! }
@@ -488,7 +487,7 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
488487
/// Constructs a chained error from another error and a kind, and generates a backtrace.
489488
fn with_chain<E, K>(error: E, kind: K) -> Self
490489
where Self: Sized,
491-
E: ::std::error::Error + Send + 'static,
490+
E: ToError + ::std::error::Error + Send + 'static,
492491
K: Into<Self::ErrorKind>;
493492

494493
/// Returns the kind of the error.
@@ -521,8 +520,11 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
521520
/// of the errors from `foreign_links`.
522521
#[cfg(feature = "backtrace")]
523522
#[doc(hidden)]
524-
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<Arc<Backtrace>>
525-
where Self: Sized;
523+
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<Arc<Backtrace>>;
524+
}
525+
526+
pub trait ToError {
527+
fn to_error(&self) -> &(error::Error + Send + 'static);
526528
}
527529

528530
/// A struct which formats an error for output.
@@ -558,7 +560,7 @@ pub struct State<T: ?Sized> {
558560
pub backtrace: Option<Arc<Backtrace>>,
559561
}
560562

561-
impl<T> Default for State<T> {
563+
impl<T: ?Sized> Default for State<T> {
562564
#[cfg(feature = "backtrace")]
563565
fn default() -> Self {
564566
State {
@@ -574,12 +576,12 @@ impl<T> Default for State<T> {
574576
}
575577

576578
impl<T> State<T>
577-
where T: error::Error + Send + 'static
579+
where T: ToError + ?Sized
578580
{
579581
/// Creates a new State type
580582
#[cfg(feature = "backtrace")]
581583
pub fn new<CE: ChainedError<T>>(e: Box<T>) -> Self {
582-
let backtrace = CE::extract_backtrace(&*e).or_else(make_backtrace);
584+
let backtrace = CE::extract_backtrace(e.to_error()).or_else(make_backtrace);
583585
State {
584586
next_error: Some(e),
585587
backtrace: backtrace,

0 commit comments

Comments
 (0)