Skip to content

Commit 6d6aef9

Browse files
committed
Remove unnecessary trait abstraction
1 parent 14e3e6f commit 6d6aef9

File tree

4 files changed

+14
-57
lines changed

4 files changed

+14
-57
lines changed

Cargo.lock

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ clap_complete = "4"
5151
download = { path = "download", default-features = false }
5252
effective-limits = "0.5.5"
5353
enum-map = "2.5.0"
54-
enum_dispatch.workspace = true
5554
flate2 = "1"
5655
fs_at.workspace = true
5756
git-testament = "0.2"
@@ -159,7 +158,6 @@ members = ["download", "rustup-macros"]
159158

160159
[workspace.dependencies]
161160
anyhow = "1.0.69"
162-
enum_dispatch = "0.3.11"
163161
fs_at = "0.1.6"
164162
once_cell = "1.18.0"
165163
opentelemetry = "0.22"

src/bin/rustup-init.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustup::cli::rustup_mode;
2626
#[cfg(windows)]
2727
use rustup::cli::self_update;
2828
use rustup::cli::setup_mode;
29-
use rustup::currentprocess::{process, with, OSProcess};
29+
use rustup::currentprocess::{process, with, Process};
3030
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
3131
use rustup::is_proxyable_tools;
3232
use rustup::utils::utils;
@@ -35,8 +35,8 @@ fn main() {
3535
#[cfg(windows)]
3636
pre_rustup_main_init();
3737

38-
let process = OSProcess::default();
39-
with(process.into(), || match maybe_trace_rustup() {
38+
let process = Process::os();
39+
with(process, || match maybe_trace_rustup() {
4040
Err(e) => {
4141
common::report_error(&e);
4242
std::process::exit(1);

src/currentprocess.rs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::{
1414
sync::{Arc, Mutex},
1515
};
1616

17-
use enum_dispatch::enum_dispatch;
1817
#[cfg(feature = "test")]
1918
use rand::{thread_rng, Rng};
2019

@@ -23,53 +22,19 @@ pub mod terminalsource;
2322

2423
use crate::utils::tty::{stderr_isatty, stdout_isatty};
2524

26-
/// An abstraction for the current process.
27-
///
28-
/// This acts as a clonable proxy to the global state provided by some key OS
29-
/// interfaces - it is a zero cost abstraction. For the test variant it manages
30-
/// a mutex and takes out locks to ensure consistency.
31-
///
32-
/// This provides replacements env::arg*, env::var*, and the standard files
33-
/// io::std* with traits that are customisable for tests. As a result any macros
34-
/// or code that have non-pluggable usage of those are incompatible with
35-
/// CurrentProcess and must not be used. That includes \[e\]println! as well as
36-
/// third party crates.
37-
///
38-
/// CurrentProcess is used via an instance in a thread local variable; when
39-
/// making new threads, be sure to copy CurrentProcess::process() into the new
40-
/// thread before calling any code that may need to use a CurrentProcess
41-
/// function.
42-
///
43-
/// Run some code using with: this will set the current instance, call your
44-
/// function, then finally reset the instance at the end before returning.
45-
///
46-
/// Testing level interoperation with external code that depends on environment
47-
/// variables could be possible with a hypothetical `with_projected()` which
48-
/// would be a zero-cost operation in real processes, but in test processes will
49-
/// take a lock out to mutually exclude other code, then overwrite the current
50-
/// value of std::env::vars, restoring it at the end. However, the only use for
51-
/// that today is a test of cargo::home, which is now implemented in a separate
52-
/// crate, so we've just deleted the test.
53-
///
54-
/// A thread local is used to permit the instance to be available to the entire
55-
/// rustup library without needing to explicitly wire this normally global state
56-
/// everywhere; and a trait object with dyn dispatch is likewise used to avoid
57-
/// needing to thread trait parameters across the entire code base: none of the
58-
/// methods are in performance critical loops (except perhaps progress bars -
59-
/// and even there we should be doing debouncing and managing update rates).
60-
#[enum_dispatch]
61-
pub trait CurrentProcess: Debug {}
62-
6325
/// Allows concrete types for the currentprocess abstraction.
6426
#[derive(Clone, Debug)]
65-
#[enum_dispatch(CurrentProcess)]
6627
pub enum Process {
6728
OSProcess(OSProcess),
6829
#[cfg(feature = "test")]
6930
TestProcess(TestProcess),
7031
}
7132

7233
impl Process {
34+
pub fn os() -> Self {
35+
Self::OSProcess(OSProcess::new())
36+
}
37+
7338
pub fn name(&self) -> Option<String> {
7439
let arg0 = match self.var("RUSTUP_FORCE_ARG0") {
7540
Ok(v) => Some(v),
@@ -186,6 +151,13 @@ impl home::env::Env for Process {
186151
}
187152
}
188153

154+
#[cfg(feature = "test")]
155+
impl From<TestProcess> for Process {
156+
fn from(p: TestProcess) -> Self {
157+
Self::TestProcess(p)
158+
}
159+
}
160+
189161
/// Obtain the current instance of CurrentProcess
190162
pub fn process() -> Process {
191163
home_process()

0 commit comments

Comments
 (0)