From 75077dea44b9b268c5e3108ff9fc44a6f4278d99 Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 4 Sep 2026 16:55:32 +0200 Subject: [PATCH 1/3] refactor(toolchain/distributable): take iterator in `DistributableToolchain::add_components()` --- src/cli/rustup_mode.rs | 22 +++++++++------------- src/toolchain/distributable.rs | 7 +++++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 824722d8a0..ea9b4f5872 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1504,18 +1504,13 @@ async fn target_add( } distributable - .add_components( - targets - .into_iter() - .map(|target| { - Component::new( - "rust-std".to_string(), - Some(TargetTuple::new(target)), - false, - ) - }) - .collect(), - ) + .add_components(targets.into_iter().map(|target| { + Component::new( + "rust-std".to_string(), + Some(TargetTuple::new(target)), + false, + ) + })) .await?; Ok(ExitCode::SUCCESS) @@ -1613,7 +1608,8 @@ async fn component_add( components .into_iter() .map(|component| Component::try_new(&component, &distributable, target.as_ref())) - .collect::>()?, + .collect::>>()? + .into_iter(), ) .await?; diff --git a/src/toolchain/distributable.rs b/src/toolchain/distributable.rs index aa06931ff6..13e0c6ef00 100644 --- a/src/toolchain/distributable.rs +++ b/src/toolchain/distributable.rs @@ -65,7 +65,10 @@ impl<'a> DistributableToolchain<'a> { &self.desc } - pub(crate) async fn add_components(&self, components: Vec) -> anyhow::Result<()> { + pub(crate) async fn add_components( + &self, + components: impl Iterator, + ) -> anyhow::Result<()> { let manifestation = self.get_manifestation()?; let manifest = self.get_manifest()?; @@ -78,7 +81,7 @@ impl<'a> DistributableToolchain<'a> { .get(&self.desc.target) .expect("installed manifest should have a known target"); - let mut validated_components = Vec::with_capacity(components.len()); + let mut validated_components = Vec::with_capacity(components.size_hint().0); for mut component in components { if let Some(c) = manifest.rename_component(&component) { From d31fee281141e71b42a7ca13caeb59b761639490 Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 4 Sep 2026 16:58:25 +0200 Subject: [PATCH 2/3] refactor(cli/rustup-mode): reduce rightward drift in `target_add()` --- src/cli/rustup_mode.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index ea9b4f5872..cfc17b8e1d 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1478,17 +1478,17 @@ async fn target_add( ) .await?; - let components = distributable.components()?; - if targets.contains(&"all".to_string()) { - if targets.len() != 1 { - return Err(anyhow!( - "`rustup target add {}` includes `all`", - targets.join(" ") - )); - } + let all = targets.iter().any(|it| it == "all"); + if all && targets.len() != 1 { + return Err(anyhow!( + "`rustup target add {}` includes `all`", + targets.join(" ") + )); + } + if all { targets.clear(); - for component in components { + for component in distributable.components()? { if component.component.short_name() == "rust-std" && component.available && !component.installed From 81044d61d888b50724ee56a170e18cc9d9cf0450 Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 4 Sep 2026 17:02:40 +0200 Subject: [PATCH 3/3] refactor(cli/rustup-mode): avoid redundant allocs in `target_add()` --- src/cli/rustup_mode.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index cfc17b8e1d..233de8c16a 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1464,7 +1464,7 @@ async fn target_list( async fn target_add( cfg: &Cfg<'_>, - mut targets: Vec, + targets: Vec, toolchain: Option, ) -> anyhow::Result { // XXX: long term move this error to cli ? the normal .into doesn't work @@ -1487,20 +1487,14 @@ async fn target_add( } if all { - targets.clear(); - for component in distributable.components()? { - if component.component.short_name() == "rust-std" - && component.available - && !component.installed - { - let target = component - .component - .target - .as_ref() - .expect("rust-std should have a target"); - targets.push(target.to_string()); - } - } + distributable + .add_components(distributable.components()?.into_iter().filter_map(|c| { + (c.available && !c.installed && c.component.short_name() == "rust-std") + .then_some(c.component) + })) + .await?; + + return Ok(ExitCode::SUCCESS); } distributable