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

Commit ad0437c

Browse files
committed
Change derive block to take the derive macro name and path to the trait
1 parent 24c9538 commit ad0437c

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

src/error_chain.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ macro_rules! impl_error_chain_processed {
5656
}
5757

5858
derive {
59-
$($bound:ident),*
59+
$($derive:ident, $bound:path);*
6060
}
6161
) => {
62-
create_super_trait!(Trait: ::std::fmt::Debug, ::std::error::Error, Send $(, $bound)*);
62+
create_super_trait!(Trait: ::std::fmt::Debug, ::std::error::Error, $crate::ToError , Send $(, $bound)*);
6363

6464
/// The Error type.
6565
///
@@ -70,7 +70,7 @@ macro_rules! impl_error_chain_processed {
7070
/// internals, containing:
7171
/// - a backtrace, generated when the error is created.
7272
/// - an error chain, used for the implementation of `Error::cause()`.
73-
#[derive(Debug, $($bound),*)]
73+
#[derive(Debug, $($derive),*)]
7474
pub struct $error_name(
7575
// The members must be `pub` for `links`.
7676
/// The kind of the error.
@@ -80,6 +80,12 @@ macro_rules! impl_error_chain_processed {
8080
pub $crate::State<Trait>,
8181
);
8282

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

@@ -93,7 +99,7 @@ macro_rules! impl_error_chain_processed {
9399

94100
fn with_chain<E, K>(error: E, kind: K)
95101
-> Self
96-
where E: ::std::error::Error + Send + 'static,
102+
where E: $crate::ToError + ::std::error::Error + Send + 'static,
97103
K: Into<Self::ErrorKind>
98104
{
99105
Self::with_chain(error, kind)
@@ -135,7 +141,7 @@ macro_rules! impl_error_chain_processed {
135141
/// Constructs a chained error from another error and a kind, and generates a backtrace.
136142
pub fn with_chain<E, K>(error: E, kind: K)
137143
-> $error_name
138-
where E: ::std::error::Error + Send + 'static,
144+
where E: Trait + 'static,
139145
K: Into<$error_kind_name>
140146
{
141147
$error_name::with_boxed_chain(Box::new(error), kind)
@@ -188,7 +194,7 @@ macro_rules! impl_error_chain_processed {
188194
#[allow(unknown_lints, unused_doc_comment)]
189195
fn cause(&self) -> Option<&::std::error::Error> {
190196
match self.1.next_error {
191-
Some(ref c) => Some(&**c),
197+
Some(ref c) => Some(c.to_error()),
192198
None => {
193199
match self.0 {
194200
$(
@@ -257,7 +263,7 @@ macro_rules! impl_error_chain_processed {
257263

258264
impl_error_chain_kind! {
259265
/// The kind of an error.
260-
#[derive(Debug, $($bound),*)]
266+
#[derive(Debug, $($derive),*)]
261267
pub enum $error_kind_name {
262268

263269
/// A convenient variant for String.
@@ -326,12 +332,14 @@ macro_rules! impl_error_chain_processed {
326332
EK: Into<$error_kind_name>;
327333
}
328334

329-
impl<T, E> $result_ext_name<T> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
335+
impl<T, E> $result_ext_name<T, E> for ::std::result::Result<T, E>
336+
where E: Trait + 'static
337+
{
330338
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
331339
where F: FnOnce() -> EK,
332340
EK: Into<$error_kind_name> {
333341
self.map_err(move |e| {
334-
let state = $crate::State::new::<$error_name>(Box::new(e), );
342+
let state = $crate::State::new::<$error_name>(Box::new(e));
335343
$crate::ChainedError::new(callback().into(), state)
336344
})
337345
}

src/example_generated.rs

+7-7
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

+9-9
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@
360360
//! mod utils {
361361
//! error_chain! {
362362
//! errors {
363-
//! BadStuff {
364363
//! description("bad stuff")
365364
//! }
366365
//! }
@@ -593,10 +592,9 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
593592

594593
/// Constructs a chained error from another error and a kind, and generates a backtrace.
595594
fn with_chain<E, K>(error: E, kind: K) -> Self
596-
where
597-
Self: Sized,
598-
E: ::std::error::Error + Send + 'static,
599-
K: Into<Self::ErrorKind>;
595+
where Self: Sized,
596+
E: ToError + ::std::error::Error + Send + 'static,
597+
K: Into<Self::ErrorKind>;
600598

601599
/// Returns the kind of the error.
602600
fn kind(&self) -> &Self::ErrorKind;
@@ -630,9 +628,11 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
630628
/// Returns the first known backtrace, either from its State or from one
631629
/// of the errors from `foreign_links`.
632630
#[doc(hidden)]
633-
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<InternalBacktrace>
634-
where
635-
Self: Sized;
631+
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<InternalBacktrace>;
632+
}
633+
634+
pub trait ToError {
635+
fn to_error(&self) -> &(error::Error + Send + 'static);
636636
}
637637

638638
/// A struct which formats an error for output.
@@ -669,7 +669,7 @@ pub struct State<T: ?Sized> {
669669
pub backtrace: InternalBacktrace,
670670
}
671671

672-
impl<T> Default for State<T> {
672+
impl<T: ?Sized> Default for State<T> {
673673
#[cfg(feature = "backtrace")]
674674
fn default() -> Self {
675675
State {

0 commit comments

Comments
 (0)