diff --git a/Cargo.lock b/Cargo.lock index 8296509f94..ea6278e93b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -592,18 +592,6 @@ dependencies = [ "syn", ] -[[package]] -name = "enum_dispatch" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa18ce2bc66555b3218614519ac839ddb759a7d6720732f979ef8d13be147ecd" -dependencies = [ - "once_cell", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "env_proxy" version = "0.4.1" @@ -1953,7 +1941,6 @@ dependencies = [ "download", "effective-limits", "enum-map", - "enum_dispatch", "flate2", "fs_at", "git-testament", diff --git a/Cargo.toml b/Cargo.toml index e18fde42f9..0c8f365198 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,6 @@ clap_complete = "4" download = { path = "download", default-features = false } effective-limits = "0.5.5" enum-map = "2.5.0" -enum_dispatch.workspace = true flate2 = "1" fs_at.workspace = true git-testament = "0.2" @@ -134,7 +133,6 @@ members = ["download", "rustup-macros"] [workspace.dependencies] anyhow = "1.0.69" -enum_dispatch = "0.3.11" fs_at = "0.1.6" once_cell = "1.18.0" opentelemetry = "0.23" diff --git a/src/bin/rustup-init.rs b/src/bin/rustup-init.rs index 8e5ee411aa..7b13d00198 100644 --- a/src/bin/rustup-init.rs +++ b/src/bin/rustup-init.rs @@ -27,7 +27,7 @@ use rustup::cli::rustup_mode; #[cfg(windows)] use rustup::cli::self_update; use rustup::cli::setup_mode; -use rustup::currentprocess::{process, with_runtime, OSProcess}; +use rustup::currentprocess::{process, with_runtime, Process}; use rustup::env_var::RUST_RECURSION_COUNT_MAX; use rustup::is_proxyable_tools; use rustup::utils::utils::{self, ExitCode}; @@ -36,10 +36,10 @@ fn main() { #[cfg(windows)] pre_rustup_main_init(); - let process = OSProcess::default(); + let process = Process::os(); let mut builder = Builder::new_multi_thread(); builder.enable_all(); - with_runtime(process.into(), builder, { + with_runtime(process, builder, { async { match maybe_trace_rustup().await { Err(e) => { diff --git a/src/currentprocess.rs b/src/currentprocess.rs index 6473aae165..ba3d982e3d 100644 --- a/src/currentprocess.rs +++ b/src/currentprocess.rs @@ -15,53 +15,14 @@ use std::{ sync::{Arc, Mutex}, }; -use enum_dispatch::enum_dispatch; #[cfg(feature = "test")] use rand::{thread_rng, Rng}; pub mod filesource; pub mod terminalsource; -/// An abstraction for the current process. -/// -/// This acts as a clonable proxy to the global state provided by some key OS -/// interfaces - it is a zero cost abstraction. For the test variant it manages -/// a mutex and takes out locks to ensure consistency. -/// -/// This provides replacements env::arg*, env::var*, and the standard files -/// io::std* with traits that are customisable for tests. As a result any macros -/// or code that have non-pluggable usage of those are incompatible with -/// CurrentProcess and must not be used. That includes \[e\]println! as well as -/// third party crates. -/// -/// CurrentProcess is used via an instance in a thread local variable; when -/// making new threads, be sure to copy CurrentProcess::process() into the new -/// thread before calling any code that may need to use a CurrentProcess -/// function. -/// -/// Run some code using with: this will set the current instance, call your -/// function, then finally reset the instance at the end before returning. -/// -/// Testing level interoperation with external code that depends on environment -/// variables could be possible with a hypothetical `with_projected()` which -/// would be a zero-cost operation in real processes, but in test processes will -/// take a lock out to mutually exclude other code, then overwrite the current -/// value of std::env::vars, restoring it at the end. However, the only use for -/// that today is a test of cargo::home, which is now implemented in a separate -/// crate, so we've just deleted the test. -/// -/// A thread local is used to permit the instance to be available to the entire -/// rustup library without needing to explicitly wire this normally global state -/// everywhere; and a trait object with dyn dispatch is likewise used to avoid -/// needing to thread trait parameters across the entire code base: none of the -/// methods are in performance critical loops (except perhaps progress bars - -/// and even there we should be doing debouncing and managing update rates). -#[enum_dispatch] -pub trait CurrentProcess: Debug {} - /// Allows concrete types for the currentprocess abstraction. #[derive(Clone, Debug)] -#[enum_dispatch(CurrentProcess)] pub enum Process { OSProcess(OSProcess), #[cfg(feature = "test")] @@ -69,6 +30,10 @@ pub enum Process { } impl Process { + pub fn os() -> Self { + Self::OSProcess(OSProcess::new()) + } + pub fn name(&self) -> Option { let arg0 = match self.var("RUSTUP_FORCE_ARG0") { Ok(v) => Some(v), @@ -185,6 +150,13 @@ impl home::env::Env for Process { } } +#[cfg(feature = "test")] +impl From for Process { + fn from(p: TestProcess) -> Self { + Self::TestProcess(p) + } +} + /// Obtain the current instance of CurrentProcess pub fn process() -> Process { home_process()