Skip to content

Commit

Permalink
Make ContextCompat consistent and non-ambiguous with WrapErr (#150)
Browse files Browse the repository at this point in the history
See: #149

Fixes: #149
  • Loading branch information
ten3roberts authored Jun 28, 2024
2 parents 907daa2 + c98bc8c commit 9498677
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 83 deletions.
7 changes: 6 additions & 1 deletion color-eyre/tests/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ fn test_backwards_compatibility(target: String, file_name: &str) {
fn normalize_backtrace(input: &str) -> String {
input
.lines()
.take_while(|v| !v.contains("core::panic") && !v.contains("theme_test_helper::main"))
.take_while(|v| {
!v.contains("core::panic")
&& !v.contains("theme_test_helper::main")
&& !v.contains("theme::test_error_backwards_compatibility::closure")
&& !v.contains("theme::test_error_backwards_compatibility::{{closure}}")
})
.collect::<Vec<_>>()
.join("\n")
}
Expand Down
33 changes: 13 additions & 20 deletions eyre/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,34 @@ where
Err(e) => Err(e.ext_report(msg())),
}
}
}

#[cfg(feature = "anyhow")]
fn context<D>(self, msg: D) -> Result<T, Report>
#[cfg(feature = "anyhow")]
impl<T, E> crate::ContextCompat<T> for Result<T, E>
where
Self: WrapErr<T, E>,
{
#[track_caller]
fn context<D>(self, msg: D) -> crate::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>
#[track_caller]
fn with_context<D, F>(self, f: F) -> crate::Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.wrap_err_with(msg)
self.wrap_err_with(f)
}
}

#[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,
{
self.context(msg)
}

fn wrap_err_with<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D,
{
self.with_context(msg)
}

#[track_caller]
fn context<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
Expand All @@ -107,6 +99,7 @@ impl<T> crate::ContextCompat<T> for Option<T> {
}
}

#[track_caller]
fn with_context<D, F>(self, msg: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
Expand Down
34 changes: 5 additions & 29 deletions eyre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ fn capture_handler(error: &(dyn StdError + 'static)) -> Box<dyn EyreHandler> {
}

impl dyn EyreHandler {
/// Check if the handler is of type `T`
pub fn is<T: EyreHandler>(&self) -> bool {
// Get `TypeId` of the type this function is instantiated with.
let t = core::any::TypeId::of::<T>();
Expand All @@ -635,6 +636,7 @@ impl dyn EyreHandler {
t == concrete
}

/// Downcast the handler to a concrete type `T`
pub fn downcast_ref<T: EyreHandler>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe { Some(&*(self as *const dyn EyreHandler as *const T)) }
Expand All @@ -643,6 +645,7 @@ impl dyn EyreHandler {
}
}

/// Downcast the handler to a concrete type `T`
pub fn downcast_mut<T: EyreHandler>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut dyn EyreHandler as *mut T)) }
Expand Down Expand Up @@ -1122,21 +1125,6 @@ pub trait WrapErr<T, E>: context::private::Sealed {
where
D: Display + Send + Sync + 'static,
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
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
}

/// Provides the [`ok_or_eyre`][OptionExt::ok_or_eyre] method for [`Option`].
Expand Down Expand Up @@ -1194,7 +1182,8 @@ pub trait OptionExt<T>: context::private::Sealed {
M: Debug + Display + Send + Sync + 'static;
}

/// Provides the `context` method for `Option` when porting from `anyhow`
/// Provides the `context` and `with_context` methods for `Result` and `Option` to enhance
/// compatibility when porting from anyhow.
///
/// This trait is sealed and cannot be implemented for types outside of
/// `eyre`.
Expand Down Expand Up @@ -1253,19 +1242,6 @@ pub trait ContextCompat<T>: context::private::Sealed {
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;

/// Compatibility re-export of `context` for porting from `anyhow` to `eyre`
#[cfg_attr(track_caller, track_caller)]
fn wrap_err<D>(self, msg: D) -> Result<T, Report>
where
D: Display + Send + Sync + 'static;

/// Compatibility re-export of `with_context` for porting from `anyhow` to `eyre`
#[cfg_attr(track_caller, track_caller)]
fn wrap_err_with<D, F>(self, f: F) -> Result<T, Report>
where
D: Display + Send + Sync + 'static,
F: FnOnce() -> D;
}

/// Equivalent to `Ok::<_, eyre::Error>(value)`.
Expand Down
34 changes: 2 additions & 32 deletions eyre/tests/test_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn test_context() {
Box::new(LocationHandler::new(expected_location))
}));

use eyre::WrapErr;
use eyre::ContextCompat;
let err = read_path("totally_fake_path")
.context("oopsie")
.unwrap_err();
Expand All @@ -122,7 +122,7 @@ fn test_with_context() {
Box::new(LocationHandler::new(expected_location))
}));

use eyre::WrapErr;
use eyre::ContextCompat;
let err = read_path("totally_fake_path")
.with_context(|| "oopsie")
.unwrap_err();
Expand All @@ -131,36 +131,6 @@ fn test_with_context() {
println!("{:?}", err);
}

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

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

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

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

use eyre::ContextCompat;
let err = None::<()>.wrap_err_with(|| "oopsie").unwrap_err();

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

#[cfg(feature = "anyhow")]
#[test]
fn test_option_compat_context() {
Expand Down
1 change: 0 additions & 1 deletion eyre/tests/test_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod drop;
use self::common::maybe_install_handler;
use self::drop::{DetectDrop, Flag};
use eyre::Report;
use std::marker::Unpin;
use std::mem;

#[test]
Expand Down

0 comments on commit 9498677

Please sign in to comment.