From 0119f3286fc69a8e9c8e866d8c7b3c19bfdb493f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 4 Nov 2024 11:45:45 -0600 Subject: [PATCH] fix(stream): Respect 'test' feature We were putting it into the generated code, not our code, causing it to be an "unexpected cfg". It would be nice to use the macro's cfgs in the generated code... --- crates/anstream/src/_macros.rs | 66 +++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/crates/anstream/src/_macros.rs b/crates/anstream/src/_macros.rs index b997c4c..705143a 100644 --- a/crates/anstream/src/_macros.rs +++ b/crates/anstream/src/_macros.rs @@ -1,9 +1,22 @@ #[doc = include_str!("print.md")] #[cfg(feature = "auto")] +#[cfg(feature = "test")] #[macro_export] macro_rules! print { ($($arg:tt)*) => {{ - if cfg!(any(feature = "test", test)) { + let target_stream = std::io::stdout(); + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); + ::std::print!("{}", buffer) + }}; +} + +#[doc = include_str!("print.md")] +#[cfg(feature = "auto")] +#[cfg(not(feature = "test"))] +#[macro_export] +macro_rules! print { + ($($arg:tt)*) => {{ + if cfg!(test) { let target_stream = std::io::stdout(); let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); ::std::print!("{}", buffer) @@ -23,13 +36,29 @@ macro_rules! print { #[doc = include_str!("println.md")] #[cfg(feature = "auto")] +#[cfg(feature = "test")] +#[macro_export] +macro_rules! println { + () => { + $crate::print!("\n") + }; + ($($arg:tt)*) => {{ + let target_stream = std::io::stdout(); + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); + ::std::println!("{}", buffer) + }}; +} + +#[doc = include_str!("println.md")] +#[cfg(feature = "auto")] +#[cfg(not(feature = "test"))] #[macro_export] macro_rules! println { () => { $crate::print!("\n") }; ($($arg:tt)*) => {{ - if cfg!(any(feature = "test", test)) { + if cfg!(test) { let target_stream = std::io::stdout(); let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); ::std::println!("{}", buffer) @@ -49,10 +78,23 @@ macro_rules! println { #[doc = include_str!("eprint.md")] #[cfg(feature = "auto")] +#[cfg(feature = "test")] +#[macro_export] +macro_rules! eprint { + ($($arg:tt)*) => {{ + let target_stream = std::io::stderr(); + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); + ::std::eprint!("{}", buffer) + }}; +} + +#[doc = include_str!("eprint.md")] +#[cfg(feature = "auto")] +#[cfg(not(feature = "test"))] #[macro_export] macro_rules! eprint { ($($arg:tt)*) => {{ - if cfg!(any(feature = "test", test)) { + if cfg!(test) { let target_stream = std::io::stderr(); let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); ::std::eprint!("{}", buffer) @@ -72,13 +114,29 @@ macro_rules! eprint { #[doc = include_str!("eprintln.md")] #[cfg(feature = "auto")] +#[cfg(feature = "test")] +#[macro_export] +macro_rules! eprintln { + () => { + $crate::eprint!("\n") + }; + ($($arg:tt)*) => {{ + let target_stream = std::io::stderr(); + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); + ::std::eprintln!("{}", buffer) + }}; +} + +#[doc = include_str!("eprintln.md")] +#[cfg(feature = "auto")] +#[cfg(not(feature = "test"))] #[macro_export] macro_rules! eprintln { () => { $crate::eprint!("\n") }; ($($arg:tt)*) => {{ - if cfg!(any(feature = "test", test)) { + if cfg!(test) { let target_stream = std::io::stderr(); let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); ::std::eprintln!("{}", buffer)