mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-29 22:11:55 +00:00
fix type inference issue in eyre macro autoderef behavior (#27)
* fix type inference issue in eyre macro autoderef behavior * use the upstream example * update testcase compile err output * remove cruft * bump version for breaking change
This commit is contained in:
parent
fd00a3bbab
commit
96ddb39cd0
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "eyre"
|
||||
version = "0.4.3" # remember to update html_root_url
|
||||
version = "0.5.0" # remember to update html_root_url
|
||||
authors = ["David Tolnay <dtolnay@gmail.com>", "Jane Lusby <jlusby42@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
@ -18,7 +18,7 @@ This crate is a fork of [`anyhow`] by @dtolnay with a support for customized
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
eyre = "0.4"
|
||||
eyre = "0.5"
|
||||
```
|
||||
|
||||
## Custom Report Handlers
|
||||
@ -153,7 +153,7 @@ Cargo.toml. A global allocator is required.
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
eyre = { version = "0.4", default-features = false }
|
||||
eyre = { version = "0.5", default-features = false }
|
||||
```
|
||||
|
||||
Since the `?`-based error conversions would normally rely on the
|
||||
|
60
src/kind.rs
60
src/kind.rs
@ -51,9 +51,6 @@ use core::fmt::{Debug, Display};
|
||||
#[cfg(feature = "std")]
|
||||
use crate::StdError;
|
||||
|
||||
// #[cfg(backtrace)]
|
||||
// use std::backtrace::Backtrace;
|
||||
|
||||
pub struct Adhoc;
|
||||
|
||||
pub trait AdhocKind: Sized {
|
||||
@ -74,21 +71,27 @@ impl Adhoc {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Trait;
|
||||
pub struct Trait<H>(std::marker::PhantomData<H>);
|
||||
|
||||
pub trait TraitKind: Sized {
|
||||
pub trait TraitKind<H>: Sized {
|
||||
#[inline]
|
||||
fn eyre_kind(&self) -> Trait {
|
||||
Trait
|
||||
fn eyre_kind(&self) -> Trait<H> {
|
||||
Trait(std::marker::PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> TraitKind for E where E: Into<Report> {}
|
||||
impl<E, H> TraitKind<H> for E
|
||||
where
|
||||
E: Into<Report<H>>,
|
||||
H: EyreHandler,
|
||||
{
|
||||
}
|
||||
|
||||
impl Trait {
|
||||
pub fn new<E>(self, error: E) -> Report
|
||||
impl<H> Trait<H> {
|
||||
pub fn new<E>(self, error: E) -> Report<H>
|
||||
where
|
||||
E: Into<Report>,
|
||||
E: Into<Report<H>>,
|
||||
H: EyreHandler,
|
||||
{
|
||||
error.into()
|
||||
}
|
||||
@ -114,3 +117,38 @@ impl Boxed {
|
||||
Report::from_boxed(error)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::eyre;
|
||||
use std::num::ParseIntError;
|
||||
|
||||
struct NonDefaultHandler;
|
||||
|
||||
impl EyreHandler for NonDefaultHandler {
|
||||
#[allow(unused_variables)]
|
||||
fn default(error: &(dyn StdError + 'static)) -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
fn debug(
|
||||
&self,
|
||||
_error: &(dyn StdError + 'static),
|
||||
_f: &mut core::fmt::Formatter<'_>,
|
||||
) -> core::fmt::Result {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn _parse(s: &str) -> Result<i32, ParseIntError> {
|
||||
s.parse::<i32>()
|
||||
}
|
||||
|
||||
fn _throw_error() -> Result<(), Report<NonDefaultHandler>> {
|
||||
match _parse("abc") {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(eyre!(e).wrap_err("try parsing an actual number")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -206,7 +206,7 @@
|
||||
//!
|
||||
//! ```toml
|
||||
//! [dependencies]
|
||||
//! eyre = { version = "0.4", default-features = false }
|
||||
//! eyre = { version = "0.5", default-features = false }
|
||||
//! ```
|
||||
//!
|
||||
//! Since the `?`-based error conversions would normally rely on the
|
||||
@ -317,7 +317,7 @@
|
||||
//! [`simple-eyre`]: https://github.com/yaahc/simple-eyre
|
||||
//! [`color-spantrace`]: https://github.com/yaahc/color-spantrace
|
||||
//! [`color-backtrace`]: https://github.com/athre0z/color-backtrace
|
||||
#![doc(html_root_url = "https://docs.rs/eyre/0.4.3")]
|
||||
#![doc(html_root_url = "https://docs.rs/eyre/0.5.0")]
|
||||
#![warn(
|
||||
missing_debug_implementations,
|
||||
missing_docs,
|
||||
|
@ -28,13 +28,13 @@ fn test_boxed_thiserror() {
|
||||
let error = MyError {
|
||||
source: io::Error::new(io::ErrorKind::Other, "oh no!"),
|
||||
};
|
||||
let error = eyre!(error);
|
||||
let error: Report = eyre!(error);
|
||||
assert_eq!("oh no!", error.source().unwrap().to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boxed_eyre() {
|
||||
let error = eyre!("oh no!").wrap_err("it failed");
|
||||
let error: Report = eyre!("oh no!").wrap_err("it failed");
|
||||
let error = eyre!(error);
|
||||
assert_eq!("oh no!", error.source().unwrap().to_string());
|
||||
}
|
||||
|
@ -4,18 +4,18 @@ error[E0599]: no method named `eyre_kind` found for reference `&Error` in the cu
|
||||
4 | struct Error;
|
||||
| -------------
|
||||
| |
|
||||
| doesn't satisfy `Error: eyre::kind::TraitKind`
|
||||
| doesn't satisfy `Error: std::convert::Into<eyre::Report>`
|
||||
| doesn't satisfy `Error: eyre::kind::TraitKind<_>`
|
||||
| doesn't satisfy `Error: std::convert::Into<eyre::Report<_>>`
|
||||
| doesn't satisfy `Error: std::fmt::Display`
|
||||
...
|
||||
7 | let _ = eyre!(Error);
|
||||
| ^^^^^^^^^^^^ method not found in `&Error`
|
||||
|
|
||||
= note: the method `eyre_kind` exists but the following trait bounds were not satisfied:
|
||||
`Error: std::convert::Into<eyre::Report>`
|
||||
which is required by `Error: eyre::kind::TraitKind`
|
||||
`Error: std::convert::Into<eyre::Report<_>>`
|
||||
which is required by `Error: eyre::kind::TraitKind<_>`
|
||||
`Error: std::fmt::Display`
|
||||
which is required by `&Error: eyre::kind::AdhocKind`
|
||||
`&Error: std::convert::Into<eyre::Report>`
|
||||
which is required by `&Error: eyre::kind::TraitKind`
|
||||
`&Error: std::convert::Into<eyre::Report<_>>`
|
||||
which is required by `&Error: eyre::kind::TraitKind<_>`
|
||||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
Loading…
x
Reference in New Issue
Block a user