Skip to content
Myr

Work around bad diagnostic when missing Context import

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

Previously the compiler would say: error[E0599]: no method named `context` found for type `std::result::Result<(), anyhow::error::Error>` in the current scope --> src/main.rs:83:13 | 83 | context(...)?; | ^^^^^^^ method not found in `std::result::Result<(), anyhow::error::Error>` | = help: items from traits can only be used if the trait is in scope help: the following trait is implemented but not in scope; perhaps add a `use` for it: | 19 | use anyhow::context::Context; where the `anyhow::context::Context` in the suggestion is not a path that is accessible to the user.

authored by David Tolnay · committed by David Tolnay · parent 2a5257a · 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

2 files+63 added−62 removed

modified:src/context.rs+2 added-61 removed
1use crate::Error;1use crate::{Context, Error};2use std::convert::Infallible;3use std::error::Error as StdError;4use std::fmt::{self, Debug, Display};56#[cfg(backtrace)]7use std::backtrace::Backtrace;89/// 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/// ```54pub 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;5960 /// 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}679mod ext {10 use super::*;1112 pub trait StdError {
136 unmodified lines
149 Some(&*self.error)150 }151}152212mod private {153pub(crate) mod private {154 use super::*;155156 pub trait Sealed {}157
3 unmodified lines
modified:src/lib.rs+61 added-1 removed
181 unmodified lines
182183#[cfg(not(feature = "std"))]184compile_error!("no_std support is not implemented yet");185186pub use crate::context::Context;186use std::fmt::Display;187188pub use crate::error::{Chain, Error};189190/// `Result<T, Error>`191///
234 unmodified lines
426 $crate::private::new_adhoc(format!($fmt, $($arg)*))427 };428}429430/// 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/// ```475pub 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;480481 /// 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}488489// Not public API.490#[doc(hidden)]491pub mod private {492 use crate::Error;
14 unmodified lines