Skip to content
Myr

Remove 2 frames of backtrace noise on Option's Context impl

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

Repro: use anyhow::Context; fn main() -> anyhow::Result<()> { let result = None::<()>; result.context("...") } Before: 0: anyhow::context::<impl anyhow::Context<T,core::convert::Infallible> for core::option::Option<T>>::context::{{closure}} at /git/anyhow/src/context.rs:95:57 1: core::option::Option<T>::ok_or_else at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/core/src/option.rs:1083:25 2: anyhow::context::<impl anyhow::Context<T,core::convert::Infallible> for core::option::Option<T>>::context at /git/anyhow/src/context.rs:95:9 3: testing::main at ./src/main.rs:5:5 After: 0: anyhow::context::<impl anyhow::Context<T,core::convert::Infallible> for core::option::Option<T>>::context at /git/anyhow/src/context.rs:99:54 1: testing::main at ./src/main.rs:5:5

authored by David Tolnay · committed by David Tolnay · parent 8de29aa · Browse files · 1 file+10 added−2 removed
This commit

State

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

Diff stats

1 file+10 added−2 removed

  • other:+10 added−2 removed

1 file+10 added−2 removed

modified:src/context.rs+10 added-2 removed
90 unmodified lines
91 fn context<C>(self, context: C) -> Result<T, Error>92 where93 C: Display + Send + Sync + 'static,94 {95 self.ok_or_else(|| Error::from_display(context, backtrace!()))95 // Not using ok_or_else to save 2 useless frames off the captured96 // backtrace.97 match self {98 Some(ok) => Ok(ok),99 None => Err(Error::from_display(context, backtrace!())),100 }101 }102103 fn with_context<C, F>(self, context: F) -> Result<T, Error>104 where105 C: Display + Send + Sync + 'static,106 F: FnOnce() -> C,107 {103 self.ok_or_else(|| Error::from_display(context(), backtrace!()))108 match self {109 Some(ok) => Ok(ok),110 None => Err(Error::from_display(context(), backtrace!())),111 }112 }113}114115impl<C, E> Debug for ContextError<C, E>
79 unmodified lines