Skip to content

Commit fa7cd69

Browse files
committed
process: discard unnecessary layer of synchronization
1 parent f4e4025 commit fa7cd69

File tree

1 file changed

+8
-37
lines changed

1 file changed

+8
-37
lines changed

src/process/terminal_source.rs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use indicatif::TermLike;
33
use std::{
44
io::{self, Write},
55
num::NonZero,
6-
ops::DerefMut,
7-
sync::{Arc, Mutex},
86
};
97

108
#[cfg(feature = "test")]
@@ -17,13 +15,7 @@ use super::file_source::TestWriter;
1715

1816
/// A colorable terminal that can be written to
1917
pub struct ColorableTerminal {
20-
// TermColor uses a lifetime on locked variants, but the API we want to
21-
// emulate from std::io uses a static lifetime for locked variants: so we
22-
// emulate it. For Test workloads this results in a double-layering of
23-
// Arc<Mutex<...> which isn't great, but OTOH it is test code. Locking the
24-
// source is important because otherwise parallel constructed terminals
25-
// would not be locked out.
26-
inner: Arc<Mutex<TerminalInner>>,
18+
inner: TerminalInner,
2719
is_a_tty: bool,
2820
color_choice: ColorChoice,
2921
width: Option<NonZero<u16>>,
@@ -73,20 +65,15 @@ impl ColorableTerminal {
7365
.ok()
7466
.and_then(|s| s.parse::<NonZero<u16>>().ok());
7567
ColorableTerminal {
76-
inner: Arc::new(Mutex::new(inner)),
68+
inner,
7769
is_a_tty,
7870
color_choice: choice,
7971
width,
8072
}
8173
}
8274

8375
pub fn lock(&self) -> ColorableTerminalLocked {
84-
let locked = match self.inner.lock() {
85-
Ok(l) => l,
86-
Err(e) => e.into_inner(),
87-
};
88-
89-
match &*locked {
76+
match &self.inner {
9077
TerminalInner::Stdout(s) => ColorableTerminalLocked::Stdout(AutoStream::new(
9178
s.as_inner().lock(),
9279
self.color_choice,
@@ -185,28 +172,23 @@ impl TermLike for ColorableTerminal {
185172

186173
impl io::Write for ColorableTerminal {
187174
fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, io::Error> {
188-
let mut locked = self.inner.lock().unwrap();
189-
locked.deref_mut().as_write().write(buf)
175+
self.lock().as_write().write(buf)
190176
}
191177

192178
fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
193-
let mut locked = self.inner.lock().unwrap();
194-
locked.deref_mut().as_write().write_vectored(bufs)
179+
self.lock().as_write().write_vectored(bufs)
195180
}
196181

197182
fn flush(&mut self) -> std::result::Result<(), io::Error> {
198-
let mut locked = self.inner.lock().unwrap();
199-
locked.deref_mut().as_write().flush()
183+
self.lock().as_write().flush()
200184
}
201185

202186
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
203-
let mut locked = self.inner.lock().unwrap();
204-
locked.deref_mut().as_write().write_all(buf)
187+
self.lock().as_write().write_all(buf)
205188
}
206189

207190
fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::io::Result<()> {
208-
let mut locked = self.inner.lock().unwrap();
209-
locked.deref_mut().as_write().write_fmt(args)
191+
self.lock().as_write().write_fmt(args)
210192
}
211193
}
212194

@@ -264,17 +246,6 @@ enum TerminalInner {
264246
TestWriter(TestWriter),
265247
}
266248

267-
impl TerminalInner {
268-
fn as_write(&mut self) -> &mut dyn io::Write {
269-
match self {
270-
TerminalInner::Stdout(s) => s,
271-
TerminalInner::Stderr(s) => s,
272-
#[cfg(feature = "test")]
273-
TerminalInner::TestWriter(w) => w,
274-
}
275-
}
276-
}
277-
278249
/// Select what stream to make a terminal on
279250
pub(super) enum StreamSelector {
280251
Stdout,

0 commit comments

Comments
 (0)