Merge pull request #47 from dtolnay/anyhow

Handle anyhow::Error as argument to anyhow!()
This commit is contained in:
David Tolnay 2019-11-18 11:46:34 -08:00 committed by GitHub
commit cb3c80cca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -45,7 +45,6 @@
// (&error).anyhow_kind().new(error)
use crate::Error;
use std::error::Error as StdError;
use std::fmt::{Debug, Display};
#[cfg(backtrace)]
@ -80,13 +79,13 @@ pub trait TraitKind: Sized {
}
}
impl<T> TraitKind for T where T: StdError + Send + Sync + 'static {}
impl<E> TraitKind for E where E: Into<Error> {}
impl Trait {
pub fn new<E>(self, error: E) -> Error
where
E: StdError + Send + Sync + 'static,
E: Into<Error>,
{
Error::from_std(error, backtrace!())
error.into()
}
}

View File

@ -53,3 +53,10 @@ fn test_io_source() {
let error = anyhow!(TestError::Io(io));
assert_eq!("oh no!", error.source().unwrap().to_string());
}
#[test]
fn test_anyhow_from_anyhow() {
let error = anyhow!("oh no!").context("context");
let error = anyhow!(error);
assert_eq!("oh no!", error.source().unwrap().to_string());
}