DirIter: now exposed by FatFs

This commit is contained in:
Moritz Gmeiner 2025-07-29 20:32:25 +02:00
commit 3c977d2d42
2 changed files with 17 additions and 9 deletions

View file

@ -2,7 +2,7 @@ use std::cell::RefCell;
use std::io::{Read, Seek, SeekFrom, Write}; use std::io::{Read, Seek, SeekFrom, Write};
use std::rc::Rc; use std::rc::Rc;
use crate::dir::{DirEntry, DirIter}; use crate::dir::DirIter;
use crate::fat::{FatError, Fatty}; use crate::fat::{FatError, Fatty};
use crate::subslice::{SubSlice, SubSliceMut}; use crate::subslice::{SubSlice, SubSliceMut};
@ -217,7 +217,12 @@ impl FatFs {
Ok(data) Ok(data)
} }
pub fn root_dir_iter(&self) -> Box<dyn Iterator<Item = DirEntry> + '_> { fn chain_reader(&self, first_cluster: u32) -> impl Read {
iter::ClusterChainReader::new(self, first_cluster)
}
pub fn root_dir_iter<'a>(&'a self) -> DirIter<Box<dyn Read + 'a>> {
// Box<dyn Iterator<Item = DirEntry> + '_>
// TODO: maybe wrap this in another RootDirIter enum, so we don't have to Box<dyn> // TODO: maybe wrap this in another RootDirIter enum, so we don't have to Box<dyn>
if let Some(root_dir_offset) = self.root_dir_offset { if let Some(root_dir_offset) = self.root_dir_offset {
@ -225,7 +230,7 @@ impl FatFs {
let sub_slice = SubSlice::new(self, root_dir_offset, self.root_dir_size); let sub_slice = SubSlice::new(self, root_dir_offset, self.root_dir_size);
return Box::new(DirIter::new(sub_slice)); return DirIter::new(Box::new(sub_slice));
} }
// FAT32 // FAT32
@ -235,10 +240,15 @@ impl FatFs {
let cluster_iter = iter::ClusterChainReader::new(self, root_cluster); let cluster_iter = iter::ClusterChainReader::new(self, root_cluster);
Box::new(DirIter::new(cluster_iter)) DirIter::new(Box::new(cluster_iter))
} }
pub fn chain_reader(&self, first_cluster: u32) -> impl Read { pub fn dir_iter<'a>(&'a self, first_cluster: u32) -> DirIter<Box<dyn Read + 'a>> {
iter::ClusterChainReader::new(self, first_cluster) // TODO: return type must match root_dir_iter
// if the Box<dyn> is changed there, update here as well
let cluster_iter = self.chain_reader(first_cluster);
DirIter::new(Box::new(cluster_iter))
} }
} }

View file

@ -68,9 +68,7 @@ fn tree(fat_fs: &FatFs, show_hidden: bool) {
} }
if dir_entry.is_dir() { if dir_entry.is_dir() {
let reader = fat_fs.chain_reader(dir_entry.first_cluster()); let iter = fat_fs.dir_iter(dir_entry.first_cluster());
let iter = DirIter::new(reader);
tree_impl(fat_fs, iter, show_hidden, indent + 1); tree_impl(fat_fs, iter, show_hidden, indent + 1);
} }