Skip to content
Myr

refactor: Avoid unnecessary allocation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Use Cow::borrowed, if we can re-use the already absolute path.

authored by Thayne McCombs · committed by Thayne McCombs · parent fcd8466 · Browse files · 2 files+50 added−51 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+50 added−51 removed

  • other:+50 added−51 removed

2 files+50 added−51 removed

modified:src/filesystem.rs-47 removed
18 unmodified lines
19 let path = path.strip_prefix(".").unwrap_or(path);20 env::current_dir().map(|path_buf| path_buf.join(path))21}2223/// Construct an absolute path from a potentially relative path and a24/// pre-resolved working directory. Unlike `path_absolute_form`, this25/// does not call `env::current_dir()` and cannot fail.26pub fn make_absolute(path: &Path, cwd: &Path) -> PathBuf {27 if path.is_absolute() {28 return path.to_path_buf();29 }30 let path = path.strip_prefix(".").unwrap_or(path);31 cwd.join(path)32}3323pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {24 let path_buf = path_absolute_form(path)?;2526 #[cfg(windows)]
125 unmodified lines
152 strip_current_dir(Path::new("foo/bar/baz")),153 Path::new("foo/bar/baz")154 );155 }167168 #[test]169 fn make_absolute_with_relative_path() {170 use super::make_absolute;171 use std::path::PathBuf;172173 let cwd = Path::new("/home/user");174 assert_eq!(175 make_absolute(Path::new("foo/bar"), cwd),176 PathBuf::from("/home/user/foo/bar")177 );178 }179180 #[test]181 fn make_absolute_strips_dot_prefix() {182 use super::make_absolute;183 use std::path::PathBuf;184185 let cwd = Path::new("/home/user");186 assert_eq!(187 make_absolute(Path::new("./foo/bar"), cwd),188 PathBuf::from("/home/user/foo/bar")189 );190 }191192 #[test]193 fn make_absolute_with_absolute_path() {194 use super::make_absolute;195 use std::path::PathBuf;196197 let cwd = Path::new("/home/user");198 assert_eq!(199 make_absolute(Path::new("/absolute/path"), cwd),200 PathBuf::from("/absolute/path")201 );202 }156}
modified:src/walk.rs+50 added-4 removed
663 unmodified lines
664}665666fn search_str_for_entry<'a>(667 entry_path: &'a std::path::Path,668 cwd: Option<&std::path::Path>,668 full_path_base: Option<&std::path::Path>,669) -> Cow<'a, OsStr> {670 if let Some(cwd) = cwd {671 let abs_path = filesystem::make_absolute(entry_path, cwd);672 Cow::Owned(abs_path.into_os_string())670 if let Some(cwd) = full_path_base {671 // If full_path_base is some, that means that we need to return672 // the absolute path673 if entry_path.is_absolute() {674 return Cow::Borrowed(entry_path.as_os_str());675 }676 let path = entry_path.strip_prefix(".").unwrap_or(entry_path);677 Cow::Owned(cwd.join(path).into())678 } else {679 match entry_path.file_name() {680 Some(filename) => Cow::Borrowed(filename),681 None => unreachable!(
12 unmodified lines
694/// path will simply be written to standard output.695pub fn scan(paths: &[PathBuf], patterns: Vec<Regex>, config: Config) -> Result<ExitCode> {696 WorkerState::new(patterns, config).scan(paths)697}698699#[cfg(test)]700mod tests {701 use super::search_str_for_entry;702 use std::path::{Path, PathBuf};703704 #[test]705 fn search_str_for_entry_with_relative_path() {706 let full_path_base = Some(Path::new("/home/user"));707 assert_eq!(708 search_str_for_entry(Path::new("foo/bar"), full_path_base),709 PathBuf::from("/home/user/foo/bar")710 );711 }712713 #[test]714 fn search_str_for_entry_strips_dot_prefix() {715 let full_path_base = Some(Path::new("/home/user"));716 assert_eq!(717 search_str_for_entry(Path::new("./foo/bar"), full_path_base),718 PathBuf::from("/home/user/foo/bar")719 );720 }721722 #[test]723 fn search_str_for_entry_with_absolute_path() {724 let full_path_base = Some(Path::new("/home/user"));725 assert_eq!(726 search_str_for_entry(Path::new("/absolute/path"), full_path_base),727 PathBuf::from("/absolute/path")728 );729 }730731 #[test]732 fn search_str_no_base_dir() {733 assert_eq!(734 search_str_for_entry(Path::new("./foo/bar"), None),735 PathBuf::from("bar")736 );737 }738}