Skip to content
Myr

Move backtrace capture to backtrace module and macro

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent 29f9375 · Browse files · 3 files+25 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+25 added−13 removed

  • other:+25 added−13 removed

3 files+25 added−13 removed

added:src/backtrace.rs+19 added
1#[cfg(backtrace)]2pub(crate) use std::backtrace::Backtrace;34#[cfg(not(backtrace))]5pub(crate) enum Backtrace {}67#[cfg(backtrace)]8macro_rules! backtrace {9 () => {10 Some(Backtrace::capture())11 };12}1314#[cfg(not(backtrace))]15macro_rules! backtrace {16 () => {17 None18 };19}
modified:src/error.rs+3 added-6 removed
1use crate::backtrace::Backtrace;2use crate::context::ContextError;3use std::any::TypeId;4use std::error::Error as StdError;5use std::fmt::{self, Debug, Display};6use std::mem;7use std::ops::{Deref, DerefMut};8use std::ptr;99#[cfg(backtrace)]10use std::backtrace::{Backtrace, BacktraceStatus};1110/// The `Error` type, a wrapper around a dynamic error type.11///12/// `Error` works a lot like `Box<dyn std::error::Error>`, but with these13/// differences:
272 unmodified lines
286 }287 }288}289292#[cfg(not(backtrace))]293pub(crate) enum Backtrace {}294290impl<E> From<E> for Error291where292 E: StdError + Send + Sync + 'static,293{
33 unmodified lines
327 }328329 #[cfg(backtrace)]330 {331 use std::backtrace::BacktraceStatus;332333 let backtrace = self.backtrace();334 match backtrace.status() {335 BacktraceStatus::Captured => {336 writeln!(f, "\n{}", backtrace)?;
175 unmodified lines
modified:src/lib.rs+3 added-7 removed
172 unmodified lines
173174#![doc(html_root_url = "https://docs.rs/anyhow/1.0.2")]175#![cfg_attr(backtrace, feature(backtrace))]176177#[macro_use]178mod backtrace;179mod context;180mod error;181182#[cfg(not(feature = "std"))]
224 unmodified lines
407 pub fn new_adhoc<M>(message: M) -> Error408 where409 M: Display + Debug + Send + Sync + 'static,410 {409 #[cfg(backtrace)]410 let backtrace = Some(Backtrace::capture());411412 #[cfg(not(backtrace))]413 let backtrace = None;414415 Error::new_adhoc(message, backtrace)411 Error::new_adhoc(message, backtrace!())412 }413}