Skip to content
Myr

Move expansion of anyhow with literal into an inline fn

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent 93fae17 · Browse files · 2 files+19 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+19 added−15 removed

  • other:+19 added−15 removed

2 files+19 added−15 removed

modified:src/lib.rs+18 added-4 removed
602 unmodified lines
603604// Not public API. Referenced by macro-generated code.605#[doc(hidden)]606pub mod private {607 pub use alloc::fmt::format; // the function608 pub use alloc::format; // the macro609 pub use core::fmt::Arguments;610 pub use core::option::Option::Some;607 use crate::Error;608 use alloc::fmt;609 use core::fmt::Arguments;610611 pub use alloc::format;612 pub use core::result::Result::Err;613 pub use core::{concat, format_args, stringify};614615 #[doc(hidden)]
2 unmodified lines
618619 #[cfg(feature = "std")]620 pub use crate::kind::BoxedKind;621 }622623 #[doc(hidden)]624 #[inline]625 #[cold]626 pub fn format_err(args: Arguments) -> Error {627 if let Some(msg) = args.as_str() {628 // anyhow!("literal"), can downcast to &'static str629 Error::msg(msg)630 } else {631 // anyhow!("interpolate {var}"), can downcast to String632 Error::msg(fmt::format(args))633 }634 }635}
modified:src/macros.rs+1 added-11 removed
166 unmodified lines
167/// ```168#[macro_export]169macro_rules! anyhow {170 ($msg:literal $(,)?) => {171 match $crate::private::format_args!($crate::private::concat!($msg)) {172 arguments => {173 if let $crate::private::Some(msg) = $crate::private::Arguments::as_str(&arguments) {174 // anyhow!("literal"), can downcast to &'static str175 $crate::Error::msg(msg)176 } else {177 // anyhow!("interpolate {var}"), can downcast to String178 $crate::Error::msg($crate::private::format(arguments))179 }180 }181 }171 $crate::private::format_err($crate::private::format_args!($crate::private::concat!($msg)))172 };173 ($err:expr $(,)?) => ({174 use $crate::private::kind::*;175 match $err {
7 unmodified lines