Skip to content

Commit 086145e

Browse files
committed
Remove unnecessary enum to reduce code complexity
The relevant is not in the hot path for a cargo build, so it acceptable to allocation String in order to reduce code coomplexity
1 parent 40e1360 commit 086145e

File tree

2 files changed

+39
-53
lines changed

2 files changed

+39
-53
lines changed

src/cargo/core/compiler/compile_kind.rs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::core::Target;
2-
use crate::util::config::BuildTargetConfigValue;
32
use crate::util::errors::CargoResult;
43
use crate::util::interning::InternedString;
54
use crate::util::{Config, StableHasher};
@@ -53,48 +52,31 @@ impl CompileKind {
5352
config: &Config,
5453
targets: &[String],
5554
) -> CargoResult<Vec<CompileKind>> {
56-
if targets.len() > 1 && !config.cli_unstable().multitarget {
57-
bail!("specifying multiple `--target` flags requires `-Zmultitarget`")
58-
}
59-
if !targets.is_empty() {
60-
return Ok(targets
55+
let dedup = |targets: &[String]| {
56+
Ok(targets
6157
.iter()
6258
.map(|value| Ok(CompileKind::Target(CompileTarget::new(value)?)))
6359
// First collect into a set to deduplicate any `--target` passed
6460
// more than once...
6561
.collect::<CargoResult<BTreeSet<_>>>()?
6662
// ... then generate a flat list for everything else to use.
6763
.into_iter()
68-
.collect());
64+
.collect())
65+
};
66+
67+
if !targets.is_empty() {
68+
if targets.len() > 1 && !config.cli_unstable().multitarget {
69+
bail!("specifying multiple `--target` flags requires `-Zmultitarget`")
70+
}
71+
return dedup(targets);
6972
}
7073

7174
let kinds = match &config.build_config()?.target {
72-
None => vec![CompileKind::Host],
73-
Some(build_target_config) => {
74-
let values = build_target_config.values(config);
75-
if values.len() > 1 && !config.cli_unstable().multitarget {
76-
bail!("specifying multiple `--target` flags requires `-Zmultitarget`")
77-
}
78-
values
79-
.into_iter()
80-
.map(|k| {
81-
use BuildTargetConfigValue::*;
82-
let value = match &k {
83-
Path(p) => p.to_str().expect("must be utf-8 in toml"),
84-
Simple(s) => s,
85-
};
86-
CompileTarget::new(value).map(CompileKind::Target)
87-
})
88-
// First collect into a set to deduplicate any `--target` passed
89-
// more than once...
90-
.collect::<CargoResult<BTreeSet<_>>>()?
91-
// ... then generate a flat list for everything else to use.
92-
.into_iter()
93-
.collect()
94-
}
75+
None => Ok(vec![CompileKind::Host]),
76+
Some(build_target_config) => dedup(&build_target_config.values(config)?),
9577
};
9678

97-
Ok(kinds)
79+
kinds
9880
}
9981

10082
/// Hash used for fingerprinting.

src/cargo/util/config/mod.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,34 +2203,38 @@ enum BuildTargetConfigInner {
22032203
}
22042204

22052205
impl BuildTargetConfig {
2206-
/// Gets values of `build.target` as a list of [`BuildTargetConfigValue`].
2207-
pub fn values(&self, config: &Config) -> Vec<BuildTargetConfigValue<'_>> {
2208-
let def_root = self.inner.definition.root(config);
2209-
fn map<'a>(s: &'a str, root: &Path) -> BuildTargetConfigValue<'a> {
2206+
/// Gets values of `build.target` as a list of strings.
2207+
pub fn values(&self, config: &Config) -> CargoResult<Vec<String>> {
2208+
let map = |s: &String| {
22102209
if s.ends_with(".json") {
2211-
// To absolute path.
2212-
BuildTargetConfigValue::Path(root.join(s))
2210+
// Path to a target specification file (in JSON).
2211+
// <https://doc.rust-lang.org/rustc/targets/custom.html>
2212+
self.inner
2213+
.definition
2214+
.root(config)
2215+
.join(s)
2216+
.to_str()
2217+
.expect("must be utf-8 in toml")
2218+
.to_string()
22132219
} else {
2214-
BuildTargetConfigValue::Simple(s)
2220+
// A string. Probably a target triple.
2221+
s.to_string()
22152222
}
2216-
}
2217-
match &self.inner.val {
2218-
BuildTargetConfigInner::One(s) => vec![map(s, def_root)],
2219-
BuildTargetConfigInner::Many(v) => v.iter().map(|s| map(s, def_root)).collect(),
2220-
}
2223+
};
2224+
let values = match &self.inner.val {
2225+
BuildTargetConfigInner::One(s) => vec![map(s)],
2226+
BuildTargetConfigInner::Many(v) => {
2227+
if v.len() > 1 && !config.cli_unstable().multitarget {
2228+
bail!("specifying multiple `target` in `build.target` config value requires `-Zmultitarget`")
2229+
} else {
2230+
v.iter().map(map).collect()
2231+
}
2232+
}
2233+
};
2234+
Ok(values)
22212235
}
22222236
}
22232237

2224-
/// Represents a value of `build.target`.
2225-
#[derive(Debug)]
2226-
pub enum BuildTargetConfigValue<'a> {
2227-
/// Path to a target specification file (in JSON).
2228-
/// <https://doc.rust-lang.org/rustc/targets/custom.html>
2229-
Path(PathBuf),
2230-
/// A string. Probably a target triple.
2231-
Simple(&'a str),
2232-
}
2233-
22342238
#[derive(Deserialize, Default)]
22352239
struct TermConfig {
22362240
verbose: Option<bool>,

0 commit comments

Comments
 (0)