Skip to content
Myr

context.rs

This file

Path

src/context.rs

193 lines · 4.5 KiB · Rust

Last changed by

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

History of this file

Take it

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

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 1–6why, one step back
1use crate::error::ContextError;
2use crate::{Context, Error, StdError};3use core::convert::Infallible;4use core::fmt::{self, Debug, Display, Write};56#[cfg(backtrace)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

line 7why, one step back
7use std::error::Request;

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 8–26why, 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 }1718 #[cfg(feature = "std")]19 impl<E> StdError for E20 where21 E: std::error::Error + Send + Sync + 'static,22 {23 fn ext_context<C>(self, context: C) -> Error24 where25 C: Display + Send + Sync + 'static,26 {

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

line 27why, one step back
27 let backtrace = backtrace_if_absent!(&self);

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 28–49why, one step back
28 Error::from_context(context, self, backtrace)
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 {

Merge pull request #279 from dtolnay/contextbacktraceDavid Tolnaybefore adoption54fc812

lines 50–55why, one step back
50 // Not using map_err to save 2 useless frames off the captured backtrace
51 // in ext_context.52 match self {53 Ok(ok) => Ok(ok),54 Err(error) => Err(error.ext_context(context)),55 }

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 56–62why, one step back
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 {

Merge pull request #279 from dtolnay/contextbacktraceDavid Tolnaybefore adoption54fc812

lines 63–66why, one step back
63 match self {
64 Ok(ok) => Ok(ok),65 Err(error) => Err(error.ext_context(context())),66 }

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 67–94why, one step back
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 {

Merge pull request #280 from dtolnay/contextbacktraceDavid Tolnaybefore adoption47e902a

lines 95–100why, one step back
95 // Not using ok_or_else to save 2 useless frames off the captured
96 // backtrace.97 match self {98 Some(ok) => Ok(ok),99 None => Err(Error::from_display(context, backtrace!())),100 }

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 101–107why, one step back
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 {

Merge pull request #280 from dtolnay/contextbacktraceDavid Tolnaybefore adoption47e902a

lines 108–111why, one step back
108 match self {
109 Some(ok) => Ok(ok),110 None => Err(Error::from_display(context(), backtrace!())),111 }

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 112–144why, one step back
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 }

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

lines 145–146why, one step back
145
146 #[cfg(backtrace)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

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

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

line 149why, one step back
149 }

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

lines 150–157why, one step back
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()) })

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

lines 158–160why, one step back
158 }
159160 #[cfg(backtrace)]

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);

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

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}