pub fn drop_write_stdout(bytes: &[u8]) {
use std::io::Write as _;
let _ = std::io::stdout().write_all(bytes);
}
#[macro_export]
macro_rules! drop_print {
($($arg:tt)+) => {{
use ::std::io::Write as _;
let _ = ::std::write!(::std::io::stdout(), $($arg)+);
}};
}
#[macro_export]
macro_rules! drop_println {
() => {{
use ::std::io::Write as _;
let _ = ::std::writeln!(::std::io::stdout());
}};
($($arg:tt)+) => {{
use ::std::io::Write as _;
let _ = ::std::writeln!(::std::io::stdout(), $($arg)+);
}};
}
#[macro_export]
macro_rules! drop_eprint {
($($arg:tt)+) => {{
use ::std::io::Write as _;
let _ = ::std::write!(::std::io::stderr(), $($arg)+);
}};
}
#[macro_export]
macro_rules! drop_eprintln {
() => {{
use ::std::io::Write as _;
let _ = ::std::writeln!(::std::io::stderr());
}};
($($arg:tt)+) => {{
use ::std::io::Write as _;
let _ = ::std::writeln!(::std::io::stderr(), $($arg)+);
}};
}
#[cfg(test)]
mod tests {
#[test]
fn all_macro_forms_expand() {
let value = 42;
drop_print!("positional {} and named {value}", "arg");
drop_println!();
drop_println!("positional {} and named {value}", "arg");
drop_println!("trailing comma {},", value,);
drop_eprint!("positional {} and named {value}", "arg");
drop_eprintln!();
drop_eprintln!("positional {} and named {value}", "arg");
}
#[test]
#[allow(
clippy::disallowed_methods,
reason = "test spawns itself as a child process"
)]
fn does_not_panic_on_closed_stdout() {
if std::env::var_os("DENO_PRINT_TEST_CHILD").is_some() {
for i in 0..1_000_000 {
drop_println!("line {}", i);
}
std::process::exit(0);
}
let exe = std::env::current_exe().unwrap();
let mut child = std::process::Command::new(exe)
.arg("tests::does_not_panic_on_closed_stdout")
.arg("--exact")
.env("DENO_PRINT_TEST_CHILD", "1")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null())
.spawn()
.unwrap();
{
use std::io::Read as _;
let mut stdout = child.stdout.take().unwrap();
let mut buf = [0u8; 100];
let _ = stdout.read_exact(&mut buf);
}
let status = child.wait().unwrap();
assert!(status.success(), "child exited with {:?}", status);
}
}