Skip to content

Commit 0997247

Browse files
committed
Abstract over home::
The home crate also accesses global process state, so we need to abstract over it as well.
1 parent 2880f30 commit 0997247

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ codegen-units = 1
113113
# Reduce build time by setting proc-macro crates non optimized.
114114
[profile.release.build-override]
115115
opt-level = 0
116+
117+
[patch.crates-io]
118+
home = { path = "../rustup-home" }

src/currentprocess.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rand::{thread_rng, Rng};
1414
pub mod argsource;
1515
pub mod cwdsource;
1616
pub mod filesource;
17+
mod homethunk;
1718
pub mod varsource;
1819

1920
use argsource::*;

src/currentprocess/homethunk.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// Adapts currentprocess to the trait home::Env
2+
use std::ffi::OsString;
3+
use std::io;
4+
use std::ops::Deref;
5+
use std::path::PathBuf;
6+
7+
use super::CurrentDirSource;
8+
use super::CurrentProcess;
9+
use super::VarSource;
10+
11+
impl home::Env for Box<dyn CurrentProcess + 'static> {
12+
fn home_dir(&self) -> Option<PathBuf> {
13+
self.var("HOME").ok().map(|v| v.into())
14+
}
15+
fn current_dir(&self) -> Result<PathBuf, io::Error> {
16+
CurrentDirSource::current_dir(self.deref())
17+
}
18+
fn var_os(&self, key: &str) -> Option<OsString> {
19+
VarSource::var_os(self.deref(), key)
20+
}
21+
}

src/utils/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ pub fn to_absolute<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
466466
}
467467

468468
pub fn home_dir() -> Option<PathBuf> {
469-
home::home_dir()
469+
home::home_dir_from(&process())
470470
}
471471

472472
pub fn cargo_home() -> Result<PathBuf> {
473-
home::cargo_home().map_err(|e| Error::from_kind(ErrorKind::Io(e)))
473+
home::cargo_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
474474
}
475475

476476
// Creates a ~/.rustup folder
@@ -496,7 +496,7 @@ pub fn rustup_home_in_user_dir() -> Result<PathBuf> {
496496
}
497497

498498
pub fn rustup_home() -> Result<PathBuf> {
499-
home::rustup_home().map_err(|e| Error::from_kind(ErrorKind::Io(e)))
499+
home::rustup_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
500500
}
501501

502502
pub fn format_path_for_display(path: &str) -> String {

0 commit comments

Comments
 (0)