From 7428386da32c2a78b80f7e67a04b5fd5449601d5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 17 Jan 2024 17:07:13 -0600 Subject: [PATCH] refactor(fmt): Pull out stream lookup from write --- src/fmt/writer/mod.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/fmt/writer/mod.rs b/src/fmt/writer/mod.rs index 8dafd26..52265d4 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -67,15 +67,20 @@ impl WritableTarget { let buf = buf.as_bytes(); match self { WritableTarget::WriteStdout => { - write!(std::io::stdout(), "{}", String::from_utf8_lossy(buf))? + let mut stream = std::io::stdout().lock(); + write!(stream, "{}", String::from_utf8_lossy(buf))?; } WritableTarget::PrintStdout => print!("{}", String::from_utf8_lossy(buf)), WritableTarget::WriteStderr => { - write!(std::io::stderr(), "{}", String::from_utf8_lossy(buf))? + let mut stream = std::io::stderr().lock(); + write!(stream, "{}", String::from_utf8_lossy(buf))?; } WritableTarget::PrintStderr => eprint!("{}", String::from_utf8_lossy(buf)), // Safety: If the target type is `Pipe`, `target_pipe` will always be non-empty. - WritableTarget::Pipe(pipe) => pipe.lock().unwrap().write_all(buf)?, + WritableTarget::Pipe(pipe) => { + let mut stream = pipe.lock().unwrap(); + write!(stream, "{}", String::from_utf8_lossy(buf))?; + } } Ok(())