Skip to content
Myr

context.rs

This file

Path

src/context.rs

193 lines · 4.6 KiB · Rust

Last changed by

Merge pull request #414 from dtolnay/nightlyDavid Tolnaybefore adoption213a9c2

History of this file

Take it

The walk stopped after 200 commits: lines older than 553ed38 point at it.

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 1–5why, one step back
1use crate::error::ContextError;
2use crate::{Context, Error, StdError};3use core::convert::Infallible;4use core::fmt::{self, Debug, Display, Write};5

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 6why, one step back
6#[cfg(error_generic_member_access)]

Merge pull request #414 from dtolnay/nightlyDavid Tolnaybefore adoption213a9c2

line 7why, one step back
7use crate::nightly::{self, Request};

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 8–17why, one step back
8
9mod ext {10 use super::*;1112 pub trait StdError {13 fn ext_context<C>(self, context: C) -> Error14 where15 C: Display + Send + Sync + 'static;16 }17

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 18why, one step back
18 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 19–20why, one step back
19 impl<E> StdError for E
20 where

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 21why, one step back
21 E: crate::StdError + Send + Sync + 'static,

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 22–27why, one step back
22 {
23 fn ext_context<C>(self, context: C) -> Error24 where25 C: Display + Send + Sync + 'static,26 {27 let backtrace = backtrace_if_absent!(&self);

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 28why, one step back
28 Error::construct_from_context(context, self, backtrace)

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 29–98why, one step back
29 }
30 }3132 impl StdError for Error {33 fn ext_context<C>(self, context: C) -> Error34 where35 C: Display + Send + Sync + 'static,36 {37 self.context(context)38 }39 }40}4142impl<T, E> Context<T, E> for Result<T, E>43where44 E: ext::StdError + Send + Sync + 'static,45{46 fn context<C>(self, context: C) -> Result<T, Error>47 where48 C: Display + Send + Sync + 'static,49 {50 // Not using map_err to save 2 useless frames off the captured backtrace51 // in ext_context.52 match self {53 Ok(ok) => Ok(ok),54 Err(error) => Err(error.ext_context(context)),55 }56 }5758 fn with_context<C, F>(self, context: F) -> Result<T, Error>59 where60 C: Display + Send + Sync + 'static,61 F: FnOnce() -> C,62 {63 match self {64 Ok(ok) => Ok(ok),65 Err(error) => Err(error.ext_context(context())),66 }67 }68}6970/// ```71/// # type T = ();72/// #73/// use anyhow::{Context, Result};74///75/// fn maybe_get() -> Option<T> {76/// # const IGNORE: &str = stringify! {77/// ...78/// # };79/// # unimplemented!()80/// }81///82/// fn demo() -> Result<()> {83/// let t = maybe_get().context("there is no T")?;84/// # const IGNORE: &str = stringify! {85/// ...86/// # };87/// # unimplemented!()88/// }89/// ```90impl<T> Context<T, Infallible> for Option<T> {91 fn context<C>(self, context: C) -> Result<T, Error>92 where93 C: Display + Send + Sync + 'static,94 {95 // Not using ok_or_else to save 2 useless frames off the captured96 // backtrace.97 match self {98 Some(ok) => Ok(ok),

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 99why, one step back
99 None => Err(Error::construct_from_display(context, backtrace!())),

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 100–109why, one step back
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 {108 match self {109 Some(ok) => Ok(ok),

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 110why, one step back
110 None => Err(Error::construct_from_display(context(), backtrace!())),

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 111–145why, one step back
111 }
112 }113}114115impl<C, E> Debug for ContextError<C, E>116where117 C: Display,118 E: Debug,119{120 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {121 f.debug_struct("Error")122 .field("context", &Quoted(&self.context))123 .field("source", &self.error)124 .finish()125 }126}127128impl<C, E> Display for ContextError<C, E>129where130 C: Display,131{132 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {133 Display::fmt(&self.context, f)134 }135}136137impl<C, E> StdError for ContextError<C, E>138where139 C: Display,140 E: StdError + 'static,141{142 fn source(&self) -> Option<&(dyn StdError + 'static)> {143 Some(&self.error)144 }145

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 146why, one step back
146 #[cfg(error_generic_member_access)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

line 147why, one step back
147 fn provide<'a>(&'a self, request: &mut Request<'a>) {

Merge pull request #414 from dtolnay/nightlyDavid Tolnaybefore adoption213a9c2

line 148why, one step back
148 nightly::provide(&self.error, request);

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 149–159why, one step back
149 }
150}151152impl<C> StdError for ContextError<C, Error>153where154 C: Display,155{156 fn source(&self) -> Option<&(dyn StdError + 'static)> {157 Some(unsafe { crate::ErrorImpl::error(self.error.inner.by_ref()) })158 }159

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 160why, one step back
160 #[cfg(error_generic_member_access)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

lines 161–162why, one step back
161 fn provide<'a>(&'a self, request: &mut Request<'a>) {
162 Error::provide(&self.error, request);

Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38

lines 163–193why, one step back
163 }
164}165166struct Quoted<C>(C);167168impl<C> Debug for Quoted<C>169where170 C: Display,171{172 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {173 formatter.write_char('"')?;174 Quoted(&mut *formatter).write_fmt(format_args!("{}", self.0))?;175 formatter.write_char('"')?;176 Ok(())177 }178}179180impl Write for Quoted<&mut fmt::Formatter<'_>> {181 fn write_str(&mut self, s: &str) -> fmt::Result {182 Display::fmt(&s.escape_debug(), self.0)183 }184}185186pub(crate) mod private {187 use super::*;188189 pub trait Sealed {}190191 impl<T, E> Sealed for Result<T, E> where E: ext::StdError {}192 impl<T> Sealed for Option<T> {}193}