From 7eadc29a59d8a3114d8d85eac34e8088da166974 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 15 Mar 2023 02:46:32 +0800 Subject: [PATCH] renamed iter_filesystems() to filesystems() --- lib/vfs/src/filesystems.rs | 26 ++++++++++++++------------ lib/vfs/src/overlay_fs.rs | 34 ++++++++++++++-------------------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/lib/vfs/src/filesystems.rs b/lib/vfs/src/filesystems.rs index 73a1b6faaef..bf47eb2df8e 100644 --- a/lib/vfs/src/filesystems.rs +++ b/lib/vfs/src/filesystems.rs @@ -2,11 +2,13 @@ use crate::FileSystem; /// A chain of one or more [`FileSystem`]s. pub trait FileSystems<'a>: 'a { - // FIXME(Michael-F-Bryan): Rewrite this to use GATs and an external iterator - // when we bump the MSRV to 1.65 or higher. That'll get rid of all the - // lifetimes and HRTBs. + // FIXME(Michael-F-Bryan): Rewrite this to use GATs when we bump the MSRV to + // 1.65 or higher. That'll get rid of all the lifetimes and HRTBs. type Iter: IntoIterator + 'a; - fn iter_filesystems(&'a self) -> Self::Iter; + + /// Get something that can be used to iterate over the underlying + /// filesystems. + fn filesystems(&'a self) -> Self::Iter; } impl<'a, 'b, S> FileSystems<'a> for &'b S @@ -16,8 +18,8 @@ where { type Iter = S::Iter; - fn iter_filesystems(&'a self) -> Self::Iter { - (**self).iter_filesystems() + fn filesystems(&'a self) -> Self::Iter { + (**self).filesystems() } } @@ -27,8 +29,8 @@ where { type Iter = <[T] as FileSystems<'a>>::Iter; - fn iter_filesystems(&'a self) -> Self::Iter { - self[..].iter_filesystems() + fn filesystems(&'a self) -> Self::Iter { + self[..].filesystems() } } @@ -38,7 +40,7 @@ where { type Iter = [&'a dyn FileSystem; N]; - fn iter_filesystems(&'a self) -> Self::Iter { + fn filesystems(&'a self) -> Self::Iter { // TODO: rewrite this when array::each_ref() is stable let mut i = 0; [(); N].map(|_| { @@ -55,7 +57,7 @@ where { type Iter = std::iter::Map, fn(&T) -> &dyn FileSystem>; - fn iter_filesystems(&'a self) -> Self::Iter { + fn filesystems(&'a self) -> Self::Iter { self.iter().map(|fs| fs as &dyn FileSystem) } } @@ -63,7 +65,7 @@ where impl<'a> FileSystems<'a> for () { type Iter = std::iter::Empty<&'a dyn FileSystem>; - fn iter_filesystems(&'a self) -> Self::Iter { + fn filesystems(&'a self) -> Self::Iter { std::iter::empty() } } @@ -84,7 +86,7 @@ macro_rules! tuple_filesystems { { type Iter = [&'a dyn FileSystem; count!($first $($rest)*)]; - fn iter_filesystems(&'a self) -> Self::Iter { + fn filesystems(&'a self) -> Self::Iter { #[allow(non_snake_case)] let ($first, $($rest),*) = self; diff --git a/lib/vfs/src/overlay_fs.rs b/lib/vfs/src/overlay_fs.rs index eaf349b399d..35627dc82d7 100644 --- a/lib/vfs/src/overlay_fs.rs +++ b/lib/vfs/src/overlay_fs.rs @@ -89,7 +89,7 @@ where } fn permission_error_or_not_found(&self, path: &Path) -> Result<(), FsError> { - for fs in self.secondaries.iter_filesystems() { + for fs in self.secondaries.filesystems() { if fs.exists(path) { return Err(FsError::PermissionDenied); } @@ -108,18 +108,11 @@ where let mut entries = Vec::new(); let mut had_at_least_one_success = false; - match self.primary.read_dir(path) { - Ok(r) => { - for entry in r { - entries.push(entry?); - } - had_at_least_one_success = true; - } - Err(e) if should_continue(e) => {} - Err(e) => return Err(e), - } + let filesystems = std::iter::once(&self.primary as &dyn FileSystem) + .into_iter() + .chain(self.secondaries().filesystems()); - for fs in self.secondaries.iter_filesystems() { + for fs in filesystems { match fs.read_dir(path) { Ok(r) => { for entry in r { @@ -133,10 +126,11 @@ where } if had_at_least_one_success { - // Note: this sort is guaranteed to be stable, so filesystems - // "higher up" the chain will be further towards the start. - entries.sort_by(|a, b| a.path.cmp(&b.path)); // Make sure later entries are removed in favour of earlier ones. + // Note: this sort is guaranteed to be stable, meaning filesystems + // "higher up" the chain will be further towards the start and kept + // when deduplicating. + entries.sort_by(|a, b| a.path.cmp(&b.path)); entries.dedup_by(|a, b| a.path == b.path); Ok(ReadDir::new(entries)) @@ -179,7 +173,7 @@ where Err(e) => return Err(e), } - for fs in self.secondaries.iter_filesystems() { + for fs in self.secondaries.filesystems() { match fs.metadata(path) { Err(e) if should_continue(e) => continue, other => return other, @@ -227,7 +221,7 @@ where if let Some(parent) = path.parent() { let parent_exists_on_secondary_fs = self .secondaries - .iter_filesystems() + .filesystems() .into_iter() .any(|fs| fs.is_dir(parent)); if parent_exists_on_secondary_fs { @@ -250,7 +244,7 @@ where return Err(FsError::PermissionDenied); } - for fs in self.secondaries.iter_filesystems() { + for fs in self.secondaries.filesystems() { match fs.new_open_options().options(conf.clone()).open(path) { Err(e) if should_continue(e) => continue, other => return other, @@ -276,7 +270,7 @@ where if conf.create { // Would we create the file if it doesn't exist yet? let already_exists = secondaries - .iter_filesystems() + .filesystems() .into_iter() .any(|fs| fs.is_file(path)); @@ -302,7 +296,7 @@ where fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut f = f.debug_list(); - for fs in self.0.iter_filesystems() { + for fs in self.0.filesystems() { f.entry(&fs); }