Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stream): Respect 'test' feature #229

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(stream): Pull out test macro body
Share more code and reduce binary bloat
epage committed Nov 4, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
commit 69cf6c9909f6dde773507941579b2e73d539fbb7
83 changes: 28 additions & 55 deletions crates/anstream/src/_macros.rs
Original file line number Diff line number Diff line change
@@ -61,17 +61,8 @@
macro_rules! print {
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
use std::io::Write as _;

let stdio = std::io::stdout();
let choice = $crate::AutoStream::choice(&stdio);
let buffer = Vec::new();
let mut stream = $crate::AutoStream::new(buffer, choice);
// Ignore errors rather than panic
let _ = ::std::write!(&mut stream, $($arg)*);
let buffer = stream.into_inner();
// Should be UTF-8 but not wanting to panic
let buffer = String::from_utf8_lossy(&buffer);
let target_stream = std::io::stdout();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::print!("{}", buffer)
} else {
use std::io::Write as _;
@@ -142,17 +133,8 @@ macro_rules! println {
};
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
use std::io::Write as _;

let stdio = std::io::stdout();
let choice = $crate::AutoStream::choice(&stdio);
let buffer = Vec::new();
let mut stream = $crate::AutoStream::new(buffer, choice);
// Ignore errors rather than panic
let _ = ::std::write!(&mut stream, $($arg)*);
let buffer = stream.into_inner();
// Should be UTF-8 but not wanting to panic
let buffer = String::from_utf8_lossy(&buffer);
let target_stream = std::io::stdout();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::println!("{}", buffer)
} else {
use std::io::Write as _;
@@ -202,17 +184,8 @@ macro_rules! println {
macro_rules! eprint {
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
use std::io::Write as _;

let stdio = std::io::stderr();
let choice = $crate::AutoStream::choice(&stdio);
let buffer = Vec::new();
let mut stream = $crate::AutoStream::new(buffer, choice);
// Ignore errors rather than panic
let _ = ::std::write!(&mut stream, $($arg)*);
let buffer = stream.into_inner();
// Should be UTF-8 but not wanting to panic
let buffer = String::from_utf8_lossy(&buffer);
let target_stream = std::io::stderr();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::eprint!("{}", buffer)
} else {
use std::io::Write as _;
@@ -265,17 +238,8 @@ macro_rules! eprintln {
};
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
use std::io::Write as _;

let stdio = std::io::stderr();
let choice = $crate::AutoStream::choice(&stdio);
let buffer = Vec::new();
let mut stream = $crate::AutoStream::new(buffer, choice);
// Ignore errors rather than panic
let _ = ::std::write!(&mut stream, $($arg)*);
let buffer = stream.into_inner();
// Should be UTF-8 but not wanting to panic
let buffer = String::from_utf8_lossy(&buffer);
let target_stream = std::io::stderr();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::eprintln!("{}", buffer)
} else {
use std::io::Write as _;
@@ -373,17 +337,26 @@ macro_rules! panic {
::std::panic!()
};
($($arg:tt)*) => {{
use std::io::Write as _;

let panic_stream = std::io::stderr();
let choice = $crate::AutoStream::choice(&panic_stream);
let buffer = Vec::new();
let mut stream = $crate::AutoStream::new(buffer, choice);
// Ignore errors rather than panic
let _ = ::std::write!(&mut stream, $($arg)*);
let buffer = stream.into_inner();
// Should be UTF-8 but not wanting to panic
let buffer = String::from_utf8_lossy(&buffer).into_owned();
let target_stream = std::io::stderr();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::panic!("{}", buffer)
}};
}

#[cfg(feature = "auto")]
pub fn to_adapted_string(
display: &dyn std::fmt::Display,
stream: &impl crate::stream::RawStream,
) -> String {
use std::io::Write as _;

let choice = crate::AutoStream::choice(stream);
let buffer = Vec::new();
let mut stream = crate::AutoStream::new(buffer, choice);
// Ignore errors rather than panic
let _ = ::std::write!(&mut stream, "{display}");
let buffer = stream.into_inner();
// Should be UTF-8 but not wanting to panic
let buffer = String::from_utf8_lossy(&buffer).into_owned();
buffer
}
7 changes: 4 additions & 3 deletions crates/anstream/src/lib.rs
Original file line number Diff line number Diff line change
@@ -38,11 +38,12 @@

pub mod adapter;
pub mod stream;

mod buffer;
#[doc(hidden)]
#[macro_use]
mod _macros;
pub mod _macros;

mod auto;
mod buffer;
mod fmt;
mod strip;
#[cfg(all(windows, feature = "wincon"))]