diff --git a/libimagerror/src/trace.rs b/libimagerror/src/trace.rs index 5100300f..30687eeb 100644 --- a/libimagerror/src/trace.rs +++ b/libimagerror/src/trace.rs @@ -87,3 +87,49 @@ fn print_trace_dbg(idx: u64, e: &Error) { } } +/// Helper functions for `Result` types to reduce overhead in the following situations: +/// +/// ```ignore +/// function().map_err(|e| { trace_error(&e); e }) +/// ``` +/// +/// and variants +pub trait MapErrTrace { + fn map_err_trace(self) -> Self; + fn map_err_dbg_trace(self) -> Self; + fn map_err_trace_exit(self, code: i32) -> Self; + fn map_err_trace_maxdepth(self, max: u64) -> Self; +} + +impl MapErrTrace for Result { + + /// Simply call `trace_error()` on the Err (if there is one) and return the error. + /// + /// This does nothing besides the side effect of printing the error trace + fn map_err_trace(self) -> Self { + self.map_err(|e| { trace_error(&e); e }) + } + + /// Simply call `trace_error_dbg()` on the Err (if there is one) and return the error. + /// + /// This does nothing besides the side effect of printing the error trace + fn map_err_dbg_trace(self) -> Self { + self.map_err(|e| { trace_error_dbg(&e); e }) + } + + /// Simply call `trace_error_exit(code)` on the Err (if there is one). + /// + /// This does not return if there is an Err(e). + fn map_err_trace_exit(self, code: i32) -> Self { + self.map_err(|e| { trace_error_exit(&e, code) }) + } + + /// Simply call `trace_error_maxdepth(max)` on the Err (if there is one) and return the error. + /// + /// This does nothing besides the side effect of printing the error trace to a certain depth + fn map_err_trace_maxdepth(self, max: u64) -> Self { + self.map_err(|e| { trace_error_maxdepth(&e, max); e }) + } + +} +