mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-10-02 07:21:36 +00:00
feat: introduce an "anyhow"
compatibility layer feature flag (#138)
This change hides the `anyhow` compatibility layer behind an `"anyhow"` feature flag. In `eyre` v1.0.0 the feature is currently enabled by default. Fixes #131 --------- Co-authored-by: Freja Roberts <ten3roberts@gmail.com>
This commit is contained in:
parent
d6c0b8d8a3
commit
34bd1d9893
12
README.md
12
README.md
@ -200,7 +200,17 @@ This crate does its best to be usable as a drop in replacement of `anyhow` and
|
|||||||
vice-versa by `re-exporting` all of the renamed APIs with the names used in
|
vice-versa by `re-exporting` all of the renamed APIs with the names used in
|
||||||
`anyhow`, though there are some differences still.
|
`anyhow`, though there are some differences still.
|
||||||
|
|
||||||
#### `Context` and `Option`
|
### Disabling the compatibility layer
|
||||||
|
|
||||||
|
The `anyhow` compatibility layer is enabled by default.
|
||||||
|
If you do not need anyhow compatibility, it is advisable
|
||||||
|
to disable the `"anyhow"` feature:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
eyre = { version = "0.6", default-features = false, features = ["auto-install", "track-caller"] }
|
||||||
|
```
|
||||||
|
|
||||||
|
### `Context` and `Option`
|
||||||
|
|
||||||
As part of renaming `Context` to `WrapErr` we also intentionally do not
|
As part of renaming `Context` to `WrapErr` we also intentionally do not
|
||||||
implement `WrapErr` for `Option`. This decision was made because `wrap_err`
|
implement `WrapErr` for `Option`. This decision was made because `wrap_err`
|
||||||
|
@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
<!-- next-header -->
|
<!-- next-header -->
|
||||||
|
|
||||||
## [Unreleased] - ReleaseDate
|
## [Unreleased] - ReleaseDate
|
||||||
|
### Added
|
||||||
|
- feature flag for `anyhow` compatibility traits [by LeoniePhiline](https://github.com/eyre-rs/eyre/pull/138)
|
||||||
|
|
||||||
## [0.6.11] - 2023-12-13
|
## [0.6.11] - 2023-12-13
|
||||||
### Fixed
|
### Fixed
|
||||||
|
@ -13,7 +13,8 @@ readme = { workspace = true }
|
|||||||
rust-version = { workspace = true }
|
rust-version = { workspace = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["auto-install", "track-caller"]
|
default = ["anyhow", "auto-install", "track-caller"]
|
||||||
|
anyhow = []
|
||||||
auto-install = []
|
auto-install = []
|
||||||
track-caller = []
|
track-caller = []
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::error::{ContextError, ErrorImpl};
|
use crate::error::{ContextError, ErrorImpl};
|
||||||
use crate::{ContextCompat, Report, StdError, WrapErr};
|
use crate::{Report, StdError, WrapErr};
|
||||||
use core::fmt::{self, Debug, Display, Write};
|
use core::fmt::{self, Debug, Display, Write};
|
||||||
|
|
||||||
#[cfg(backtrace)]
|
#[cfg(backtrace)]
|
||||||
@ -62,6 +62,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
fn context<D>(self, msg: D) -> Result<T, Report>
|
fn context<D>(self, msg: D) -> Result<T, Report>
|
||||||
where
|
where
|
||||||
D: Display + Send + Sync + 'static,
|
D: Display + Send + Sync + 'static,
|
||||||
@ -69,6 +70,7 @@ where
|
|||||||
self.wrap_err(msg)
|
self.wrap_err(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
|
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
|
||||||
where
|
where
|
||||||
D: Display + Send + Sync + 'static,
|
D: Display + Send + Sync + 'static,
|
||||||
@ -78,7 +80,8 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ContextCompat<T> for Option<T> {
|
#[cfg(feature = "anyhow")]
|
||||||
|
impl<T> crate::ContextCompat<T> for Option<T> {
|
||||||
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
|
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
|
||||||
where
|
where
|
||||||
D: Display + Send + Sync + 'static,
|
D: Display + Send + Sync + 'static,
|
||||||
|
@ -116,6 +116,7 @@ impl Report {
|
|||||||
unsafe { Report::construct(error, vtable, handler) }
|
unsafe { Report::construct(error, vtable, handler) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[cfg_attr(track_caller, track_caller)]
|
#[cfg_attr(track_caller, track_caller)]
|
||||||
pub(crate) fn from_display<M>(message: M) -> Self
|
pub(crate) fn from_display<M>(message: M) -> Self
|
||||||
where
|
where
|
||||||
|
@ -265,7 +265,17 @@
|
|||||||
//! vice-versa by re-exporting all of the renamed APIs with the names used in
|
//! vice-versa by re-exporting all of the renamed APIs with the names used in
|
||||||
//! `anyhow`, though there are some differences still.
|
//! `anyhow`, though there are some differences still.
|
||||||
//!
|
//!
|
||||||
//! #### `Context` and `Option`
|
//! ### Disabling the compatibility layer
|
||||||
|
//!
|
||||||
|
//! The `anyhow` compatibility layer is enabled by default.
|
||||||
|
//! If you do not need anyhow compatibility, it is advisable
|
||||||
|
//! to disable the `"anyhow"` feature:
|
||||||
|
//!
|
||||||
|
//! ```toml
|
||||||
|
//! eyre = { version = "0.6", default-features = false, features = ["auto-install", "track-caller"] }
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ### `Context` and `Option`
|
||||||
//!
|
//!
|
||||||
//! As part of renaming `Context` to `WrapErr` we also intentionally do not
|
//! As part of renaming `Context` to `WrapErr` we also intentionally do not
|
||||||
//! implement `WrapErr` for `Option`. This decision was made because `wrap_err`
|
//! implement `WrapErr` for `Option`. This decision was made because `wrap_err`
|
||||||
@ -375,18 +385,23 @@ use std::error::Error as StdError;
|
|||||||
|
|
||||||
pub use eyre as format_err;
|
pub use eyre as format_err;
|
||||||
/// Compatibility re-export of `eyre` for interop with `anyhow`
|
/// Compatibility re-export of `eyre` for interop with `anyhow`
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
pub use eyre as anyhow;
|
pub use eyre as anyhow;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use ptr::OwnedPtr;
|
use ptr::OwnedPtr;
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use DefaultHandler as DefaultContext;
|
pub use DefaultHandler as DefaultContext;
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use EyreHandler as EyreContext;
|
pub use EyreHandler as EyreContext;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use Report as ErrReport;
|
pub use Report as ErrReport;
|
||||||
/// Compatibility re-export of `Report` for interop with `anyhow`
|
/// Compatibility re-export of `Report` for interop with `anyhow`
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
pub use Report as Error;
|
pub use Report as Error;
|
||||||
/// Compatibility re-export of `WrapErr` for interop with `anyhow`
|
/// Compatibility re-export of `WrapErr` for interop with `anyhow`
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
pub use WrapErr as Context;
|
pub use WrapErr as Context;
|
||||||
|
|
||||||
/// The core error reporting type of the library, a wrapper around a dynamic error reporting type.
|
/// The core error reporting type of the library, a wrapper around a dynamic error reporting type.
|
||||||
@ -1112,12 +1127,14 @@ pub trait WrapErr<T, E>: context::private::Sealed {
|
|||||||
F: FnOnce() -> D;
|
F: FnOnce() -> D;
|
||||||
|
|
||||||
/// Compatibility re-export of wrap_err for interop with `anyhow`
|
/// Compatibility re-export of wrap_err for interop with `anyhow`
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[cfg_attr(track_caller, track_caller)]
|
#[cfg_attr(track_caller, track_caller)]
|
||||||
fn context<D>(self, msg: D) -> Result<T, Report>
|
fn context<D>(self, msg: D) -> Result<T, Report>
|
||||||
where
|
where
|
||||||
D: Display + Send + Sync + 'static;
|
D: Display + Send + Sync + 'static;
|
||||||
|
|
||||||
/// Compatibility re-export of wrap_err_with for interop with `anyhow`
|
/// Compatibility re-export of wrap_err_with for interop with `anyhow`
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[cfg_attr(track_caller, track_caller)]
|
#[cfg_attr(track_caller, track_caller)]
|
||||||
fn with_context<D, F>(self, f: F) -> Result<T, Report>
|
fn with_context<D, F>(self, f: F) -> Result<T, Report>
|
||||||
where
|
where
|
||||||
@ -1223,6 +1240,7 @@ pub trait OptionExt<T>: context::private::Sealed {
|
|||||||
/// .ok_or_else(|| eyre!("the thing wasnt in the list"))
|
/// .ok_or_else(|| eyre!("the thing wasnt in the list"))
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
pub trait ContextCompat<T>: context::private::Sealed {
|
pub trait ContextCompat<T>: context::private::Sealed {
|
||||||
/// Compatibility version of `wrap_err` for creating new errors with new source on `Option`
|
/// Compatibility version of `wrap_err` for creating new errors with new source on `Option`
|
||||||
/// when porting from `anyhow`
|
/// when porting from `anyhow`
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#![cfg(feature = "anyhow")]
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
use crate::common::maybe_install_handler;
|
use crate::common::maybe_install_handler;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::panic::Location;
|
use std::panic::Location;
|
||||||
|
|
||||||
use eyre::{OptionExt as _, WrapErr};
|
use eyre::WrapErr;
|
||||||
|
|
||||||
struct LocationHandler {
|
struct LocationHandler {
|
||||||
actual: Option<&'static str>,
|
actual: Option<&'static str>,
|
||||||
@ -83,6 +83,7 @@ fn test_wrap_err_with() {
|
|||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_option_ok_or_eyre() {
|
fn test_option_ok_or_eyre() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
@ -90,12 +91,14 @@ fn test_option_ok_or_eyre() {
|
|||||||
Box::new(LocationHandler::new(expected_location))
|
Box::new(LocationHandler::new(expected_location))
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
use eyre::OptionExt;
|
||||||
let err = None::<()>.ok_or_eyre("oopsie").unwrap_err();
|
let err = None::<()>.ok_or_eyre("oopsie").unwrap_err();
|
||||||
|
|
||||||
// should panic if the location isn't in our crate
|
// should panic if the location isn't in our crate
|
||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_context() {
|
fn test_context() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
@ -111,6 +114,7 @@ fn test_context() {
|
|||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_with_context() {
|
fn test_with_context() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
@ -126,6 +130,7 @@ fn test_with_context() {
|
|||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_option_compat_wrap_err() {
|
fn test_option_compat_wrap_err() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
@ -140,6 +145,7 @@ fn test_option_compat_wrap_err() {
|
|||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_option_compat_wrap_err_with() {
|
fn test_option_compat_wrap_err_with() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
@ -154,6 +160,7 @@ fn test_option_compat_wrap_err_with() {
|
|||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_option_compat_context() {
|
fn test_option_compat_context() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
@ -168,6 +175,7 @@ fn test_option_compat_context() {
|
|||||||
println!("{:?}", err);
|
println!("{:?}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_option_compat_with_context() {
|
fn test_option_compat_with_context() {
|
||||||
let _ = eyre::set_hook(Box::new(|_e| {
|
let _ = eyre::set_hook(Box::new(|_e| {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user