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

Commit 1a67b33

Browse files
committed
Change derive block to take the derive macro name and path to the trait
1 parent 7f168a2 commit 1a67b33

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

src/error_chain.rs

+20-8
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,7 +139,7 @@ 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::with_boxed_chain(Box::new(error), kind)
@@ -180,7 +186,7 @@ macro_rules! error_chain_processed {
180186
#[allow(unknown_lints, unused_doc_comment)]
181187
fn cause(&self) -> Option<&::std::error::Error> {
182188
match self.1.next_error {
183-
Some(ref c) => Some(&**c),
189+
Some(ref c) => Some(c.to_error()),
184190
None => {
185191
match self.0 {
186192
$(
@@ -257,7 +263,7 @@ macro_rules! error_chain_processed {
257263

258264
quick_error! {
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,18 @@ macro_rules! error_chain_processed {
326332
EK: Into<$error_kind_name>;
327333
}
328334

335+
<<<<<<< HEAD
329336
impl<T, E> $result_ext_name<T> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
337+
=======
338+
impl<T, E> $result_ext_name<T, E> for ::std::result::Result<T, E>
339+
where E: Trait + 'static
340+
{
341+
>>>>>>> Change `derive` block to take the derive macro name and path to the trait
330342
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
331343
where F: FnOnce() -> EK,
332344
EK: Into<$error_kind_name> {
333345
self.map_err(move |e| {
334-
let state = $crate::State::new::<$error_name>(Box::new(e), );
346+
let state = $crate::State::new::<$error_name>(Box::new(e));
335347
$crate::ChainedError::new(callback().into(), state)
336348
})
337349
}

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-7
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
//! }
@@ -569,7 +568,7 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
569568
/// Constructs a chained error from another error and a kind, and generates a backtrace.
570569
fn with_chain<E, K>(error: E, kind: K) -> Self
571570
where Self: Sized,
572-
E: ::std::error::Error + Send + 'static,
571+
E: ToError + ::std::error::Error + Send + 'static,
573572
K: Into<Self::ErrorKind>;
574573

575574
/// Returns the kind of the error.
@@ -602,8 +601,11 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
602601
/// of the errors from `foreign_links`.
603602
#[cfg(feature = "backtrace")]
604603
#[doc(hidden)]
605-
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<Arc<Backtrace>>
606-
where Self: Sized;
604+
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<Arc<Backtrace>>;
605+
}
606+
607+
pub trait ToError {
608+
fn to_error(&self) -> &(error::Error + Send + 'static);
607609
}
608610

609611
/// A struct which formats an error for output.
@@ -647,7 +649,7 @@ pub struct State<T: ?Sized> {
647649
pub backtrace: Option<Arc<Backtrace>>,
648650
}
649651

650-
impl<T> Default for State<T> {
652+
impl<T: ?Sized> Default for State<T> {
651653
#[cfg(feature = "backtrace")]
652654
fn default() -> Self {
653655
State {
@@ -663,12 +665,12 @@ impl<T> Default for State<T> {
663665
}
664666

665667
impl<T> State<T>
666-
where T: error::Error + Send + 'static
668+
where T: ToError + ?Sized
667669
{
668670
/// Creates a new State type
669671
#[cfg(feature = "backtrace")]
670672
pub fn new<CE: ChainedError<T>>(e: Box<T>) -> Self {
671-
let backtrace = CE::extract_backtrace(&*e).or_else(make_backtrace);
673+
let backtrace = CE::extract_backtrace(e.to_error()).or_else(make_backtrace);
672674
State {
673675
next_error: Some(e),
674676
backtrace: backtrace,

0 commit comments

Comments
 (0)