# HG changeset patch # User Simon Sapin # Date 1622650484 -7200 # Node ID 777c3d2319130e1284eb67e30270bb7586d56531 # Parent 26127236b22970a1e63fab0f77adeafb0c282ba3 rust: Make some file path parameters less generic These are not widely used APIs that benefit from being maximally flexible, taking an explicit `&Path` borrow is fine and simplifies their internals. Differential Revision: https://phab.mercurial-scm.org/D10833 diff -r 26127236b229 -r 777c3d231913 rust/hg-core/src/filepatterns.rs --- a/rust/hg-core/src/filepatterns.rs Mon Apr 26 22:59:56 2021 +0200 +++ b/rust/hg-core/src/filepatterns.rs Wed Jun 02 18:14:44 2021 +0200 @@ -318,9 +318,9 @@ NoSuchFile(PathBuf), } -pub fn parse_pattern_file_contents>( +pub fn parse_pattern_file_contents( lines: &[u8], - file_path: P, + file_path: &Path, warn: bool, ) -> Result<(Vec, Vec), PatternError> { let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); @@ -357,7 +357,7 @@ current_syntax = rel_syntax; } else if warn { warnings.push(PatternFileWarning::InvalidSyntax( - file_path.as_ref().to_owned(), + file_path.to_owned(), syntax.to_owned(), )); } @@ -384,32 +384,30 @@ PatternError::UnsupportedSyntax(syntax) => { PatternError::UnsupportedSyntaxInFile( syntax, - file_path.as_ref().to_string_lossy().into(), + file_path.to_string_lossy().into(), line_number, ) } _ => e, })?, &line, - &file_path, + file_path, )); } Ok((inputs, warnings)) } -pub fn read_pattern_file>( - file_path: P, +pub fn read_pattern_file( + file_path: &Path, warn: bool, ) -> Result<(Vec, Vec), PatternError> { - let mut f = match File::open(file_path.as_ref()) { + let mut f = match File::open(file_path) { Ok(f) => Ok(f), Err(e) => match e.kind() { std::io::ErrorKind::NotFound => { return Ok(( vec![], - vec![PatternFileWarning::NoSuchFile( - file_path.as_ref().to_owned(), - )], + vec![PatternFileWarning::NoSuchFile(file_path.to_owned())], )) } _ => Err(e), @@ -431,15 +429,11 @@ } impl IgnorePattern { - pub fn new( - syntax: PatternSyntax, - pattern: &[u8], - source: impl AsRef, - ) -> Self { + pub fn new(syntax: PatternSyntax, pattern: &[u8], source: &Path) -> Self { Self { syntax, pattern: pattern.to_owned(), - source: source.as_ref().to_owned(), + source: source.to_owned(), } } } @@ -452,10 +446,10 @@ /// `subinclude:` is not treated as a special pattern here: unraveling them /// needs to occur in the "ignore" phase. pub fn get_patterns_from_file( - pattern_file: impl AsRef, - root_dir: impl AsRef, + pattern_file: &Path, + root_dir: &Path, ) -> PatternResult<(Vec, Vec)> { - let (patterns, mut warnings) = read_pattern_file(&pattern_file, true)?; + let (patterns, mut warnings) = read_pattern_file(pattern_file, true)?; let patterns = patterns .into_iter() .flat_map(|entry| -> PatternResult<_> { @@ -465,11 +459,9 @@ Ok(match syntax { PatternSyntax::Include => { let inner_include = - root_dir.as_ref().join(get_path_from_bytes(&pattern)); - let (inner_pats, inner_warnings) = get_patterns_from_file( - &inner_include, - root_dir.as_ref(), - )?; + root_dir.join(get_path_from_bytes(&pattern)); + let (inner_pats, inner_warnings) = + get_patterns_from_file(&inner_include, root_dir)?; warnings.extend(inner_warnings); inner_pats } @@ -496,9 +488,9 @@ impl SubInclude { pub fn new( - root_dir: impl AsRef, + root_dir: &Path, pattern: &[u8], - source: impl AsRef, + source: &Path, ) -> Result { let normalized_source = normalize_path_bytes(&get_bytes_from_path(source)); @@ -510,7 +502,7 @@ let path = source_root.join(get_path_from_bytes(pattern)); let new_root = path.parent().unwrap_or_else(|| path.deref()); - let prefix = canonical_path(&root_dir, &root_dir, new_root)?; + let prefix = canonical_path(root_dir, root_dir, new_root)?; Ok(Self { prefix: path_to_hg_path_buf(prefix).and_then(|mut p| { @@ -527,10 +519,10 @@ /// Separate and pre-process subincludes from other patterns for the "ignore" /// phase. -pub fn filter_subincludes( - ignore_patterns: &[IgnorePattern], - root_dir: impl AsRef, -) -> Result<(Vec, Vec<&IgnorePattern>), HgPathError> { +pub fn filter_subincludes<'a>( + ignore_patterns: &'a [IgnorePattern], + root_dir: &Path, +) -> Result<(Vec, Vec<&'a IgnorePattern>), HgPathError> { let mut subincludes = vec![]; let mut others = vec![]; @@ -541,7 +533,7 @@ source, } = ignore_pattern; if *syntax == PatternSyntax::SubInclude { - subincludes.push(SubInclude::new(&root_dir, pattern, &source)?); + subincludes.push(SubInclude::new(root_dir, pattern, &source)?); } else { others.push(ignore_pattern) } diff -r 26127236b229 -r 777c3d231913 rust/hg-core/src/matchers.rs --- a/rust/hg-core/src/matchers.rs Mon Apr 26 22:59:56 2021 +0200 +++ b/rust/hg-core/src/matchers.rs Wed Jun 02 18:14:44 2021 +0200 @@ -237,7 +237,7 @@ /// /// /// let ignore_patterns = /// vec![IgnorePattern::new(PatternSyntax::RootGlob, b"this*", Path::new(""))]; -/// let (matcher, _) = IncludeMatcher::new(ignore_patterns, "").unwrap(); +/// let (matcher, _) = IncludeMatcher::new(ignore_patterns, "".as_ref()).unwrap(); /// /// /// assert_eq!(matcher.matches(HgPath::new(b"testing")), false); /// assert_eq!(matcher.matches(HgPath::new(b"this should work")), true); @@ -479,7 +479,7 @@ /// should be matched. fn build_match<'a, 'b>( ignore_patterns: &'a [IgnorePattern], - root_dir: impl AsRef, + root_dir: &Path, ) -> PatternResult<( Vec, Box bool + 'b + Sync>, @@ -500,7 +500,7 @@ for SubInclude { prefix, root, path } in subincludes.into_iter() { let (match_fn, warnings) = - get_ignore_function(vec![path.to_path_buf()], root)?; + get_ignore_function(vec![path.to_path_buf()], &root)?; all_warnings.extend(warnings); prefixes.push(prefix.to_owned()); submatchers.insert(prefix.to_owned(), match_fn); @@ -573,7 +573,7 @@ /// ignored. pub fn get_ignore_function<'a>( all_pattern_files: Vec, - root_dir: impl AsRef, + root_dir: &Path, ) -> PatternResult<( Box Fn(&'r HgPath) -> bool + Sync + 'a>, Vec, @@ -581,9 +581,9 @@ let mut all_patterns = vec![]; let mut all_warnings = vec![]; - for pattern_file in all_pattern_files.into_iter() { + for pattern_file in &all_pattern_files { let (patterns, warnings) = - get_patterns_from_file(pattern_file, &root_dir)?; + get_patterns_from_file(pattern_file, root_dir)?; all_patterns.extend(patterns.to_owned()); all_warnings.extend(warnings); @@ -599,7 +599,7 @@ impl<'a> IncludeMatcher<'a> { pub fn new( ignore_patterns: Vec, - root_dir: impl AsRef, + root_dir: &Path, ) -> PatternResult<(Self, Vec)> { let (patterns, match_fn, warnings) = build_match(&ignore_patterns, root_dir)?; @@ -816,7 +816,7 @@ b"dir/subdir", Path::new(""), )], - "", + "".as_ref(), ) .unwrap(); @@ -854,7 +854,7 @@ b"dir/subdir", Path::new(""), )], - "", + "".as_ref(), ) .unwrap(); @@ -892,7 +892,7 @@ b"dir/z*", Path::new(""), )], - "", + "".as_ref(), ) .unwrap();