Skip to content

Commit

Permalink
feat: introduce an "anyhow" compatibility layer feature flag (#138)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
LeoniePhiline and ten3roberts committed Jan 16, 2024
1 parent d6c0b8d commit 34bd1d9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 6 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`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
implement `WrapErr` for `Option`. This decision was made because `wrap_err`
Expand Down
2 changes: 2 additions & 0 deletions eyre/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- next-header -->

## [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
### Fixed
Expand Down
3 changes: 2 additions & 1 deletion eyre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ readme = { workspace = true }
rust-version = { workspace = true }

[features]
default = ["auto-install", "track-caller"]
default = ["anyhow", "auto-install", "track-caller"]
anyhow = []
auto-install = []
track-caller = []

Expand Down
7 changes: 5 additions & 2 deletions eyre/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::{ContextError, ErrorImpl};
use crate::{ContextCompat, Report, StdError, WrapErr};
use crate::{Report, StdError, WrapErr};
use core::fmt::{self, Debug, Display, Write};

#[cfg(backtrace)]
Expand Down Expand Up @@ -62,13 +62,15 @@ where
}
}

#[cfg(feature = "anyhow")]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
{
self.wrap_err(msg)
}

#[cfg(feature = "anyhow")]
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
Expand All @@ -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>
where
D: Display + Send + Sync + 'static,
Expand Down
1 change: 1 addition & 0 deletions eyre/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl Report {
unsafe { Report::construct(error, vtable, handler) }
}

#[cfg(feature = "anyhow")]
#[cfg_attr(track_caller, track_caller)]
pub(crate) fn from_display<M>(message: M) -> Self
where
Expand Down
20 changes: 19 additions & 1 deletion eyre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,17 @@
//! vice-versa by re-exporting all of the renamed APIs with the names used in
//! `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
//! implement `WrapErr` for `Option`. This decision was made because `wrap_err`
Expand Down Expand Up @@ -375,18 +385,23 @@ use std::error::Error as StdError;

pub use eyre as format_err;
/// Compatibility re-export of `eyre` for interop with `anyhow`
#[cfg(feature = "anyhow")]
pub use eyre as anyhow;
use once_cell::sync::OnceCell;
use ptr::OwnedPtr;
#[cfg(feature = "anyhow")]
#[doc(hidden)]
pub use DefaultHandler as DefaultContext;
#[cfg(feature = "anyhow")]
#[doc(hidden)]
pub use EyreHandler as EyreContext;
#[doc(hidden)]
pub use Report as ErrReport;
/// Compatibility re-export of `Report` for interop with `anyhow`
#[cfg(feature = "anyhow")]
pub use Report as Error;
/// Compatibility re-export of `WrapErr` for interop with `anyhow`
#[cfg(feature = "anyhow")]
pub use WrapErr as Context;

/// The core error reporting type of the library, a wrapper around a dynamic error reporting type.
Expand Down Expand Up @@ -1112,12 +1127,14 @@ pub trait WrapErr<T, E>: context::private::Sealed {
F: FnOnce() -> D;

/// Compatibility re-export of wrap_err for interop with `anyhow`
#[cfg(feature = "anyhow")]
#[cfg_attr(track_caller, track_caller)]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static;

/// Compatibility re-export of wrap_err_with for interop with `anyhow`
#[cfg(feature = "anyhow")]
#[cfg_attr(track_caller, track_caller)]
fn with_context<D, F>(self, f: F) -> Result<T, Report>
where
Expand Down Expand Up @@ -1223,6 +1240,7 @@ pub trait OptionExt<T>: context::private::Sealed {
/// .ok_or_else(|| eyre!("the thing wasnt in the list"))
/// }
/// ```
#[cfg(feature = "anyhow")]
pub trait ContextCompat<T>: context::private::Sealed {
/// Compatibility version of `wrap_err` for creating new errors with new source on `Option`
/// when porting from `anyhow`
Expand Down
2 changes: 2 additions & 0 deletions eyre/tests/test_context_access.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "anyhow")]

mod common;

use crate::common::maybe_install_handler;
Expand Down
10 changes: 9 additions & 1 deletion eyre/tests/test_location.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::panic::Location;

use eyre::{OptionExt as _, WrapErr};
use eyre::WrapErr;

struct LocationHandler {
actual: Option<&'static str>,
Expand Down Expand Up @@ -83,19 +83,22 @@ fn test_wrap_err_with() {
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
#[test]
fn test_option_ok_or_eyre() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));

use eyre::OptionExt;
let err = None::<()>.ok_or_eyre("oopsie").unwrap_err();

// should panic if the location isn't in our crate
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
#[test]
fn test_context() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand All @@ -111,6 +114,7 @@ fn test_context() {
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
#[test]
fn test_with_context() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand All @@ -126,6 +130,7 @@ fn test_with_context() {
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
#[test]
fn test_option_compat_wrap_err() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand All @@ -140,6 +145,7 @@ fn test_option_compat_wrap_err() {
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
#[test]
fn test_option_compat_wrap_err_with() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand All @@ -154,6 +160,7 @@ fn test_option_compat_wrap_err_with() {
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
#[test]
fn test_option_compat_context() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand All @@ -168,6 +175,7 @@ fn test_option_compat_context() {
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
#[test]
fn test_option_compat_with_context() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand Down

0 comments on commit 34bd1d9

Please sign in to comment.