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

Commit

Permalink
Change derive block to take the derive macro name and path to the t…
Browse files Browse the repository at this point in the history
…rait
  • Loading branch information
pengowen123 committed Aug 21, 2017
1 parent 7f168a2 commit 1a67b33
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
28 changes: 20 additions & 8 deletions src/error_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ macro_rules! error_chain_processed {
}

derive {
$($bound:ident),*
$($derive:ident, $bound:path);*
}
) => {
create_super_trait!(Trait: ::std::fmt::Debug, ::std::error::Error, Send $(, $bound)*);
create_super_trait!(Trait: ::std::fmt::Debug, ::std::error::Error, $crate::ToError , Send $(, $bound)*);

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

impl $crate::ToError for $error_name {
fn to_error(&self) -> &(::std::error::Error + Send + 'static) {
self
}
}

impl $crate::ChainedError<Trait> for $error_name {
type ErrorKind = $error_kind_name;

Expand All @@ -91,7 +97,7 @@ macro_rules! error_chain_processed {

fn with_chain<E, K>(error: E, kind: K)
-> Self
where E: ::std::error::Error + Send + 'static,
where E: $crate::ToError + ::std::error::Error + Send + 'static,
K: Into<Self::ErrorKind>
{
Self::with_chain(error, kind)
Expand Down Expand Up @@ -133,7 +139,7 @@ macro_rules! error_chain_processed {
/// Constructs a chained error from another error and a kind, and generates a backtrace.
pub fn with_chain<E, K>(error: E, kind: K)
-> $error_name
where E: ::std::error::Error + Send + 'static,
where E: Trait + 'static,
K: Into<$error_kind_name>
{
$error_name::with_boxed_chain(Box::new(error), kind)
Expand Down Expand Up @@ -180,7 +186,7 @@ macro_rules! error_chain_processed {
#[allow(unknown_lints, unused_doc_comment)]
fn cause(&self) -> Option<&::std::error::Error> {
match self.1.next_error {
Some(ref c) => Some(&**c),
Some(ref c) => Some(c.to_error()),
None => {
match self.0 {
$(
Expand Down Expand Up @@ -257,7 +263,7 @@ macro_rules! error_chain_processed {

quick_error! {
/// The kind of an error.
#[derive(Debug, $($bound),*)]
#[derive(Debug, $($derive),*)]
pub enum $error_kind_name {

/// A convenient variant for String.
Expand Down Expand Up @@ -326,12 +332,18 @@ macro_rules! error_chain_processed {
EK: Into<$error_kind_name>;
}

<<<<<<< HEAD
impl<T, E> $result_ext_name<T> for ::std::result::Result<T, E> where E: ::std::error::Error + Send + 'static {
=======
impl<T, E> $result_ext_name<T, E> for ::std::result::Result<T, E>
where E: Trait + 'static
{
>>>>>>> Change `derive` block to take the derive macro name and path to the trait
fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, $error_name>
where F: FnOnce() -> EK,
EK: Into<$error_kind_name> {
self.map_err(move |e| {
let state = $crate::State::new::<$error_name>(Box::new(e), );
let state = $crate::State::new::<$error_name>(Box::new(e));
$crate::ChainedError::new(callback().into(), state)
})
}
Expand Down
14 changes: 7 additions & 7 deletions src/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
pub mod inner {
error_chain! {
derive {
PartialEq
PartialEq, PartialEq<Error>
}
}
}
Expand All @@ -34,18 +34,18 @@ error_chain! {
Inner(inner::Error, inner::ErrorKind) #[doc = "Link to another `ErrorChain`."];
}
foreign_links {
Io(::std::io::Error) #[doc = "Link to a `std::error::Error` type."];
//Io(::std::io::Error) #[doc = "Link to a `std::error::Error` type."];
}
errors {
#[doc = "A custom error kind."]
Custom
}
derive {
PartialEq
PartialEq, PartialEq<Error>
}
}

fn foo<T: PartialEq>() {}
fn bar() {
foo::<Error>();
}
//fn foo<T: PartialEq>() {}
//fn bar() {
//foo::<Error>();
//}
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@
//! mod utils {
//! error_chain! {
//! errors {
//! BadStuff {
//! description("bad stuff")
//! }
//! }
Expand Down Expand Up @@ -569,7 +568,7 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
/// Constructs a chained error from another error and a kind, and generates a backtrace.
fn with_chain<E, K>(error: E, kind: K) -> Self
where Self: Sized,
E: ::std::error::Error + Send + 'static,
E: ToError + ::std::error::Error + Send + 'static,
K: Into<Self::ErrorKind>;

/// Returns the kind of the error.
Expand Down Expand Up @@ -602,8 +601,11 @@ pub trait ChainedError<S: ?Sized>: error::Error + Send + 'static {
/// of the errors from `foreign_links`.
#[cfg(feature = "backtrace")]
#[doc(hidden)]
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<Arc<Backtrace>>
where Self: Sized;
fn extract_backtrace(e: &(error::Error + Send + 'static)) -> Option<Arc<Backtrace>>;
}

pub trait ToError {
fn to_error(&self) -> &(error::Error + Send + 'static);
}

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

impl<T> Default for State<T> {
impl<T: ?Sized> Default for State<T> {
#[cfg(feature = "backtrace")]
fn default() -> Self {
State {
Expand All @@ -663,12 +665,12 @@ impl<T> Default for State<T> {
}

impl<T> State<T>
where T: error::Error + Send + 'static
where T: ToError + ?Sized
{
/// Creates a new State type
#[cfg(feature = "backtrace")]
pub fn new<CE: ChainedError<T>>(e: Box<T>) -> Self {
let backtrace = CE::extract_backtrace(&*e).or_else(make_backtrace);
let backtrace = CE::extract_backtrace(e.to_error()).or_else(make_backtrace);
State {
next_error: Some(e),
backtrace: backtrace,
Expand Down

0 comments on commit 1a67b33

Please sign in to comment.