Skip to content
Myr

Preserve type of non-&str literal passed to format_err

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 9da23ae · Browse files · 2 files+8 added−6 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+8 added−6 removed

  • other:+8 added−6 removed

2 files+8 added−6 removed

modified:src/lib.rs+7 added-5 removed
604 unmodified lines
605#[doc(hidden)]606pub mod private {607 use crate::Error;608 use alloc::fmt;609 use core::fmt::Arguments;609 use core::fmt::{Arguments, Debug, Display};610611 pub use alloc::format;612 pub use core::result::Result::Err;613 pub use core::{concat, format_args, stringify};
6 unmodified lines
620 pub use crate::kind::BoxedKind;621 }622623 #[doc(hidden)]624 #[inline]624 #[cold]626 pub fn format_err(args: Arguments) -> Error {627 if let Some(msg) = args.as_str() {625 pub fn format_err<M>(message: M, args: Arguments) -> Error626 where627 M: Display + Debug + Send + Sync + 'static,628 {629 if args.as_str().is_some() {630 // anyhow!("literal"), can downcast to &'static str629 Error::msg(msg)631 Error::msg(message)632 } else {633 // anyhow!("interpolate {var}"), can downcast to String634 Error::msg(fmt::format(args))635 }
2 unmodified lines
modified:src/macros.rs+1 added-1 removed
166 unmodified lines
167/// ```168#[macro_export]169macro_rules! anyhow {170 ($msg:literal $(,)?) => {171 $crate::private::format_err($crate::private::format_args!($crate::private::concat!($msg)))171 $crate::private::format_err($msg, $crate::private::format_args!($crate::private::concat!($msg)))172 };173 ($err:expr $(,)?) => ({174 use $crate::private::kind::*;175 match $err {
7 unmodified lines