make it easier to conditionally use a backtrace

This commit is contained in:
Jacob Rothstein 2020-08-04 17:34:27 -07:00
parent c29011aa01
commit 4c188ff4fc
1 changed files with 13 additions and 2 deletions

View File

@ -85,8 +85,19 @@ impl Error {
///
/// [tracking]: https://github.com/rust-lang/rust/issues/53487
#[cfg(backtrace)]
pub fn backtrace(&self) -> &std::backtrace::Backtrace {
self.error.backtrace()
pub fn backtrace(&self) -> Option<&std::backtrace::Backtrace> {
let backtrace = self.error.backtrace();
if let std::backtrace::BacktraceStatus::Captured = backtrace.status() {
Some(backtrace)
} else {
None
}
}
#[cfg(not(backtrace))]
#[allow(missing_docs)]
pub fn backtrace(&self) -> Option<()> {
None
}
/// Attempt to downcast the error object to a concrete type.