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

Commit 225f592

Browse files
committed
Add generated trait
1 parent c3bf41d commit 225f592

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/error_chain.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ macro_rules! error_chain_processed {
4040
}
4141

4242
derive {
43-
$($trait:ident),*
43+
$($bound:ident),*
4444
}
4545

4646
links {
@@ -58,6 +58,10 @@ macro_rules! error_chain_processed {
5858
}
5959

6060
) => {
61+
use ::std::fmt::Debug;
62+
use ::std::error::Error as StdError;
63+
create_super_trait!(Trait: Debug, StdError, Send $(, $bound)*);
64+
6165
/// The Error type.
6266
///
6367
/// This tuple struct is made of two elements:
@@ -67,7 +71,7 @@ macro_rules! error_chain_processed {
6771
/// internals, containing:
6872
/// - a backtrace, generated when the error is created.
6973
/// - an error chain, used for the implementation of `Error::cause()`.
70-
#[derive(Debug)]
74+
#[derive(Debug, $($bound),*)]
7175
pub struct $error_name(
7276
// The members must be `pub` for `links`.
7377
/// The kind of the error.
@@ -256,7 +260,7 @@ macro_rules! error_chain_processed {
256260

257261
quick_error! {
258262
/// The kind of an error.
259-
#[derive(Debug)]
263+
#[derive(Debug, $($bound),*)]
260264
pub enum $error_kind_name {
261265

262266
/// A convenient variant for String.
@@ -427,6 +431,19 @@ macro_rules! error_chain {
427431
};
428432
}
429433

434+
/// Macro used to generate traits with `Self` bounds
435+
#[macro_export]
436+
#[doc(hidden)]
437+
macro_rules! create_super_trait {
438+
($name:ident: $($bound:ident),*) => {
439+
create_super_trait!($name: $($bound +)*);
440+
};
441+
($name:ident: $bound_1:ident + $($bound_2:tt +)*) => {
442+
trait $name: $bound_1 $(+ $bound_2)* {}
443+
impl<T: $bound_1 $(+ $bound_2)*> $name for T {}
444+
};
445+
}
446+
430447
/// Macro used to manage the `backtrace` feature.
431448
///
432449
/// See

src/example_generated.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
pub mod inner {
2424
error_chain! {
2525
derive {
26-
Send
26+
PartialEq, Eq
2727
}
2828
}
2929
}
@@ -40,11 +40,11 @@ error_chain! {
4040
Custom
4141
}
4242
derive {
43-
Send
43+
PartialEq, Eq
4444
}
4545
}
4646

47-
fn foo<T: Send>() {}
47+
fn foo<T: PartialEq + Eq>() {}
4848
fn bar() {
4949
foo::<Error>();
5050
}

0 commit comments

Comments
 (0)