Skip to content

Commit

Permalink
Remove unnecessary trait abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jun 4, 2024
1 parent 6b2508e commit 59d15e6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 57 deletions.
13 changes: 0 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/bin/rustup-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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) => {
Expand Down
50 changes: 11 additions & 39 deletions src/currentprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,25 @@ 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")]
TestProcess(TestProcess),
}

impl Process {
pub fn os() -> Self {
Self::OSProcess(OSProcess::new())
}

pub fn name(&self) -> Option<String> {
let arg0 = match self.var("RUSTUP_FORCE_ARG0") {
Ok(v) => Some(v),
Expand Down Expand Up @@ -185,6 +150,13 @@ impl home::env::Env for Process {
}
}

#[cfg(feature = "test")]
impl From<TestProcess> for Process {
fn from(p: TestProcess) -> Self {
Self::TestProcess(p)
}
}

/// Obtain the current instance of CurrentProcess
pub fn process() -> Process {
home_process()
Expand Down

0 comments on commit 59d15e6

Please sign in to comment.