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

Commit 012c621

Browse files
committed
Add generated trait
1 parent 3c5bfed commit 012c621

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/error_chain.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! impl_error_chain_processed {
4242
}
4343

4444
derive {
45-
$($trait:ident),*
45+
$($bound:ident),*
4646
}
4747

4848
links {
@@ -60,6 +60,10 @@ macro_rules! impl_error_chain_processed {
6060
}
6161

6262
) => {
63+
use ::std::fmt::Debug;
64+
use ::std::error::Error as StdError;
65+
create_super_trait!(Trait: Debug, StdError, Send $(, $bound)*);
66+
6367
/// The Error type.
6468
///
6569
/// This tuple struct is made of two elements:
@@ -69,7 +73,7 @@ macro_rules! impl_error_chain_processed {
6973
/// internals, containing:
7074
/// - a backtrace, generated when the error is created.
7175
/// - an error chain, used for the implementation of `Error::cause()`.
72-
#[derive(Debug)]
76+
#[derive(Debug, $($bound),*)]
7377
pub struct $error_name(
7478
// The members must be `pub` for `links`.
7579
/// The kind of the error.
@@ -256,7 +260,7 @@ macro_rules! impl_error_chain_processed {
256260

257261
impl_error_chain_kind! {
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.
@@ -426,6 +430,19 @@ macro_rules! error_chain {
426430
};
427431
}
428432

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

src/example_generated.rs

Lines changed: 3 additions & 3 deletions
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)