@@ -26,8 +26,7 @@ macro_rules! error_chain_processed {
2626 }
2727 $( $rest ) *
2828 }
29- /// Convenient wrapper around `std::Result`.
30- pub type $result_name<T > = :: std:: result:: Result <T , $error_name>;
29+ result_wrapper!( $result_name, $error_name) ;
3130 } ;
3231 // Without `Result` wrapper.
3332 (
@@ -70,14 +69,9 @@ macro_rules! error_chain_processed {
7069
7170 impl $crate:: ChainedError for $error_name {
7271 type ErrorKind = $error_kind_name;
72+ maybe_boxed_error!( ) ;
7373
74- fn new( kind: $error_kind_name, state: $crate:: State ) -> $error_name {
75- $error_name {
76- kind: kind,
77- state: state,
78- }
79- }
80-
74+ impl_chained_error_new!( $error_name, $error_kind_name) ;
8175 impl_extract_backtrace!( $error_name
8276 $error_kind_name
8377 $( [ $link_error_path, $( #[ $meta_links] ) * ] ) * ) ;
@@ -148,6 +142,15 @@ macro_rules! error_chain_processed {
148142 }
149143 }
150144 }
145+ $( #[ $meta_links] ) *
146+ impl From <$link_error_path> for Box <$error_name> {
147+ fn from( e: $link_error_path) -> Self {
148+ Box :: new( $error_name {
149+ kind: $error_kind_name:: $link_variant( e. kind) ,
150+ state: e. state,
151+ } )
152+ }
153+ }
151154 ) *
152155
153156 $(
@@ -159,6 +162,14 @@ macro_rules! error_chain_processed {
159162 )
160163 }
161164 }
165+ $( #[ $meta_foreign_links] ) *
166+ impl From <$foreign_link_error_path> for Box <$error_name> {
167+ fn from( e: $foreign_link_error_path) -> Self {
168+ Box :: new( $error_name:: from_kind(
169+ $error_kind_name:: $foreign_link_variant( e)
170+ ) )
171+ }
172+ }
162173 ) *
163174
164175 impl From <$error_kind_name> for $error_name {
@@ -360,3 +371,69 @@ macro_rules! impl_extract_backtrace {
360371 $error_kind_name: ident
361372 $( [ $link_error_path: path, $( #[ $meta_links: meta] ) * ] ) * ) => { }
362373}
374+
375+ #[ macro_export]
376+ #[ doc( hidden) ]
377+ #[ cfg( feature = "boxed-error" ) ]
378+ macro_rules! result_wrapper {
379+ ( $result_name: ident, $error_name: ident) => {
380+ /// Convenient wrapper around `std::Result`.
381+ pub type $result_name<T > = :: std:: result:: Result <T , Box <$error_name>>;
382+ }
383+ }
384+
385+ #[ macro_export]
386+ #[ doc( hidden) ]
387+ #[ cfg( not( feature = "boxed-error" ) ) ]
388+ macro_rules! result_wrapper {
389+ ( $result_name: ident, $error_name: ident) => {
390+ /// Convenient wrapper around `std::Result`.
391+ pub type $result_name<T > = :: std:: result:: Result <T , $error_name>;
392+ }
393+ }
394+
395+ #[ macro_export]
396+ #[ doc( hidden) ]
397+ #[ cfg( feature = "boxed-error" ) ]
398+ macro_rules! maybe_boxed_error {
399+ ( ) => {
400+ type Error = Box <Self >;
401+ }
402+ }
403+
404+ #[ macro_export]
405+ #[ doc( hidden) ]
406+ #[ cfg( not( feature = "boxed-error" ) ) ]
407+ macro_rules! maybe_boxed_error {
408+ ( ) => {
409+ type Error = Self ;
410+ }
411+ }
412+
413+ #[ macro_export]
414+ #[ doc( hidden) ]
415+ #[ cfg( feature = "boxed-error" ) ]
416+ macro_rules! impl_chained_error_new {
417+ ( $error_name: ident, $error_kind_name: ident) => {
418+ fn new( kind: $error_kind_name, state: $crate:: State ) -> Box <$error_name> {
419+ Box :: new( $error_name {
420+ kind: kind,
421+ state: state,
422+ } )
423+ }
424+ }
425+ }
426+
427+ #[ macro_export]
428+ #[ doc( hidden) ]
429+ #[ cfg( not( feature = "boxed-error" ) ) ]
430+ macro_rules! impl_chained_error_new {
431+ ( $error_name: ident, $error_kind_name: ident) => {
432+ fn new( kind: $error_kind_name, state: $crate:: State ) -> $error_name {
433+ $error_name {
434+ kind: kind,
435+ state: state,
436+ }
437+ }
438+ }
439+ }
0 commit comments