Skip to content

Commit 91e610a

Browse files
committed
Auto merge of #6620 - dwijnand:make-better-use-of-stdlib-Path-strip_prefix, r=alexcrichton
Replace util::without_prefix with Path::strip_prefix None
2 parents 4e74e2f + 092c88c commit 91e610a

File tree

7 files changed

+19
-36
lines changed

7 files changed

+19
-36
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
6969
let mut list: Vec<_> = src
7070
.list_files(pkg)?
7171
.iter()
72-
.map(|file| util::without_prefix(file, root).unwrap().to_path_buf())
72+
.map(|file| file.strip_prefix(root).unwrap().to_path_buf())
7373
.collect();
7474
if include_lockfile(pkg) {
7575
list.push("Cargo.lock".into());
@@ -266,7 +266,7 @@ fn check_vcs_file_collision(pkg: &Package, src_files: &[PathBuf]) -> CargoResult
266266
let vcs_info_path = Path::new(VCS_INFO_FILE);
267267
let collision = src_files
268268
.iter()
269-
.find(|&p| util::without_prefix(&p, root).unwrap() == vcs_info_path);
269+
.find(|&p| p.strip_prefix(root).unwrap() == vcs_info_path);
270270
if collision.is_some() {
271271
failure::bail!(
272272
"Invalid inclusion of reserved file name \
@@ -296,7 +296,7 @@ fn tar(
296296
let config = ws.config();
297297
let root = pkg.root();
298298
for file in src_files.iter() {
299-
let relative = util::without_prefix(file, root).unwrap();
299+
let relative = file.strip_prefix(root)?;
300300
check_filename(relative)?;
301301
let relative = relative.to_str().ok_or_else(|| {
302302
failure::format_err!("non-utf8 path in source directory: {}", relative.display())

src/cargo/ops/cargo_run.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::Path;
33

44
use crate::core::{nightly_features_allowed, TargetKind, Workspace};
55
use crate::ops;
6-
use crate::util::{self, CargoResult, ProcessError};
6+
use crate::util::{CargoResult, ProcessError};
77

88
pub fn run(
99
ws: &Workspace<'_>,
@@ -81,10 +81,10 @@ pub fn run(
8181
let compile = ops::compile(ws, options)?;
8282
assert_eq!(compile.binaries.len(), 1);
8383
let exe = &compile.binaries[0];
84-
let exe = match util::without_prefix(exe, config.cwd()) {
85-
Some(path) if path.file_name() == Some(path.as_os_str()) => Path::new(".").join(path),
86-
Some(path) => path.to_path_buf(),
87-
None => exe.to_path_buf(),
84+
let exe = match exe.strip_prefix(config.cwd()) {
85+
Ok(path) if path.file_name() == Some(path.as_os_str()) => Path::new(".").join(path),
86+
Ok(path) => path.to_path_buf(),
87+
Err(_) => exe.to_path_buf(),
8888
};
8989
let pkg = bins[0].0;
9090
let mut process = compile.target_process(exe, pkg)?;

src/cargo/ops/cargo_test.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::core::compiler::{Compilation, Doctest};
44
use crate::core::Workspace;
55
use crate::ops;
66
use crate::util::errors::CargoResult;
7-
use crate::util::{self, CargoTestError, ProcessError, Test};
7+
use crate::util::{CargoTestError, ProcessError, Test};
88

99
pub struct TestOptions<'a> {
1010
pub compile_opts: ops::CompileOptions<'a>,
@@ -81,18 +81,15 @@ fn run_unit_tests(
8181
let mut errors = Vec::new();
8282

8383
for &(ref pkg, ref kind, ref test, ref exe) in &compilation.tests {
84-
let to_display = match util::without_prefix(exe, cwd) {
85-
Some(path) => path,
86-
None => &**exe,
87-
};
84+
let exe_display = exe.strip_prefix(cwd).unwrap_or(exe).display();
8885
let mut cmd = compilation.target_process(exe, pkg)?;
8986
cmd.args(test_args);
9087
config
9188
.shell()
92-
.concise(|shell| shell.status("Running", to_display.display().to_string()))?;
89+
.concise(|shell| shell.status("Running", &exe_display))?;
9390
config
9491
.shell()
95-
.verbose(|shell| shell.status("Running", cmd.to_string()))?;
92+
.verbose(|shell| shell.status("Running", &cmd))?;
9693

9794
let result = cmd.exec();
9895

src/cargo/sources/path.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::core::{Dependency, Package, PackageId, Source, SourceId, Summary};
1313
use crate::ops;
1414
use crate::util::paths;
1515
use crate::util::Config;
16-
use crate::util::{self, internal, CargoResult};
16+
use crate::util::{internal, CargoResult};
1717

1818
pub struct PathSource<'cfg> {
1919
source_id: SourceId,
@@ -200,7 +200,7 @@ impl<'cfg> PathSource<'cfg> {
200200
// matching to paths
201201

202202
let mut filter = |path: &Path| -> CargoResult<bool> {
203-
let relative_path = util::without_prefix(path, root).unwrap();
203+
let relative_path = path.strip_prefix(root)?;
204204
let glob_should_package = glob_should_package(relative_path);
205205
let ignore_should_package = ignore_should_package(relative_path)?;
206206

@@ -278,7 +278,7 @@ impl<'cfg> PathSource<'cfg> {
278278
Ok(index) => index,
279279
Err(err) => return Some(Err(err.into())),
280280
};
281-
let path = util::without_prefix(root, cur).unwrap().join("Cargo.toml");
281+
let path = root.strip_prefix(cur).unwrap().join("Cargo.toml");
282282
if index.get_path(&path, 0).is_some() {
283283
return Some(self.list_files_git(pkg, &repo, filter));
284284
}
@@ -325,7 +325,7 @@ impl<'cfg> PathSource<'cfg> {
325325
});
326326
let mut opts = git2::StatusOptions::new();
327327
opts.include_untracked(true);
328-
if let Some(suffix) = util::without_prefix(pkg_path, root) {
328+
if let Ok(suffix) = pkg_path.strip_prefix(root) {
329329
opts.pathspec(suffix);
330330
}
331331
let statuses = repo.statuses(Some(&mut opts))?;
@@ -376,7 +376,7 @@ impl<'cfg> PathSource<'cfg> {
376376

377377
if is_dir.unwrap_or_else(|| file_path.is_dir()) {
378378
warn!(" found submodule {}", file_path.display());
379-
let rel = util::without_prefix(&file_path, root).unwrap();
379+
let rel = file_path.strip_prefix(root)?;
380380
let rel = rel.to_str().ok_or_else(|| {
381381
failure::format_err!("invalid utf-8 filename: {}", rel.display())
382382
})?;

src/cargo/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::hex::{hash_u64, short_hash, to_hex};
1313
pub use self::lev_distance::lev_distance;
1414
pub use self::lockserver::{LockServer, LockServerClient, LockServerStarted};
1515
pub use self::paths::{bytes2path, dylib_path, join_paths, path2bytes};
16-
pub use self::paths::{dylib_path_envvar, normalize_path, without_prefix};
16+
pub use self::paths::{dylib_path_envvar, normalize_path};
1717
pub use self::process_builder::{process, ProcessBuilder};
1818
pub use self::progress::{Progress, ProgressStyle};
1919
pub use self::read2::read2;

src/cargo/util/paths.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,6 @@ pub fn normalize_path(path: &Path) -> PathBuf {
8585
ret
8686
}
8787

88-
pub fn without_prefix<'a>(long_path: &'a Path, prefix: &'a Path) -> Option<&'a Path> {
89-
let mut a = long_path.components();
90-
let mut b = prefix.components();
91-
loop {
92-
match b.next() {
93-
Some(y) => match a.next() {
94-
Some(x) if x == y => continue,
95-
_ => return None,
96-
},
97-
None => return Some(a.as_path()),
98-
}
99-
}
100-
}
101-
10288
pub fn resolve_executable(exec: &Path) -> CargoResult<PathBuf> {
10389
if exec.components().count() == 1 {
10490
let paths = env::var_os("PATH").ok_or_else(|| failure::format_err!("no PATH"))?;

src/cargo/util/toml/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn do_read_manifest(
5353

5454
let toml = {
5555
let pretty_filename =
56-
util::without_prefix(manifest_file, config.cwd()).unwrap_or(manifest_file);
56+
manifest_file.strip_prefix(config.cwd()).unwrap_or(manifest_file);
5757
parse(contents, pretty_filename, config)?
5858
};
5959

0 commit comments

Comments
 (0)