Skip to content
Myr

Merge pull request #180 from dtolnay/capture

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

Support for format args capture

authored by David Tolnay · committed by GitHub · parents bc66b90, 6ae9937 · Browse files · 3 files+30 added−4 removed
This commit

State

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

Diff stats

3 files+30 added−4 removed

  • other:+30 added−4 removed

3 files+30 added−4 removed

modified:src/lib.rs+25 added-1 removed
602 unmodified lines
603604// Not public API. Referenced by macro-generated code.605#[doc(hidden)]606pub mod private {607 use crate::Error;608 use alloc::fmt;609 use core::fmt::{Arguments, Debug, Display};610611 pub use alloc::format;612 pub use core::result::Result::Err;609 pub use core::{concat, stringify};613 pub use core::{concat, format_args, stringify};614615 #[doc(hidden)]616 pub mod kind {617 pub use crate::kind::{AdhocKind, TraitKind};618619 #[cfg(feature = "std")]620 pub use crate::kind::BoxedKind;621 }622623 #[doc(hidden)]624 #[cold]625 pub fn format_err<M>(message: M, args: Arguments) -> Error626 where627 M: Display + Debug + Send + Sync + 'static,628 {629 #[cfg(anyhow_no_fmt_arguments_as_str)]630 let has_interpolated_format_args = false;631 #[cfg(not(anyhow_no_fmt_arguments_as_str))]632 let has_interpolated_format_args = args.as_str().is_none();633634 if has_interpolated_format_args {635 // anyhow!("interpolate {var}"), can downcast to String636 Error::msg(fmt::format(args))637 } else {638 // anyhow!("literal"), can downcast to &'static str639 Error::msg(message)640 }641 }642}
modified:src/macros.rs+1 added-3 removed
166 unmodified lines
167/// ```168#[macro_export]169macro_rules! anyhow {170 ($msg:literal $(,)?) => {171 // Handle $:literal as a special case to make cargo-expanded code more172 // concise in the common case.173 $crate::Error::msg($msg)171 $crate::private::format_err($msg, $crate::private::format_args!($msg))172 };173 ($err:expr $(,)?) => ({174 use $crate::private::kind::*;175 match $err {
7 unmodified lines
modified:build.rs+4 added
56 unmodified lines
5758 if rustc < 51 {59 println!("cargo:rustc-cfg=anyhow_no_ptr_addr_of");60 }6162 if rustc < 52 {63 println!("cargo:rustc-cfg=anyhow_no_fmt_arguments_as_str");64 }65}6667fn compile_probe() -> Option<ExitStatus> {68 let rustc = env::var_os("RUSTC")?;
45 unmodified lines