Skip to content

Commit

Permalink
refactor(fmt): Pull out stream lookup from write
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 17, 2024
1 parent e8674a2 commit 7428386
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit 7428386

Please sign in to comment.