Skip to content

Commit 061cf78

Browse files
epagedjc
authored andcommitted
fix(process): Ensure stdout/stderr lock is held across calls
While `std::io::Write` only has a few required methods, while implementing `anstream` I found its better to implement as many as possible when dealing with `stdout` or `stderr` so that the implicitly acquired lock is aquired once for the call, rather than multiple times as the inherent methods break down a single higher level calls into multiple lower level calls.
1 parent 32ef78a commit 061cf78

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/process/terminalsource.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,47 @@ impl io::Write for ColorableTerminal {
226226
locked.deref_mut().as_write().write(buf)
227227
}
228228

229+
fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
230+
let mut locked = self.inner.lock().unwrap();
231+
locked.deref_mut().as_write().write_vectored(bufs)
232+
}
233+
229234
fn flush(&mut self) -> std::result::Result<(), io::Error> {
230235
let mut locked = self.inner.lock().unwrap();
231236
locked.deref_mut().as_write().flush()
232237
}
238+
239+
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
240+
let mut locked = self.inner.lock().unwrap();
241+
locked.deref_mut().as_write().write_all(buf)
242+
}
243+
244+
fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::io::Result<()> {
245+
let mut locked = self.inner.lock().unwrap();
246+
locked.deref_mut().as_write().write_fmt(args)
247+
}
233248
}
234249

235250
impl io::Write for ColorableTerminalLocked {
236251
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
237252
self.locked.as_write().write(buf)
238253
}
239254

255+
fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
256+
self.locked.as_write().write_vectored(bufs)
257+
}
258+
240259
fn flush(&mut self) -> io::Result<()> {
241260
self.locked.as_write().flush()
242261
}
262+
263+
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
264+
self.locked.as_write().write_all(buf)
265+
}
266+
267+
fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::io::Result<()> {
268+
self.locked.as_write().write_fmt(args)
269+
}
243270
}
244271

245272
impl TermLike for ColorableTerminal {

0 commit comments

Comments
 (0)