@@ -26,8 +26,7 @@ macro_rules! error_chain_processed {
26
26
}
27
27
$( $rest ) *
28
28
}
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) ;
31
30
} ;
32
31
// Without `Result` wrapper.
33
32
(
@@ -70,14 +69,9 @@ macro_rules! error_chain_processed {
70
69
71
70
impl $crate:: ChainedError for $error_name {
72
71
type ErrorKind = $error_kind_name;
72
+ maybe_boxed_error!( ) ;
73
73
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) ;
81
75
impl_extract_backtrace!( $error_name
82
76
$error_kind_name
83
77
$( [ $link_error_path, $( #[ $meta_links] ) * ] ) * ) ;
@@ -148,6 +142,15 @@ macro_rules! error_chain_processed {
148
142
}
149
143
}
150
144
}
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
+ }
151
154
) *
152
155
153
156
$(
@@ -159,6 +162,14 @@ macro_rules! error_chain_processed {
159
162
)
160
163
}
161
164
}
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
+ }
162
173
) *
163
174
164
175
impl From <$error_kind_name> for $error_name {
@@ -360,3 +371,69 @@ macro_rules! impl_extract_backtrace {
360
371
$error_kind_name: ident
361
372
$( [ $link_error_path: path, $( #[ $meta_links: meta] ) * ] ) * ) => { }
362
373
}
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