Skip to content

Commit cd60454

Browse files
committed
Simplify process access to stdin
1 parent 648af17 commit cd60454

File tree

3 files changed

+15
-38
lines changed

3 files changed

+15
-38
lines changed

src/cli/common.rs

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

1515
use super::self_update;
1616
use crate::cli::download_tracker::DownloadTracker;
17-
use crate::currentprocess::{filesource::StdinSource, terminalsource, varsource::VarSource};
17+
use crate::currentprocess::{terminalsource, varsource::VarSource};
1818
use crate::dist::dist::{TargetTriple, ToolchainDesc};
1919
use crate::install::UpdateStatus;
2020
use crate::utils::notifications as util_notifications;

src/currentprocess.rs

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

2828
use cwdsource::*;
29-
use filesource::*;
3029
use varsource::*;
3130

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

7268
/// Allows concrete types for the currentprocess abstraction.
7369
#[derive(Clone, Debug)]
74-
#[enum_dispatch(
75-
CurrentProcess,
76-
CurrentDirSource,
77-
VarSource,
78-
StdinSource,
79-
ProcessSource
80-
)]
70+
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource, ProcessSource)]
8171
pub enum Process {
8272
OSProcess(OSProcess),
8373
#[cfg(feature = "test")]
@@ -114,6 +104,14 @@ impl Process {
114104
}
115105
}
116106

107+
pub(crate) fn stdin(&self) -> Box<dyn filesource::Stdin> {
108+
match self {
109+
Process::OSProcess(_) => Box::new(io::stdin()),
110+
#[cfg(feature = "test")]
111+
Process::TestProcess(p) => Box::new(filesource::TestStdin(p.stdin.clone())),
112+
}
113+
}
114+
117115
pub(crate) fn stdout(&self) -> Box<dyn filesource::Writer> {
118116
match self {
119117
Process::OSProcess(_) => Box::new(io::stdout()),
@@ -229,9 +227,9 @@ pub struct TestProcess {
229227
pub args: Vec<String>,
230228
pub vars: HashMap<String, String>,
231229
pub id: u64,
232-
pub stdin: TestStdinInner,
233-
pub stdout: TestWriterInner,
234-
pub stderr: TestWriterInner,
230+
pub stdin: filesource::TestStdinInner,
231+
pub stdout: filesource::TestWriterInner,
232+
pub stderr: filesource::TestWriterInner,
235233
}
236234

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

src/currentprocess/filesource.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::io::{self, BufRead, Cursor, Read, Result, Write};
22
use std::sync::{Arc, Mutex, MutexGuard};
33

4-
use enum_dispatch::enum_dispatch;
5-
64
use crate::currentprocess::process;
75

86
use super::terminalsource::{ColorableTerminal, StreamSelector};
@@ -16,12 +14,6 @@ pub trait Stdin {
1614
/// Stand-in for std::io::StdinLock
1715
pub trait StdinLock: Read + BufRead {}
1816

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

2719
impl StdinLock for io::StdinLock<'_> {}
@@ -36,12 +28,6 @@ impl Stdin for io::Stdin {
3628
}
3729
}
3830

39-
impl StdinSource for super::OSProcess {
40-
fn stdin(&self) -> Box<dyn Stdin> {
41-
Box::new(io::stdin())
42-
}
43-
}
44-
4531
// ----------------------- test support for stdin ------------------
4632

4733
struct TestStdinLock<'a> {
@@ -67,7 +53,7 @@ impl BufRead for TestStdinLock<'_> {
6753

6854
pub(crate) type TestStdinInner = Arc<Mutex<Cursor<String>>>;
6955

70-
struct TestStdin(TestStdinInner);
56+
pub(super) struct TestStdin(pub(super) TestStdinInner);
7157

7258
impl Stdin for TestStdin {
7359
fn lock(&self) -> Box<dyn StdinLock + '_> {
@@ -80,13 +66,6 @@ impl Stdin for TestStdin {
8066
}
8167
}
8268

83-
#[cfg(feature = "test")]
84-
impl StdinSource for super::TestProcess {
85-
fn stdin(&self) -> Box<dyn Stdin> {
86-
Box::new(TestStdin(self.stdin.clone()))
87-
}
88-
}
89-
9069
// -------------- stdout -------------------------------
9170

9271
/// This is a stand-in for [`std::io::StdoutLock`] and [`std::io::StderrLock`].

0 commit comments

Comments
 (0)