@@ -130,10 +130,18 @@ macro_rules! error_chain_processed {
130130 -> $error_name
131131 where E : :: std:: error:: Error + Send + ' static ,
132132 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>
133141 {
134142 $error_name(
135143 kind. into( ) ,
136- $crate:: State :: new:: <$error_name>( Box :: new ( error) , ) ,
144+ $crate:: State :: new:: <$error_name>( error, ) ,
137145 )
138146 }
139147
@@ -302,7 +310,7 @@ macro_rules! error_chain_processed {
302310 // The ResultExt trait defines the `chain_err` method.
303311
304312 /// 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 > {
306314 /// If the `Result` is an `Err` then `chain_err` evaluates the closure,
307315 /// which returns *some type that can be converted to `ErrorKind`*, boxes
308316 /// the original error to store as the cause, then returns a new error
@@ -312,7 +320,7 @@ macro_rules! error_chain_processed {
312320 EK : Into <$error_kind_name>;
313321 }
314322
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 {
316324 fn chain_err<F , EK >( self , callback: F ) -> :: std:: result:: Result <T , $error_name>
317325 where F : FnOnce ( ) -> EK ,
318326 EK : Into <$error_kind_name> {
@@ -323,6 +331,16 @@ macro_rules! error_chain_processed {
323331 }
324332 }
325333
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+
326344
327345 } ;
328346}
0 commit comments