Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: introduce an "anyhow" compatibility layer feature flag #138

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.10] - 2023-12-07
### 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")]
LeoniePhiline marked this conversation as resolved.
Show resolved Hide resolved
#[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
LeoniePhiline marked this conversation as resolved.
Show resolved Hide resolved
//!
//! 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
6 changes: 6 additions & 0 deletions eyre/tests/test_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fn test_wrap_err_with() {
println!("{:?}", err);
}

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

#[cfg(feature = "anyhow")]
#[test]
fn test_with_context() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand All @@ -113,6 +115,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 @@ -127,6 +130,7 @@ fn test_option_compat_wrap_err() {
println!("{:?}", err);
}

#[cfg(feature = "anyhow")]
ten3roberts marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn test_option_compat_wrap_err_with() {
let _ = eyre::set_hook(Box::new(|_e| {
Expand All @@ -141,6 +145,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 @@ -155,6 +160,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
Loading