From c6bb4bbb644d375dfa5bcb0effc2e8279e58d4dd Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 30 May 2025 16:37:20 +0200 Subject: [PATCH 1/3] Avoid intermediate allocation in listing --- src/cli/common.rs | 2 +- src/cli/rustup_mode.rs | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/cli/common.rs b/src/cli/common.rs index f8f0174990..c4a205c222 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -383,7 +383,7 @@ where /// The boolean value is needed to determine whether to print "(installed)" /// next to the target/component name." pub(super) fn list_items( - items: impl Iterator, + items: impl Iterator, installed_only: bool, quiet: bool, process: &Process, diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index c4397dc20d..a4494658db 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1177,10 +1177,7 @@ async fn target_list( common::list_items( distributable.components()?.into_iter().filter_map(|c| { if c.component.short_name_in_manifest() == "rust-std" && c.available { - c.component - .target - .as_deref() - .map(|target| (target.to_string(), c.installed)) + c.component.target.map(|target| (target, c.installed)) } else { None } @@ -1192,10 +1189,7 @@ async fn target_list( } else { let toolchain = cfg.toolchain_from_partial(toolchain).await?; common::list_items( - toolchain - .installed_targets()? - .iter() - .map(|s| (s.to_string(), true)), + toolchain.installed_targets()?.iter().map(|s| (s, true)), installed_only, quiet, cfg.process, @@ -1314,7 +1308,7 @@ async fn component_list( toolchain .installed_components()? .iter() - .map(|s| (s.name().to_string(), true)), + .map(|s| (s.name(), true)), installed_only, quiet, cfg.process, From 31eb7a875f3e0fbb26bf62bc761fe90ffe83ffc3 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 30 May 2025 17:18:31 +0200 Subject: [PATCH 2/3] Leverage bool::then_some() to simplify some code --- src/cli/rustup_mode.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index a4494658db..7476d32fa9 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1291,13 +1291,10 @@ async fn component_list( // downcasting required because the toolchain files can name any toolchain if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await { common::list_items( - distributable.components()?.into_iter().filter_map(|c| { - if c.available { - Some((c.name, c.installed)) - } else { - None - } - }), + distributable + .components()? + .into_iter() + .filter_map(|c| c.available.then_some((c.name, c.installed))), installed_only, quiet, cfg.process, From 9606e94cb715663cb69f6980111d99ff5ecc5e73 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 30 May 2025 17:21:48 +0200 Subject: [PATCH 3/3] Tweak list_items() docstring Use the first line as a high-level description. Separate the explanation of the `items` argument type, and drop a stray '"' character. --- src/cli/common.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cli/common.rs b/src/cli/common.rs index c4a205c222..cc554a38df 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -378,10 +378,10 @@ where Ok(utils::ExitCode(0)) } -/// Iterates over pairs representing the name of a target or component and a -/// boolean value indicating whether it is installed or not. -/// The boolean value is needed to determine whether to print "(installed)" -/// next to the target/component name." +/// Print a list of items (targets or components) to stdout. +/// +/// `items` represents the list of items, with the name and a boolean +/// to represent whether the item is currently installed. pub(super) fn list_items( items: impl Iterator, installed_only: bool,