Skip to main content

visit_files_bounded_depth

Function visit_files_bounded_depth 

Source
pub fn visit_files_bounded_depth<E, F>(
    dir: &Path,
    ext: &str,
    max_depth: usize,
    visit: F,
) -> Result<(), E>
where E: From<Error>, F: FnMut(&Path) -> Result<(), E>,
Expand description

Visits every file with extension ext under dir, in a deterministic order, without materialising the file list.

walk_files_bounded_depth returns a sorted Vec<PathBuf>. That is the right shape when a caller needs the whole list, and the wrong one when it only needs to see each path once: on a 10,000-page site the vector alone is ~1.9 MiB and it is held for the entire pass. emit_sidecars measured its peak heap at exactly that figure — the per-document work never exceeded the list it was iterating (#578).

This walks depth-first, sorting each directory’s entries by file name before descending, so the visit order is identical to sorting the full list — on every platform, since read_dir order is not portable — while peak memory is one directory listing rather than the tree.

The callback’s error stops the walk and is returned as-is.

§Errors

Returns the first I/O error from reading a directory, or the callback’s.