Skip to content

Commit a55ff4a

Browse files
committed
Move create_command to InstalledCommonToolchain
1 parent 754886f commit a55ff4a

File tree

2 files changed

+57
-44
lines changed

2 files changed

+57
-44
lines changed

src/config.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,10 @@ impl Cfg {
625625
if let Some(cmd) = self.maybe_do_cargo_fallback(toolchain, binary)? {
626626
Ok(cmd)
627627
} else {
628-
toolchain.create_command(binary)
628+
// NB this can only fail in race conditions since we used toolchain
629+
// for dir.
630+
let installed = toolchain.as_installed_common()?;
631+
installed.create_command(binary)
629632
}
630633
}
631634

@@ -644,7 +647,9 @@ impl Cfg {
644647
if let Some(cmd) = self.maybe_do_cargo_fallback(&toolchain, binary)? {
645648
Ok(cmd)
646649
} else {
647-
toolchain.create_command(binary)
650+
// NB note this really can't fail due to to having installed the toolchain if needed
651+
let installed = toolchain.as_installed_common()?;
652+
installed.create_command(binary)
648653
}
649654
}
650655

src/toolchain.rs

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ impl<'a> Toolchain<'a> {
7171
})
7272
}
7373

74+
pub fn as_installed_common(&'a self) -> Result<InstalledCommonToolchain<'a>> {
75+
if !self.exists() {
76+
// Should be verify perhaps?
77+
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
78+
}
79+
Ok(InstalledCommonToolchain(self))
80+
}
81+
7482
fn as_installed(&'a self) -> Result<Box<dyn InstalledToolchain<'a> + 'a>> {
7583
if self.is_custom() {
7684
let toolchain = CustomToolchain::new(self)?;
@@ -150,48 +158,6 @@ impl<'a> Toolchain<'a> {
150158
== Some(true)
151159
}
152160

153-
// Both Distributable and Custom; Installed only.
154-
pub fn create_command<T: AsRef<OsStr>>(&self, binary: T) -> Result<Command> {
155-
if !self.exists() {
156-
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
157-
}
158-
159-
// Create the path to this binary within the current toolchain sysroot
160-
let binary = if let Some(binary_str) = binary.as_ref().to_str() {
161-
if binary_str.to_lowercase().ends_with(EXE_SUFFIX) {
162-
binary.as_ref().to_owned()
163-
} else {
164-
OsString::from(format!("{}{}", binary_str, EXE_SUFFIX))
165-
}
166-
} else {
167-
// Very weird case. Non-unicode command.
168-
binary.as_ref().to_owned()
169-
};
170-
171-
let bin_path = self.path.join("bin").join(&binary);
172-
let path = if utils::is_file(&bin_path) {
173-
&bin_path
174-
} else {
175-
let recursion_count = env::var("RUST_RECURSION_COUNT")
176-
.ok()
177-
.and_then(|s| s.parse().ok())
178-
.unwrap_or(0);
179-
if recursion_count > env_var::RUST_RECURSION_COUNT_MAX - 1 {
180-
let defaults = self.cfg.get_default()?;
181-
return Err(ErrorKind::BinaryNotFound(
182-
binary.to_string_lossy().into(),
183-
self.name.clone(),
184-
Some(&self.name) == defaults.as_ref(),
185-
)
186-
.into());
187-
}
188-
Path::new(&binary)
189-
};
190-
let mut cmd = Command::new(&path);
191-
self.set_env(&mut cmd);
192-
Ok(cmd)
193-
}
194-
195161
// Custom and Distributable. Installed only.
196162
fn set_env(&self, cmd: &mut Command) {
197163
self.set_ldpath(cmd);
@@ -355,6 +321,48 @@ impl<'a> Toolchain<'a> {
355321
}
356322
}
357323

324+
/// Newtype hosting functions that apply to both custom and distributable toolchains that are installed.
325+
pub struct InstalledCommonToolchain<'a>(&'a Toolchain<'a>);
326+
327+
impl<'a> InstalledCommonToolchain<'a> {
328+
pub fn create_command<T: AsRef<OsStr>>(&self, binary: T) -> Result<Command> {
329+
// Create the path to this binary within the current toolchain sysroot
330+
let binary = if let Some(binary_str) = binary.as_ref().to_str() {
331+
if binary_str.to_lowercase().ends_with(EXE_SUFFIX) {
332+
binary.as_ref().to_owned()
333+
} else {
334+
OsString::from(format!("{}{}", binary_str, EXE_SUFFIX))
335+
}
336+
} else {
337+
// Very weird case. Non-unicode command.
338+
binary.as_ref().to_owned()
339+
};
340+
341+
let bin_path = self.0.path.join("bin").join(&binary);
342+
let path = if utils::is_file(&bin_path) {
343+
&bin_path
344+
} else {
345+
let recursion_count = env::var("RUST_RECURSION_COUNT")
346+
.ok()
347+
.and_then(|s| s.parse().ok())
348+
.unwrap_or(0);
349+
if recursion_count > env_var::RUST_RECURSION_COUNT_MAX - 1 {
350+
let defaults = self.0.cfg.get_default()?;
351+
return Err(ErrorKind::BinaryNotFound(
352+
binary.to_string_lossy().into(),
353+
self.0.name.clone(),
354+
Some(&self.0.name) == defaults.as_ref(),
355+
)
356+
.into());
357+
}
358+
Path::new(&binary)
359+
};
360+
let mut cmd = Command::new(&path);
361+
self.0.set_env(&mut cmd);
362+
Ok(cmd)
363+
}
364+
}
365+
358366
/// Newtype to facilitate splitting out custom-toolchain specific code.
359367
pub struct CustomToolchain<'a>(&'a Toolchain<'a>);
360368

0 commit comments

Comments
 (0)