From 7a47c1da87a878fcb62b65f673c73721ff521aca Mon Sep 17 00:00:00 2001 From: rami3l Date: Tue, 7 May 2024 11:19:34 +0800 Subject: [PATCH] fix(filesource): make some constructs only available via the `test` feature --- src/currentprocess/filesource.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/currentprocess/filesource.rs b/src/currentprocess/filesource.rs index f50e270ce3..9546d6d8e4 100644 --- a/src/currentprocess/filesource.rs +++ b/src/currentprocess/filesource.rs @@ -1,11 +1,14 @@ -use std::io::{self, BufRead, Cursor, Read, Result, Write}; -use std::sync::{Arc, Mutex, MutexGuard}; +use std::io::{self, BufRead, Read, Result, Write}; +#[cfg(feature = "test")] +use std::{ + io::Cursor, + sync::{Arc, Mutex, MutexGuard}, +}; use enum_dispatch::enum_dispatch; -use crate::currentprocess::process; - use super::terminalsource::{ColorableTerminal, StreamSelector}; +use crate::currentprocess::process; /// Stand-in for std::io::Stdin pub trait Stdin { @@ -44,18 +47,22 @@ impl StdinSource for super::OSProcess { // ----------------------- test support for stdin ------------------ +#[cfg(feature = "test")] struct TestStdinLock<'a> { inner: MutexGuard<'a, Cursor>, } +#[cfg(feature = "test")] impl StdinLock for TestStdinLock<'_> {} +#[cfg(feature = "test")] 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() @@ -65,10 +72,13 @@ impl BufRead for TestStdinLock<'_> { } } +#[cfg(feature = "test")] pub(crate) type TestStdinInner = Arc>>; +#[cfg(feature = "test")] struct TestStdin(TestStdinInner); +#[cfg(feature = "test")] impl Stdin for TestStdin { fn lock(&self) -> Box { Box::new(TestStdinLock { @@ -179,12 +189,15 @@ impl StderrSource for super::OSProcess { // ----------------------- test support for writers ------------------ +#[cfg(feature = "test")] pub(super) struct TestWriterLock<'a> { inner: MutexGuard<'a, Vec>, } +#[cfg(feature = "test")] impl WriterLock for TestWriterLock<'_> {} +#[cfg(feature = "test")] impl Write for TestWriterLock<'_> { fn write(&mut self, buf: &[u8]) -> Result { self.inner.write(buf)