Skip to content

Commit b6de3f7

Browse files
committed
Simplify process access to stdin
1 parent 4db3688 commit b6de3f7

File tree

3 files changed

+16
-40
lines changed

3 files changed

+16
-40
lines changed

src/cli/common.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ use once_cell::sync::Lazy;
1414

1515
use super::self_update;
1616
use crate::cli::download_tracker::DownloadTracker;
17-
use crate::currentprocess::{
18-
filesource::StdinSource, process, terminalsource, varsource::VarSource,
19-
};
17+
use crate::currentprocess::{process, terminalsource, varsource::VarSource};
2018
use crate::dist::dist::{TargetTriple, ToolchainDesc};
2119
use crate::dist::manifest::ComponentStatus;
2220
use crate::install::UpdateStatus;

src/currentprocess.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub mod terminalsource;
2727
pub mod varsource;
2828

2929
use cwdsource::*;
30-
use filesource::*;
3130
use varsource::*;
3231

3332
/// An abstraction for the current process.
@@ -65,20 +64,11 @@ use varsource::*;
6564
/// methods are in performance critical loops (except perhaps progress bars -
6665
/// and even there we should be doing debouncing and managing update rates).
6766
#[enum_dispatch]
68-
pub trait CurrentProcess:
69-
home::Env + CurrentDirSource + VarSource + StdinSource + ProcessSource + Debug
70-
{
71-
}
67+
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + ProcessSource + Debug {}
7268

7369
/// Allows concrete types for the currentprocess abstraction.
7470
#[derive(Clone, Debug)]
75-
#[enum_dispatch(
76-
CurrentProcess,
77-
CurrentDirSource,
78-
VarSource,
79-
StdinSource,
80-
ProcessSource
81-
)]
71+
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource, ProcessSource)]
8272
pub enum Process {
8373
OSProcess(OSProcess),
8474
#[cfg(feature = "test")]
@@ -115,6 +105,14 @@ impl Process {
115105
}
116106
}
117107

108+
pub(crate) fn stdin(&self) -> Box<dyn filesource::Stdin> {
109+
match self {
110+
Process::OSProcess(_) => Box::new(io::stdin()),
111+
#[cfg(feature = "test")]
112+
Process::TestProcess(p) => Box::new(filesource::TestStdin(p.stdin.clone())),
113+
}
114+
}
115+
118116
pub(crate) fn stdout(&self) -> Box<dyn filesource::Writer> {
119117
match self {
120118
Process::OSProcess(_) => Box::new(io::stdout()),
@@ -296,9 +294,9 @@ pub struct TestProcess {
296294
pub args: Vec<String>,
297295
pub vars: HashMap<String, String>,
298296
pub id: u64,
299-
pub stdin: TestStdinInner,
300-
pub stdout: TestWriterInner,
301-
pub stderr: TestWriterInner,
297+
pub stdin: filesource::TestStdinInner,
298+
pub stdout: filesource::TestWriterInner,
299+
pub stderr: filesource::TestWriterInner,
302300
}
303301

304302
#[cfg(feature = "test")]

src/currentprocess/filesource.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::io::{self, BufRead, Read, Result, Write};
22

3-
use enum_dispatch::enum_dispatch;
4-
53
use super::terminalsource::{ColorableTerminal, StreamSelector};
64
use crate::currentprocess::process;
75

@@ -14,12 +12,6 @@ pub trait Stdin {
1412
/// Stand-in for std::io::StdinLock
1513
pub trait StdinLock: Read + BufRead {}
1614

17-
/// Stand-in for std::io::stdin
18-
#[enum_dispatch]
19-
pub trait StdinSource {
20-
fn stdin(&self) -> Box<dyn Stdin>;
21-
}
22-
2315
// ----------------- OS support for stdin -----------------
2416

2517
impl StdinLock for io::StdinLock<'_> {}
@@ -34,12 +26,6 @@ impl Stdin for io::Stdin {
3426
}
3527
}
3628

37-
impl StdinSource for super::OSProcess {
38-
fn stdin(&self) -> Box<dyn Stdin> {
39-
Box::new(io::stdin())
40-
}
41-
}
42-
4329
// -------------- stdout -------------------------------
4430

4531
/// This is a stand-in for [`std::io::StdoutLock`] and [`std::io::StderrLock`].
@@ -112,7 +98,7 @@ mod test_support {
11298
sync::{Arc, Mutex, MutexGuard},
11399
};
114100

115-
use super::{super::TestProcess, *};
101+
use super::*;
116102

117103
// ----------------------- test support for stdin ------------------
118104

@@ -139,7 +125,7 @@ mod test_support {
139125

140126
pub(crate) type TestStdinInner = Arc<Mutex<Cursor<String>>>;
141127

142-
struct TestStdin(TestStdinInner);
128+
pub struct TestStdin(pub(in super::super) TestStdinInner);
143129

144130
impl Stdin for TestStdin {
145131
fn lock(&self) -> Box<dyn StdinLock + '_> {
@@ -152,12 +138,6 @@ mod test_support {
152138
}
153139
}
154140

155-
impl StdinSource for TestProcess {
156-
fn stdin(&self) -> Box<dyn Stdin> {
157-
Box::new(TestStdin(self.stdin.clone()))
158-
}
159-
}
160-
161141
// ----------------------- test support for writers ------------------
162142

163143
pub(in super::super) struct TestWriterLock<'a> {

0 commit comments

Comments
 (0)