Implement Iterator for FatDir.

This commit is contained in:
Rafał Harabień 2017-09-24 02:24:42 +02:00
parent daa51e3540
commit 894c2d34f3

View File

@ -115,22 +115,7 @@ impl FatDir {
pub fn list(&mut self) -> io::Result<Vec<FatDirEntry>> { pub fn list(&mut self) -> io::Result<Vec<FatDirEntry>> {
self.rewind(); self.rewind();
let mut entries = Vec::new(); Ok(self.map(|x| x.unwrap()).collect())
loop {
let entry = self.read_dir_entry()?;
if entry.name[0] == 0 {
break; // end of dir
}
if entry.name[0] == 0xE5 {
continue; // deleted
}
if entry.attrs == FatFileAttributes::LFN {
continue; // FIXME: support LFN
}
entries.push(entry);
}
Ok(entries)
} }
pub fn rewind(&mut self) { pub fn rewind(&mut self) {
@ -194,3 +179,27 @@ impl FatDir {
} }
} }
} }
impl Iterator for FatDir {
type Item = io::Result<FatDirEntry>;
fn next(&mut self) -> Option<io::Result<FatDirEntry>> {
loop {
let r = self.read_dir_entry();
let e = match r {
Ok(e) => e,
Err(_) => return Some(r),
};
if e.name[0] == 0 {
return None; // end of dir
}
if e.name[0] == 0xE5 {
continue; // deleted
}
if e.attrs == FatFileAttributes::LFN {
continue; // FIXME: support LFN
}
return Some(Ok(e))
}
}
}