diff --git a/src/ensure.rs b/src/ensure.rs index 96a810a..80bdab7 100644 --- a/src/ensure.rs +++ b/src/ensure.rs @@ -818,17 +818,17 @@ macro_rules! __fallback_ensure { }; ($cond:expr, $msg:literal $(,)?) => { if !$cond { - return $crate::private::Err($crate::anyhow!($msg)); + return $crate::private::Err($crate::__anyhow!($msg)); } }; ($cond:expr, $err:expr $(,)?) => { if !$cond { - return $crate::private::Err($crate::anyhow!($err)); + return $crate::private::Err($crate::__anyhow!($err)); } }; ($cond:expr, $fmt:expr, $($arg:tt)*) => { if !$cond { - return $crate::private::Err($crate::anyhow!($fmt, $($arg)*)); + return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*)); } }; } diff --git a/src/macros.rs b/src/macros.rs index e92a264..bbd61d4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -54,13 +54,13 @@ #[macro_export] macro_rules! bail { ($msg:literal $(,)?) => { - return $crate::private::Err($crate::anyhow!($msg)) + return $crate::private::Err($crate::__anyhow!($msg)) }; ($err:expr $(,)?) => { - return $crate::private::Err($crate::anyhow!($err)) + return $crate::private::Err($crate::__anyhow!($err)) }; ($fmt:expr, $($arg:tt)*) => { - return $crate::private::Err($crate::anyhow!($fmt, $($arg)*)) + return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*)) }; } @@ -75,13 +75,13 @@ macro_rules! bail { return $crate::private::Err($crate::Error::msg("pattern does not contain `{}`")) }; ($msg:literal $(,)?) => { - return $crate::private::Err($crate::anyhow!($msg)) + return $crate::private::Err($crate::__anyhow!($msg)) }; ($err:expr $(,)?) => { - return $crate::private::Err($crate::anyhow!($err)) + return $crate::private::Err($crate::__anyhow!($err)) }; ($fmt:expr, $($arg:tt)*) => { - return $crate::private::Err($crate::anyhow!($fmt, $($arg)*)) + return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*)) }; } @@ -145,17 +145,17 @@ macro_rules! ensure { }; ($cond:expr, $msg:literal $(,)?) => { if !$cond { - return $crate::private::Err($crate::anyhow!($msg)); + return $crate::private::Err($crate::__anyhow!($msg)); } }; ($cond:expr, $err:expr $(,)?) => { if !$cond { - return $crate::private::Err($crate::anyhow!($err)); + return $crate::private::Err($crate::__anyhow!($err)); } }; ($cond:expr, $fmt:expr, $($arg:tt)*) => { if !$cond { - return $crate::private::Err($crate::anyhow!($fmt, $($arg)*)); + return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*)); } }; } @@ -225,3 +225,25 @@ macro_rules! anyhow { $crate::Error::msg($crate::private::format!($fmt, $($arg)*)) }; } + +// Not public API. This is used in the implementation of some of the other +// macros, in which the must_use call is not needed because the value is known +// to be used. +#[doc(hidden)] +#[macro_export] +macro_rules! __anyhow { + ($msg:literal $(,)?) => ({ + let error = $crate::private::format_err($crate::private::format_args!($msg)); + error + }); + ($err:expr $(,)?) => ({ + use $crate::private::kind::*; + let error = match $err { + error => (&error).anyhow_kind().new(error), + }; + error + }); + ($fmt:expr, $($arg:tt)*) => { + $crate::Error::msg($crate::private::format!($fmt, $($arg)*)) + }; +}