use std::backtrace::{Backtrace, BacktraceStatus};
pub trait ErrorBacktrace {
fn trace(self) -> Self
where
Self: Sized,
{
#[cfg(all(feature = "debug-only", not(debug_assertions)))]
return self;
if !self.is_error() {
return self;
}
let backtrace = Backtrace::capture();
match backtrace.status() {
BacktraceStatus::Unsupported => {
println!("Backtrace is not supported on this platform");
}
BacktraceStatus::Disabled => {
return self;
}
BacktraceStatus::Captured => {}
_ => unreachable!(),
}
println!("Error Backtrace:\n{}\n", backtrace);
self
}
fn is_error(&self) -> bool;
}
impl<T1, T2> ErrorBacktrace for Result<T1, T2> {
fn is_error(&self) -> bool {
self.is_err()
}
}
impl<T1> ErrorBacktrace for Option<T1> {
fn is_error(&self) -> bool {
self.is_none()
}
}