Skip to content

Commit 4675a7b

Browse files
committed
Add ToolchainConfig struct with a builder pattern
1 parent 19bb97f commit 4675a7b

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

collector/src/bin/collector.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use collector::runtime::{
3838
};
3939
use collector::runtime::{profile_runtime, RuntimeCompilationOpts};
4040
use collector::toolchain::{
41-
create_toolchain_from_published_version, get_local_toolchain, Sysroot, Toolchain,
41+
create_toolchain_from_published_version, get_local_toolchain, Sysroot, Toolchain, ToolchainConfig,
4242
};
4343
use collector::utils::cachegrind::cachegrind_diff;
4444
use collector::utils::{is_installed, wait_for_future};
@@ -664,10 +664,7 @@ fn main_result() -> anyhow::Result<i32> {
664664
let toolchain = get_local_toolchain(
665665
&[Profile::Opt],
666666
rustc,
667-
None,
668-
None,
669-
None,
670-
None,
667+
ToolchainConfig::default(),
671668
id,
672669
target_triple.clone(),
673670
)?;
@@ -723,10 +720,7 @@ fn main_result() -> anyhow::Result<i32> {
723720
let toolchain = get_local_toolchain(
724721
&[Profile::Opt],
725722
rustc,
726-
None,
727-
None,
728-
None,
729-
None,
723+
ToolchainConfig::default(),
730724
id,
731725
target_triple.clone(),
732726
)?;
@@ -762,10 +756,11 @@ fn main_result() -> anyhow::Result<i32> {
762756
let toolchain = get_local_toolchain(
763757
&profiles,
764758
&local.rustc,
765-
opts.rustdoc.as_deref(),
766-
opts.clippy.as_deref(),
767-
local.cargo.as_deref(),
768-
local.id.as_deref(),
759+
*ToolchainConfig::default()
760+
.rustdoc(opts.rustdoc.as_deref())
761+
.clippy(opts.clippy.as_deref())
762+
.cargo(local.cargo.as_deref())
763+
.id(local.id.as_deref()),
769764
"",
770765
target_triple,
771766
)?;
@@ -955,10 +950,11 @@ fn main_result() -> anyhow::Result<i32> {
955950
let toolchain = get_local_toolchain(
956951
profiles,
957952
rustc,
958-
opts.rustdoc.as_deref(),
959-
opts.clippy.as_deref(),
960-
local.cargo.as_deref(),
961-
local.id.as_deref(),
953+
*ToolchainConfig::default()
954+
.rustdoc(opts.rustdoc.as_deref())
955+
.clippy(opts.clippy.as_deref())
956+
.cargo(local.cargo.as_deref())
957+
.id(local.id.as_deref()),
962958
suffix,
963959
target_triple.clone(),
964960
)?;
@@ -1070,10 +1066,9 @@ fn get_local_toolchain_for_runtime_benchmarks(
10701066
get_local_toolchain(
10711067
&[Profile::Opt],
10721068
&local.rustc,
1073-
None,
1074-
None,
1075-
local.cargo.as_deref(),
1076-
local.id.as_deref(),
1069+
*ToolchainConfig::default()
1070+
.cargo(local.cargo.as_deref())
1071+
.id(local.id.as_deref()),
10771072
"",
10781073
target_triple.to_string(),
10791074
)

collector/src/toolchain.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,45 @@ impl ToolchainComponents {
292292
}
293293
}
294294

295+
#[derive(Clone, Copy)]
296+
pub struct ToolchainConfig<'a> {
297+
rustdoc: Option<&'a Path>,
298+
clippy: Option<&'a Path>,
299+
cargo: Option<&'a Path>,
300+
id: Option<&'a str>,
301+
}
302+
303+
impl<'a> ToolchainConfig<'a> {
304+
pub fn default() -> Self {
305+
Self {
306+
rustdoc: None,
307+
clippy: None,
308+
cargo: None,
309+
id: None
310+
}
311+
}
312+
313+
pub fn rustdoc(&mut self, rustdoc: Option<&'a Path>) -> &mut Self {
314+
self.rustdoc = rustdoc;
315+
self
316+
}
317+
318+
pub fn clippy(&mut self, clippy: Option<&'a Path>) -> &mut Self {
319+
self.clippy = clippy;
320+
self
321+
}
322+
323+
pub fn cargo(&mut self, cargo: Option<&'a Path>) -> &mut Self {
324+
self.cargo = cargo;
325+
self
326+
}
327+
328+
pub fn id(&mut self, id: Option<&'a str>) -> &mut Self {
329+
self.id = id;
330+
self
331+
}
332+
}
333+
295334
/// Get a toolchain from the input.
296335
/// - `rustc`: check if the given one is acceptable.
297336
/// - `rustdoc`: if one is given, check if it is acceptable. Otherwise, if
@@ -301,10 +340,7 @@ impl ToolchainComponents {
301340
pub fn get_local_toolchain(
302341
profiles: &[Profile],
303342
rustc: &str,
304-
rustdoc: Option<&Path>,
305-
clippy: Option<&Path>,
306-
cargo: Option<&Path>,
307-
id: Option<&str>,
343+
toolchain_config: ToolchainConfig<'_>,
308344
id_suffix: &str,
309345
target_triple: String,
310346
) -> anyhow::Result<Toolchain> {
@@ -359,7 +395,7 @@ pub fn get_local_toolchain(
359395
debug!("found rustc: {:?}", &rustc);
360396

361397
// When the id comes from a +toolchain, the suffix is *not* added.
362-
let id = if let Some(id) = id {
398+
let id = if let Some(id) = toolchain_config.id {
363399
let mut id = id.to_owned();
364400
id.push_str(id_suffix);
365401
id
@@ -374,7 +410,7 @@ pub fn get_local_toolchain(
374410

375411
// When specifying rustc via a path, the suffix is always added to the
376412
// id.
377-
let mut id = if let Some(id) = id {
413+
let mut id = if let Some(id) = toolchain_config.id {
378414
id.to_owned()
379415
} else {
380416
"Id".to_string()
@@ -385,7 +421,7 @@ pub fn get_local_toolchain(
385421
};
386422

387423
let rustdoc =
388-
if let Some(rustdoc) = &rustdoc {
424+
if let Some(rustdoc) = &toolchain_config.rustdoc {
389425
Some(rustdoc.canonicalize().with_context(|| {
390426
format!("failed to canonicalize rustdoc executable {:?}", rustdoc)
391427
})?)
@@ -406,7 +442,7 @@ pub fn get_local_toolchain(
406442
};
407443

408444
let clippy =
409-
if let Some(clippy) = &clippy {
445+
if let Some(clippy) = &toolchain_config.clippy {
410446
Some(clippy.canonicalize().with_context(|| {
411447
format!("failed to canonicalize clippy executable {:?}", clippy)
412448
})?)
@@ -425,7 +461,7 @@ pub fn get_local_toolchain(
425461
// No `clippy` provided, but none needed.
426462
None
427463
};
428-
let cargo = if let Some(cargo) = &cargo {
464+
let cargo = if let Some(cargo) = &toolchain_config.cargo {
429465
cargo
430466
.canonicalize()
431467
.with_context(|| format!("failed to canonicalize cargo executable {:?}", cargo))?

0 commit comments

Comments
 (0)