@@ -44,7 +44,7 @@ pub struct FileType {
44
44
}
45
45
46
46
pub struct ReadDir {
47
- handle : FindNextFileHandle ,
47
+ handle : Option < FindNextFileHandle > ,
48
48
root : Arc < PathBuf > ,
49
49
first : Option < c:: WIN32_FIND_DATAW > ,
50
50
}
@@ -113,13 +113,13 @@ impl fmt::Debug for ReadDir {
113
113
impl Iterator for ReadDir {
114
114
type Item = io:: Result < DirEntry > ;
115
115
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 {
117
117
// This iterator was initialized with an `INVALID_HANDLE_VALUE` as its handle.
118
118
// Simply return `None` because this is only the case when `FindFirstFileExW` in
119
119
// the construction of this iterator returns `ERROR_FILE_NOT_FOUND` which means
120
120
// no matchhing files can be found.
121
121
return None ;
122
- }
122
+ } ;
123
123
if let Some ( first) = self . first . take ( ) {
124
124
if let Some ( e) = DirEntry :: new ( & self . root , & first) {
125
125
return Some ( Ok ( e) ) ;
@@ -128,7 +128,7 @@ impl Iterator for ReadDir {
128
128
unsafe {
129
129
let mut wfd = mem:: zeroed ( ) ;
130
130
loop {
131
- if c:: FindNextFileW ( self . handle . 0 , & mut wfd) == 0 {
131
+ if c:: FindNextFileW ( handle. 0 , & mut wfd) == 0 {
132
132
match api:: get_last_error ( ) {
133
133
WinError :: NO_MORE_FILES => return None ,
134
134
WinError { code } => {
@@ -1190,7 +1190,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
1190
1190
1191
1191
if find_handle != c:: INVALID_HANDLE_VALUE {
1192
1192
Ok ( ReadDir {
1193
- handle : FindNextFileHandle ( find_handle) ,
1193
+ handle : Some ( FindNextFileHandle ( find_handle) ) ,
1194
1194
root : Arc :: new ( root) ,
1195
1195
first : Some ( wfd) ,
1196
1196
} )
@@ -1208,11 +1208,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
1208
1208
// See issue #120040: https://github.com/rust-lang/rust/issues/120040.
1209
1209
let last_error = api:: get_last_error ( ) ;
1210
1210
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 } ) ;
1216
1212
}
1217
1213
1218
1214
// Just return the error constructed from the raw OS error if the above is not the case.
0 commit comments