@@ -130,10 +130,18 @@ macro_rules! error_chain_processed {
130
130
-> $error_name
131
131
where E : :: std:: error:: Error + Send + ' static ,
132
132
K : Into <$error_kind_name>
133
+ {
134
+ $error_name:: with_boxed_chain( Box :: new( error) , kind)
135
+ }
136
+
137
+ /// Construct a chained error from another boxed error and a kind, and generates a backtrace
138
+ pub fn with_boxed_chain<K >( error: Box <:: std:: error:: Error + Send >, kind: K )
139
+ -> $error_name
140
+ where K : Into <$error_kind_name>
133
141
{
134
142
$error_name(
135
143
kind. into( ) ,
136
- $crate:: State :: new:: <$error_name>( Box :: new ( error) , ) ,
144
+ $crate:: State :: new:: <$error_name>( error, ) ,
137
145
)
138
146
}
139
147
@@ -302,7 +310,7 @@ macro_rules! error_chain_processed {
302
310
// The ResultExt trait defines the `chain_err` method.
303
311
304
312
/// Additional methods for `Result`, for easy interaction with this crate.
305
- pub trait $result_ext_name<T , E > {
313
+ pub trait $result_ext_name<T > {
306
314
/// If the `Result` is an `Err` then `chain_err` evaluates the closure,
307
315
/// which returns *some type that can be converted to `ErrorKind`*, boxes
308
316
/// the original error to store as the cause, then returns a new error
@@ -312,7 +320,7 @@ macro_rules! error_chain_processed {
312
320
EK : Into <$error_kind_name>;
313
321
}
314
322
315
- impl <T , E > $result_ext_name<T , E > for :: std:: result:: Result <T , E > where E : :: std:: error:: Error + Send + ' static {
323
+ impl <T , E > $result_ext_name<T > for :: std:: result:: Result <T , E > where E : :: std:: error:: Error + Send + ' static {
316
324
fn chain_err<F , EK >( self , callback: F ) -> :: std:: result:: Result <T , $error_name>
317
325
where F : FnOnce ( ) -> EK ,
318
326
EK : Into <$error_kind_name> {
@@ -323,6 +331,16 @@ macro_rules! error_chain_processed {
323
331
}
324
332
}
325
333
334
+ impl <T > $result_ext_name<T > for :: std:: option:: Option <T > {
335
+ fn chain_err<F , EK >( self , callback: F ) -> :: std:: result:: Result <T , $error_name>
336
+ where F : FnOnce ( ) -> EK ,
337
+ EK : Into <$error_kind_name> {
338
+ self . ok_or_else( move || {
339
+ $crate:: ChainedError :: from_kind( callback( ) . into( ) )
340
+ } )
341
+ }
342
+ }
343
+
326
344
327
345
} ;
328
346
}
0 commit comments