Skip to content

Commit b9b9d30

Browse files
committed
Simplify process access to stdin
1 parent 3d7dc55 commit b9b9d30

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
use crate::utils::tty::{stderr_isatty, stdout_isatty};
@@ -66,20 +65,11 @@ use crate::utils::tty::{stderr_isatty, stdout_isatty};
6665
/// methods are in performance critical loops (except perhaps progress bars -
6766
/// and even there we should be doing debouncing and managing update rates).
6867
#[enum_dispatch]
69-
pub trait CurrentProcess:
70-
home::Env + CurrentDirSource + VarSource + StdinSource + ProcessSource + Debug
71-
{
72-
}
68+
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + ProcessSource + Debug {}
7369

7470
/// Allows concrete types for the currentprocess abstraction.
7571
#[derive(Clone, Debug)]
76-
#[enum_dispatch(
77-
CurrentProcess,
78-
CurrentDirSource,
79-
VarSource,
80-
StdinSource,
81-
ProcessSource
82-
)]
72+
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource, ProcessSource)]
8373
pub enum Process {
8474
OSProcess(OSProcess),
8575
#[cfg(feature = "test")]
@@ -116,6 +106,14 @@ impl Process {
116106
}
117107
}
118108

109+
pub(crate) fn stdin(&self) -> Box<dyn filesource::Stdin> {
110+
match self {
111+
Process::OSProcess(_) => Box::new(io::stdin()),
112+
#[cfg(feature = "test")]
113+
Process::TestProcess(p) => Box::new(filesource::TestStdin(p.stdin.clone())),
114+
}
115+
}
116+
119117
pub(crate) fn stdout(&self) -> Box<dyn filesource::Writer> {
120118
match self {
121119
Process::OSProcess(_) => Box::new(io::stdout()),
@@ -231,9 +229,9 @@ pub struct TestProcess {
231229
pub args: Vec<String>,
232230
pub vars: HashMap<String, String>,
233231
pub id: u64,
234-
pub stdin: TestStdinInner,
235-
pub stdout: TestWriterInner,
236-
pub stderr: TestWriterInner,
232+
pub stdin: filesource::TestStdinInner,
233+
pub stdout: filesource::TestWriterInner,
234+
pub stderr: filesource::TestWriterInner,
237235
}
238236

239237
#[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)