From e47b6a90dee58f9194f64e527de5f24ab72ceceb Mon Sep 17 00:00:00 2001 From: rami3l Date: Thu, 16 May 2024 09:59:30 +0800 Subject: [PATCH] refactor(filesource): replace repetitive `#[cfg()]` usages with `cfg_if!{}` --- src/currentprocess/filesource.rs | 194 +++++++++++++++---------------- 1 file changed, 93 insertions(+), 101 deletions(-) diff --git a/src/currentprocess/filesource.rs b/src/currentprocess/filesource.rs index 9546d6d8e4..f2a36438ef 100644 --- a/src/currentprocess/filesource.rs +++ b/src/currentprocess/filesource.rs @@ -5,6 +5,7 @@ use std::{ sync::{Arc, Mutex, MutexGuard}, }; +use cfg_if::cfg_if; use enum_dispatch::enum_dispatch; use super::terminalsource::{ColorableTerminal, StreamSelector}; @@ -47,53 +48,49 @@ impl StdinSource for super::OSProcess { // ----------------------- test support for stdin ------------------ -#[cfg(feature = "test")] -struct TestStdinLock<'a> { - inner: MutexGuard<'a, Cursor>, -} +cfg_if! { + if #[cfg(feature = "test")] { + struct TestStdinLock<'a> { + inner: MutexGuard<'a, Cursor>, + } -#[cfg(feature = "test")] -impl StdinLock for TestStdinLock<'_> {} + impl StdinLock for TestStdinLock<'_> {} -#[cfg(feature = "test")] -impl Read for TestStdinLock<'_> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.inner.read(buf) - } -} + impl Read for TestStdinLock<'_> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.inner.read(buf) + } + } -#[cfg(feature = "test")] -impl BufRead for TestStdinLock<'_> { - fn fill_buf(&mut self) -> io::Result<&[u8]> { - self.inner.fill_buf() - } - fn consume(&mut self, n: usize) { - self.inner.consume(n) - } -} + impl BufRead for TestStdinLock<'_> { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + self.inner.fill_buf() + } + fn consume(&mut self, n: usize) { + self.inner.consume(n) + } + } -#[cfg(feature = "test")] -pub(crate) type TestStdinInner = Arc>>; + pub(crate) type TestStdinInner = Arc>>; -#[cfg(feature = "test")] -struct TestStdin(TestStdinInner); + struct TestStdin(TestStdinInner); -#[cfg(feature = "test")] -impl Stdin for TestStdin { - fn lock(&self) -> Box { - Box::new(TestStdinLock { - inner: self.0.lock().unwrap_or_else(|e| e.into_inner()), - }) - } - fn read_line(&self, buf: &mut String) -> Result { - self.lock().read_line(buf) - } -} + impl Stdin for TestStdin { + fn lock(&self) -> Box { + Box::new(TestStdinLock { + inner: self.0.lock().unwrap_or_else(|e| e.into_inner()), + }) + } + fn read_line(&self, buf: &mut String) -> Result { + self.lock().read_line(buf) + } + } -#[cfg(feature = "test")] -impl StdinSource for super::TestProcess { - fn stdin(&self) -> Box { - Box::new(TestStdin(self.stdin.clone())) + impl StdinSource for super::TestProcess { + fn stdin(&self) -> Box { + Box::new(TestStdin(self.stdin.clone())) + } + } } } @@ -189,79 +186,74 @@ impl StderrSource for super::OSProcess { // ----------------------- test support for writers ------------------ -#[cfg(feature = "test")] -pub(super) struct TestWriterLock<'a> { - inner: MutexGuard<'a, Vec>, -} +cfg_if! { + if #[cfg(feature = "test")] { + pub(super) struct TestWriterLock<'a> { + inner: MutexGuard<'a, Vec>, + } -#[cfg(feature = "test")] -impl WriterLock for TestWriterLock<'_> {} + impl WriterLock for TestWriterLock<'_> {} -#[cfg(feature = "test")] -impl Write for TestWriterLock<'_> { - fn write(&mut self, buf: &[u8]) -> Result { - self.inner.write(buf) - } + impl Write for TestWriterLock<'_> { + fn write(&mut self, buf: &[u8]) -> Result { + self.inner.write(buf) + } - fn flush(&mut self) -> Result<()> { - Ok(()) - } -} + fn flush(&mut self) -> Result<()> { + Ok(()) + } + } -#[cfg(feature = "test")] -pub(super) type TestWriterInner = Arc>>; -/// A thread-safe test file handle that pretends to be e.g. stdout. -#[derive(Clone, Default)] -#[cfg(feature = "test")] -pub(super) struct TestWriter(TestWriterInner); + pub(super) type TestWriterInner = Arc>>; -#[cfg(feature = "test")] -impl TestWriter { - pub(super) fn lock(&self) -> TestWriterLock<'_> { - // The stream can be locked even if a test thread panicked: its state - // will be ok - TestWriterLock { - inner: self.0.lock().unwrap_or_else(|e| e.into_inner()), + /// A thread-safe test file handle that pretends to be e.g. stdout. + #[derive(Clone, Default)] + pub(super) struct TestWriter(TestWriterInner); + + impl TestWriter { + pub(super) fn lock(&self) -> TestWriterLock<'_> { + // The stream can be locked even if a test thread panicked: its state + // will be ok + TestWriterLock { + inner: self.0.lock().unwrap_or_else(|e| e.into_inner()), + } + } } - } -} -#[cfg(feature = "test")] -impl Writer for TestWriter { - fn is_a_tty(&self) -> bool { - false - } + impl Writer for TestWriter { + fn is_a_tty(&self) -> bool { + false + } - fn lock(&self) -> Box { - Box::new(self.lock()) - } + fn lock(&self) -> Box { + Box::new(self.lock()) + } - fn terminal(&self) -> ColorableTerminal { - ColorableTerminal::new(StreamSelector::TestWriter(self.clone())) - } -} + fn terminal(&self) -> ColorableTerminal { + ColorableTerminal::new(StreamSelector::TestWriter(self.clone())) + } + } -#[cfg(feature = "test")] -impl Write for TestWriter { - fn write(&mut self, buf: &[u8]) -> Result { - self.lock().write(buf) - } + impl Write for TestWriter { + fn write(&mut self, buf: &[u8]) -> Result { + self.lock().write(buf) + } - fn flush(&mut self) -> Result<()> { - Ok(()) - } -} + fn flush(&mut self) -> Result<()> { + Ok(()) + } + } -#[cfg(feature = "test")] -impl StdoutSource for super::TestProcess { - fn stdout(&self) -> Box { - Box::new(TestWriter(self.stdout.clone())) - } -} + impl StdoutSource for super::TestProcess { + fn stdout(&self) -> Box { + Box::new(TestWriter(self.stdout.clone())) + } + } -#[cfg(feature = "test")] -impl StderrSource for super::TestProcess { - fn stderr(&self) -> Box { - Box::new(TestWriter(self.stderr.clone())) + impl StderrSource for super::TestProcess { + fn stderr(&self) -> Box { + Box::new(TestWriter(self.stderr.clone())) + } + } } }