提交 5d0bd371 编写于 作者: B Ben S

Make Dir return an Iterator of files, not Vec

This is part of work to make the flow of files more iterator-able, rather than going in and out of vectors. Here, a Dir returns an iterator of files, rather than a pre-filled vector.

For now, this removes the ability for error messages to be displayed. Will be added in later though!
上级 d547c3f5
use std::io;
use std::fs;
use std::path::{Path, PathBuf};
use std::slice::Iter as SliceIter;
use feature::Git;
use file::{File, fields};
......@@ -36,17 +37,12 @@ impl Dir {
///
/// Passing in `recurse` means that any directories will be scanned for
/// their contents, as well.
pub fn files(&self, recurse: bool) -> Vec<File> {
let mut files = vec![];
for path in self.contents.iter() {
match File::from_path(path, Some(self), recurse) {
Ok(file) => files.push(file),
Err(e) => println!("{}: {}", path.display(), e),
}
pub fn files<'dir>(&'dir self, recurse: bool) -> Files<'dir> {
Files {
inner: self.contents.iter(),
recurse: recurse,
dir: &self,
}
files
}
/// Whether this directory contains a file with the given path.
......@@ -73,3 +69,18 @@ impl Dir {
}
}
}
pub struct Files<'dir> {
inner: SliceIter<'dir, PathBuf>,
recurse: bool,
dir: &'dir Dir,
}
impl<'dir> Iterator for Files<'dir> {
type Item = io::Result<File<'dir>>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|path| File::from_path(path, Some(self.dir), self.recurse))
}
}
\ No newline at end of file
......@@ -146,7 +146,7 @@ impl<'dir> Exa<'dir> {
match Dir::readdir(&dir_path, self.options.should_scan_for_git()) {
Ok(ref dir) => {
let mut files = dir.files(false);
let mut files = dir.files(false).flat_map(|f| f).collect();
self.options.transform_files(&mut files);
// When recursing, add any directories to the dirs stack
......
......@@ -99,7 +99,8 @@ impl Details {
// them, so we don't examine any directories that wouldn't
// have their contents listed anyway.
if let Some(ref dir) = file.this {
let mut files = dir.files(true);
let mut files = dir.files(true).flat_map(|f| f).collect();
filter.transform_files(&mut files);
self.add_files_to_table(table, &files, depth + 1);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册