diff --git a/examples/size.rs b/examples/size.rs deleted file mode 100644 index ae360d66e..000000000 --- a/examples/size.rs +++ /dev/null @@ -1,38 +0,0 @@ -#[macro_use] -extern crate error_chain; - -use std::mem::{size_of, size_of_val}; - -error_chain! { - errors { - AVariant - Another - } -} - -fn main() { - println!("Memory usage in bytes"); - println!("---------------------"); - println!("Result<()>: {}", size_of::>()); - println!(" (): {}", size_of::<()>()); - println!(" Error: {}", size_of::()); - println!(" ErrorKind: {}", size_of::()); - let msg = ErrorKind::Msg("test".into()); - println!(" ErrorKind::Msg: {}", size_of_val(&msg)); - println!(" String: {}", size_of::()); - println!(" State: {}", size_of::()); - #[cfg(feature = "backtrace")] - { - let state = error_chain::State { - next_error: None, - backtrace: None, - }; - println!(" State.next_error: {}", size_of_val(&state.next_error)); - println!(" State.backtrace: {}", size_of_val(&state.backtrace)); - } - #[cfg(not(feature = "backtrace"))] - { - let state = error_chain::State { next_error: None }; - println!(" State.next_error: {}", size_of_val(&state.next_error)); - } -} diff --git a/src/error_chain.rs b/src/error_chain.rs index cbd42cd67..eaa151c7c 100644 --- a/src/error_chain.rs +++ b/src/error_chain.rs @@ -65,20 +65,13 @@ macro_rules! impl_error_chain_processed { /// - a backtrace, generated when the error is created. /// - an error chain, used for the implementation of `Error::cause()`. #[derive(Debug)] - pub struct $error_name( - // The members must be `pub` for `links`. - /// The kind of the error. - pub $error_kind_name, - /// Contains the error chain and the backtrace. - #[doc(hidden)] - pub $crate::State, - ); + pub struct $error_name(pub Box<($error_kind_name, $crate::State)>); impl $crate::ChainedError for $error_name { type ErrorKind = $error_kind_name; fn new(kind: $error_kind_name, state: $crate::State) -> $error_name { - $error_name(kind, state) + $error_name(Box::new((kind, state))) } fn from_kind(kind: Self::ErrorKind) -> Self { @@ -120,10 +113,10 @@ macro_rules! impl_error_chain_processed { impl $error_name { /// Constructs an error from a kind, and generates a backtrace. pub fn from_kind(kind: $error_kind_name) -> $error_name { - $error_name( + $error_name(Box::new(( kind, $crate::State::default(), - ) + ))) } /// Constructs a chained error from another error and a kind, and generates a backtrace. @@ -140,15 +133,15 @@ macro_rules! impl_error_chain_processed { -> $error_name where K: Into<$error_kind_name> { - $error_name( + $error_name(Box::new(( kind.into(), $crate::State::new::<$error_name>(error, ), - ) + ))) } /// Returns the kind of the error. pub fn kind(&self) -> &$error_kind_name { - &self.0 + &(self.0).0 } /// Iterates over the error chain. @@ -158,7 +151,7 @@ macro_rules! impl_error_chain_processed { /// Returns the backtrace associated with this error. pub fn backtrace(&self) -> Option<&$crate::Backtrace> { - self.1.backtrace() + (self.0).1.backtrace() } /// Extends the error chain with a new entry. @@ -170,15 +163,15 @@ macro_rules! impl_error_chain_processed { impl ::std::error::Error for $error_name { fn description(&self) -> &str { - self.0.description() + (self.0).0.description() } #[allow(unknown_lints, unused_doc_comment)] fn cause(&self) -> Option<&::std::error::Error> { - match self.1.next_error { + match (self.0).1.next_error { Some(ref c) => Some(&**c), None => { - match self.0 { + match (self.0).0 { $( $(#[$meta_foreign_links])* $error_kind_name::$foreign_link_variant(ref foreign_err) => { @@ -194,7 +187,7 @@ macro_rules! impl_error_chain_processed { impl ::std::fmt::Display for $error_name { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - ::std::fmt::Display::fmt(&self.0, f) + ::std::fmt::Display::fmt(&(self.0).0, f) } } @@ -202,10 +195,11 @@ macro_rules! impl_error_chain_processed { $(#[$meta_links])* impl From<$link_error_path> for $error_name { fn from(e: $link_error_path) -> Self { - $error_name( + let e = *e.0; + $error_name(Box::new(( $error_kind_name::$link_variant(e.0), e.1, - ) + ))) } } ) * @@ -243,7 +237,7 @@ macro_rules! impl_error_chain_processed { type Target = $error_kind_name; fn deref(&self) -> &Self::Target { - &self.0 + &(self.0).0 } } @@ -305,7 +299,7 @@ macro_rules! impl_error_chain_processed { impl From<$error_name> for $error_kind_name { fn from(e: $error_name) -> Self { - e.0 + (e.0).0 } } @@ -428,13 +422,13 @@ macro_rules! impl_extract_backtrace { fn extract_backtrace(e: &(::std::error::Error + Send + 'static)) -> Option<::std::sync::Arc<$crate::Backtrace>> { if let Some(e) = e.downcast_ref::<$error_name>() { - return e.1.backtrace.clone(); + return (e.0).1.backtrace.clone(); } $( $( #[$meta_links] )* { if let Some(e) = e.downcast_ref::<$link_error_path>() { - return e.1.backtrace.clone(); + return (e.0).1.backtrace.clone(); } } ) *