Skip to content

Commit 10189db

Browse files
committed
Use Option for FindNextFileHandle
1 parent f3cc895 commit 10189db

File tree

1 file changed

+6
-10
lines changed
  • std/src/sys/pal/windows

1 file changed

+6
-10
lines changed

std/src/sys/pal/windows/fs.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct FileType {
4444
}
4545

4646
pub struct ReadDir {
47-
handle: FindNextFileHandle,
47+
handle: Option<FindNextFileHandle>,
4848
root: Arc<PathBuf>,
4949
first: Option<c::WIN32_FIND_DATAW>,
5050
}
@@ -113,13 +113,13 @@ impl fmt::Debug for ReadDir {
113113
impl Iterator for ReadDir {
114114
type Item = io::Result<DirEntry>;
115115
fn next(&mut self) -> Option<io::Result<DirEntry>> {
116-
if self.handle.0 == c::INVALID_HANDLE_VALUE {
116+
let Some(handle) = self.handle.as_ref() else {
117117
// This iterator was initialized with an `INVALID_HANDLE_VALUE` as its handle.
118118
// Simply return `None` because this is only the case when `FindFirstFileExW` in
119119
// the construction of this iterator returns `ERROR_FILE_NOT_FOUND` which means
120120
// no matchhing files can be found.
121121
return None;
122-
}
122+
};
123123
if let Some(first) = self.first.take() {
124124
if let Some(e) = DirEntry::new(&self.root, &first) {
125125
return Some(Ok(e));
@@ -128,7 +128,7 @@ impl Iterator for ReadDir {
128128
unsafe {
129129
let mut wfd = mem::zeroed();
130130
loop {
131-
if c::FindNextFileW(self.handle.0, &mut wfd) == 0 {
131+
if c::FindNextFileW(handle.0, &mut wfd) == 0 {
132132
match api::get_last_error() {
133133
WinError::NO_MORE_FILES => return None,
134134
WinError { code } => {
@@ -1190,7 +1190,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
11901190

11911191
if find_handle != c::INVALID_HANDLE_VALUE {
11921192
Ok(ReadDir {
1193-
handle: FindNextFileHandle(find_handle),
1193+
handle: Some(FindNextFileHandle(find_handle)),
11941194
root: Arc::new(root),
11951195
first: Some(wfd),
11961196
})
@@ -1208,11 +1208,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
12081208
// See issue #120040: https://github.com/rust-lang/rust/issues/120040.
12091209
let last_error = api::get_last_error();
12101210
if last_error == WinError::FILE_NOT_FOUND {
1211-
return Ok(ReadDir {
1212-
handle: FindNextFileHandle(find_handle),
1213-
root: Arc::new(root),
1214-
first: None,
1215-
});
1211+
return Ok(ReadDir { handle: None, root: Arc::new(root), first: None });
12161212
}
12171213

12181214
// Just return the error constructed from the raw OS error if the above is not the case.

0 commit comments

Comments
 (0)