Merge pull request #31 from dtolnay/diagnostic
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Work around bad diagnostic when missing Context import
authored by David Tolnay · committed by GitHub · parents 2a5257a, cf77cab · Browse files · 2 files·+63 added−62 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+63 added−62 removed
- other:+63 added−62 removed
Provenance
- Commit
fc44da4- Tree
fa91d0f- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2a5257a- Parent 2
cf77cab
2 files changed+63 added−62 removed
modified:src/context.rs+2 added-61 removed
1-use crate::Error;1+use crate::{Context, Error};2 use std::convert::Infallible;3 use std::error::Error as StdError;4 use std::fmt::{self, Debug, Display};5 6 #[cfg(backtrace)]7 use std::backtrace::Backtrace;8 9-/// Provides the `context` method for `Result`.10-///11-/// This trait is sealed and cannot be implemented for types outside of12-/// `anyhow`.13-///14-/// # Example15-///16-/// ```17-/// use anyhow::{Context, Result};18-/// use std::fs;19-/// use std::path::PathBuf;20-///21-/// pub struct ImportantThing {22-/// path: PathBuf,23-/// }24-///25-/// impl ImportantThing {26-/// # const IGNORE: &'static str = stringify! {27-/// pub fn detach(&mut self) -> Result<()> {...}28-/// # };29-/// # fn detach(&mut self) -> Result<()> {30-/// # unimplemented!()31-/// # }32-/// }33-///34-/// pub fn do_it(mut it: ImportantThing) -> Result<Vec<u8>> {35-/// it.detach().context("failed to detach the important thing")?;36-///37-/// let path = &it.path;38-/// let content = fs::read(path)39-/// .with_context(|| format!("failed to read instrs from {}", path.display()))?;40-///41-/// Ok(content)42-/// }43-/// ```44-///45-/// When printed, the outermost context would be printed first and the lower46-/// level underlying causes would be enumerated below.47-///48-/// ```console49-/// Error: failed to read instrs from ./path/to/instrs.jsox50-///51-/// caused by:52-/// No such file or directory (os error 2)53-/// ```54-pub trait Context<T, E>: private::Sealed {55- /// Wrap the error value with additional context.56- fn context<C>(self, context: C) -> Result<T, Error>57- where58- C: Display + Send + Sync + 'static;59-60- /// Wrap the error value with additional context that is evaluated lazily61- /// only once an error does occur.62- fn with_context<C, F>(self, f: F) -> Result<T, Error>63- where64- C: Display + Send + Sync + 'static,65- F: FnOnce() -> C;66-}67-9 mod ext {10 use super::*;11 12 pub trait StdError {136 unmodified lines149 Some(&*self.error)150 }151 }152 212-mod private {153+pub(crate) mod private {154 use super::*;155 156 pub trait Sealed {}157 3 unmodified linesmodified:src/lib.rs+61 added-1 removed
181 unmodified lines182 183 #[cfg(not(feature = "std"))]184 compile_error!("no_std support is not implemented yet");185 186-pub use crate::context::Context;186+use std::fmt::Display;187+188 pub use crate::error::{Chain, Error};189 190 /// `Result<T, Error>`191 ///234 unmodified lines426 $crate::private::new_adhoc(format!($fmt, $($arg)*))427 };428 }429 430+/// Provides the `context` method for `Result`.431+///432+/// This trait is sealed and cannot be implemented for types outside of433+/// `anyhow`.434+///435+/// # Example436+///437+/// ```438+/// use anyhow::{Context, Result};439+/// use std::fs;440+/// use std::path::PathBuf;441+///442+/// pub struct ImportantThing {443+/// path: PathBuf,444+/// }445+///446+/// impl ImportantThing {447+/// # const IGNORE: &'static str = stringify! {448+/// pub fn detach(&mut self) -> Result<()> {...}449+/// # };450+/// # fn detach(&mut self) -> Result<()> {451+/// # unimplemented!()452+/// # }453+/// }454+///455+/// pub fn do_it(mut it: ImportantThing) -> Result<Vec<u8>> {456+/// it.detach().context("failed to detach the important thing")?;457+///458+/// let path = &it.path;459+/// let content = fs::read(path)460+/// .with_context(|| format!("failed to read instrs from {}", path.display()))?;461+///462+/// Ok(content)463+/// }464+/// ```465+///466+/// When printed, the outermost context would be printed first and the lower467+/// level underlying causes would be enumerated below.468+///469+/// ```console470+/// Error: failed to read instrs from ./path/to/instrs.jsox471+///472+/// caused by:473+/// No such file or directory (os error 2)474+/// ```475+pub trait Context<T, E>: context::private::Sealed {476+ /// Wrap the error value with additional context.477+ fn context<C>(self, context: C) -> Result<T, Error>478+ where479+ C: Display + Send + Sync + 'static;480+481+ /// Wrap the error value with additional context that is evaluated lazily482+ /// only once an error does occur.483+ fn with_context<C, F>(self, f: F) -> Result<T, Error>484+ where485+ C: Display + Send + Sync + 'static,486+ F: FnOnce() -> C;487+}488+489 // Not public API.490 #[doc(hidden)]491 pub mod private {492 use crate::Error;14 unmodified lines