9836: Refactor: quick clean-up of iteration idioms in the `vfs` crate r=matklad a=Some-Dood

This PR cleans up some of the iteration idioms used in the `vfs` crate. Most of the changes simply converted `for` loops into their `std::iter::Iterator`-method counterpart. Other changes required some inversion of logic to accommodate for better short-circuiting. Overall, there should be no behavioral changes. If there are any stylistic issues, I will gladly adhere to them and adjust the PR accordingly. Thanks!

Co-authored-by: Basti Ortiz <39114273+Some-Dood@users.noreply.github.com>
This commit is contained in:
bors[bot] 2021-08-11 09:54:09 +00:00 committed by GitHub
commit 145b51f9da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -136,10 +136,13 @@ pub fn contains_dir(&self, path: &AbsPath) -> bool {
impl Directories {
/// Returns `true` if `path` is included in `self`.
pub fn contains_file(&self, path: &AbsPath) -> bool {
// First, check the file extension...
let ext = path.extension().unwrap_or_default();
if self.extensions.iter().all(|it| it.as_str() != ext) {
return false;
}
// Then, check for path inclusion...
self.includes_path(path)
}
@ -158,25 +161,22 @@ pub fn contains_dir(&self, path: &AbsPath) -> bool {
/// - This path is longer than any element in `self.exclude` that is a prefix
/// of `path`. In case of equality, exclusion wins.
fn includes_path(&self, path: &AbsPath) -> bool {
let mut include: Option<&AbsPathBuf> = None;
let mut include = None::<&AbsPathBuf>;
for incl in &self.include {
if path.starts_with(incl) {
include = Some(match include {
Some(prev) if prev.starts_with(incl) => prev,
_ => incl,
})
});
}
}
let include = match include {
Some(it) => it,
None => return false,
};
for excl in &self.exclude {
if path.starts_with(excl) && excl.starts_with(include) {
return false;
}
}
true
!self.exclude.iter().any(|excl| path.starts_with(excl) && excl.starts_with(include))
}
}