impl: use shorthand initialization, remove old work-around

This switches to using shorthand struct field initialization. It also
removes a work-around for an older version of Rust on Windows.

Finally, we remove an explicit dependency on winapi, which was
required by the aforementioned work-around. However, we do still
indirectly depend on winapi via winapi-util.
This commit is contained in:
vsuryamurthy 2023-03-16 13:55:45 +01:00 committed by GitHub
parent abf3a15887
commit 21be522788
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 44 deletions

View File

@ -23,10 +23,6 @@ members = ["walkdir-list"]
[dependencies] [dependencies]
same-file = "1.0.1" same-file = "1.0.1"
[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["std", "winnt"]
[target.'cfg(windows)'.dependencies.winapi-util] [target.'cfg(windows)'.dependencies.winapi-util]
version = "0.1.1" version = "0.1.1"

View File

@ -177,18 +177,6 @@ impl DirEntry {
} }
/// Returns true if and only if this entry points to a directory. /// Returns true if and only if this entry points to a directory.
///
/// This works around a bug in Rust's standard library:
/// https://github.com/rust-lang/rust/issues/46484
#[cfg(windows)]
pub(crate) fn is_dir(&self) -> bool {
use std::os::windows::fs::MetadataExt;
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
self.metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0
}
/// Returns true if and only if this entry points to a directory.
#[cfg(not(windows))]
pub(crate) fn is_dir(&self) -> bool { pub(crate) fn is_dir(&self) -> bool {
self.ty.is_dir() self.ty.is_dir()
} }
@ -205,13 +193,7 @@ impl DirEntry {
let md = ent let md = ent
.metadata() .metadata()
.map_err(|err| Error::from_path(depth, path.clone(), err))?; .map_err(|err| Error::from_path(depth, path.clone(), err))?;
Ok(DirEntry { Ok(DirEntry { path, ty, follow_link: false, depth, metadata: md })
path: path,
ty: ty,
follow_link: false,
depth: depth,
metadata: md,
})
} }
#[cfg(unix)] #[cfg(unix)]
@ -226,9 +208,9 @@ impl DirEntry {
.map_err(|err| Error::from_path(depth, ent.path(), err))?; .map_err(|err| Error::from_path(depth, ent.path(), err))?;
Ok(DirEntry { Ok(DirEntry {
path: ent.path(), path: ent.path(),
ty: ty, ty,
follow_link: false, follow_link: false,
depth: depth, depth,
ino: ent.ino(), ino: ent.ino(),
}) })
} }
@ -241,12 +223,7 @@ impl DirEntry {
let ty = ent let ty = ent
.file_type() .file_type()
.map_err(|err| Error::from_path(depth, ent.path(), err))?; .map_err(|err| Error::from_path(depth, ent.path(), err))?;
Ok(DirEntry { Ok(DirEntry { path: ent.path(), ty, follow_link: false, depth })
path: ent.path(),
ty: ty,
follow_link: false,
depth: depth,
})
} }
#[cfg(windows)] #[cfg(windows)]
@ -266,7 +243,7 @@ impl DirEntry {
path: pb, path: pb,
ty: md.file_type(), ty: md.file_type(),
follow_link: follow, follow_link: follow,
depth: depth, depth,
metadata: md, metadata: md,
}) })
} }
@ -290,7 +267,7 @@ impl DirEntry {
path: pb, path: pb,
ty: md.file_type(), ty: md.file_type(),
follow_link: follow, follow_link: follow,
depth: depth, depth,
ino: md.ino(), ino: md.ino(),
}) })
} }
@ -312,7 +289,7 @@ impl DirEntry {
path: pb, path: pb,
ty: md.file_type(), ty: md.file_type(),
follow_link: follow, follow_link: follow,
depth: depth, depth,
}) })
} }
} }

View File

@ -164,10 +164,7 @@ impl Error {
pb: PathBuf, pb: PathBuf,
err: io::Error, err: io::Error,
) -> Self { ) -> Self {
Error { Error { depth, inner: ErrorInner::Io { path: Some(pb), err } }
depth: depth,
inner: ErrorInner::Io { path: Some(pb), err: err },
}
} }
pub(crate) fn from_entry(dent: &DirEntry, err: io::Error) -> Self { pub(crate) fn from_entry(dent: &DirEntry, err: io::Error) -> Self {
@ -175,13 +172,13 @@ impl Error {
depth: dent.depth(), depth: dent.depth(),
inner: ErrorInner::Io { inner: ErrorInner::Io {
path: Some(dent.path().to_path_buf()), path: Some(dent.path().to_path_buf()),
err: err, err,
}, },
} }
} }
pub(crate) fn from_io(depth: usize, err: io::Error) -> Self { pub(crate) fn from_io(depth: usize, err: io::Error) -> Self {
Error { depth: depth, inner: ErrorInner::Io { path: None, err: err } } Error { depth, inner: ErrorInner::Io { path: None, err } }
} }
pub(crate) fn from_loop( pub(crate) fn from_loop(
@ -190,7 +187,7 @@ impl Error {
child: &Path, child: &Path,
) -> Self { ) -> Self {
Error { Error {
depth: depth, depth,
inner: ErrorInner::Loop { inner: ErrorInner::Loop {
ancestor: ancestor.to_path_buf(), ancestor: ancestor.to_path_buf(),
child: child.to_path_buf(), child: child.to_path_buf(),

View File

@ -601,7 +601,7 @@ impl Ancestor {
#[cfg(windows)] #[cfg(windows)]
fn new(dent: &DirEntry) -> io::Result<Ancestor> { fn new(dent: &DirEntry) -> io::Result<Ancestor> {
let handle = Handle::from_path(dent.path())?; let handle = Handle::from_path(dent.path())?;
Ok(Ancestor { path: dent.path().to_path_buf(), handle: handle }) Ok(Ancestor { path: dent.path().to_path_buf(), handle })
} }
/// Create a new ancestor from the given directory path. /// Create a new ancestor from the given directory path.
@ -811,7 +811,7 @@ impl IntoIter {
where where
P: FnMut(&DirEntry) -> bool, P: FnMut(&DirEntry) -> bool,
{ {
FilterEntry { it: self, predicate: predicate } FilterEntry { it: self, predicate }
} }
fn handle_entry( fn handle_entry(
@ -1109,7 +1109,7 @@ where
/// [`min_depth`]: struct.WalkDir.html#method.min_depth /// [`min_depth`]: struct.WalkDir.html#method.min_depth
/// [`max_depth`]: struct.WalkDir.html#method.max_depth /// [`max_depth`]: struct.WalkDir.html#method.max_depth
pub fn filter_entry(self, predicate: P) -> FilterEntry<Self, P> { pub fn filter_entry(self, predicate: P) -> FilterEntry<Self, P> {
FilterEntry { it: self, predicate: predicate } FilterEntry { it: self, predicate }
} }
/// Skips the current directory. /// Skips the current directory.