Skip to content

Commit

Permalink
Clippy suggestions and more doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegrosjean committed Feb 11, 2019
1 parent f432c4f commit 8fd5ae1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
Empty file added CHANGELOG.md
Empty file.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "jwalk"
version = "0.2.0"
authors = ["Jesse Grosjean <[email protected]>"]
description = "Recursively walk a directory."
description = "Filesystem walk performed in parallel with streamed and sorted results."
homepage = "https://github.com/jessegrosjean/jwalk"
repository = "https://github.com/jessegrosjean/jwalk"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
jwalk
=======

Fast recursive directory walk.
Filesystem walk.

- Performed in parallel using rayon
- Results are streamed in sorted order
- Entries streamed in sorted order
- Custom sort/filter/skip

[![Build Status](https://travis-ci.org/jessegrosjean/jwalk.svg?branch=master)](https://travis-ci.org/jessegrosjean/jwalk)
Expand Down
32 changes: 25 additions & 7 deletions src/core/dir_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl DirEntry {
file_type,
parent_spec,
metadata: metadata_cell,
content_spec: content_spec,
content_spec,
read_content_error: None,
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ impl DirEntry {
&self.file_name
}

/// File type for the file that this entry points at.
/// File type for the file/directory that this entry points at.
///
/// This function will not traverse symlinks.
pub fn file_type(&self) -> ::std::result::Result<&FileType, &Error> {
Expand All @@ -84,10 +84,17 @@ impl DirEntry {
self.depth
}

/// Path to the file that this entry represents.
/// Reference to the path of the directory containing this entry.
pub fn parent_path(&self) -> Option<&Path> {
self
.parent_spec
.as_ref()
.map(|parent_spec| parent_spec.path.as_ref())
}

/// Path to the file/directory represented by this entry.
///
/// The path is created by joining the `parent_spec` path with the filename of
/// this entry.
/// The path is created by joining `parent_path` with `file_name`.
pub fn path(&self) -> PathBuf {
let mut path = match self.parent_spec.as_ref() {
Some(parent_spec) => parent_spec.path.to_path_buf(),
Expand All @@ -97,7 +104,7 @@ impl DirEntry {
path
}

/// Metadata for the file that this entry points at.
/// Metadata for the file/directory that this entry points at.
///
/// This function will not traverse symlinks.
pub fn metadata(&self) -> ::std::result::Result<&Metadata, &Error> {
Expand All @@ -117,7 +124,7 @@ impl DirEntry {
/// may call `entry.set_content_spec(None)` to skip descending into a
/// particular directory.
pub fn set_content_spec(&mut self, content_spec: Option<ReadDirSpec>) {
self.content_spec = content_spec.map(|read_dir_spec| Arc::new(read_dir_spec));
self.content_spec = content_spec.map(Arc::new);
}

/// Error generated when reading this entry's content.
Expand All @@ -128,4 +135,15 @@ impl DirEntry {
pub(crate) fn set_read_content_error(&mut self, read_content_error: Option<Error>) {
self.read_content_error = read_content_error;
}

/// Consumes the entry returning the `depth`, `file_name`, `file_type`, and
/// `metadata` parts.
pub fn into_parts(self) -> (usize, OsString, Result<FileType>, Option<Result<Metadata>>) {
(
self.depth,
self.file_name,
self.file_type,
self.metadata.into_inner(),
)
}
}
20 changes: 11 additions & 9 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ impl RunContext {
}

fn schedule_read_dir_spec(&self, read_dir_spec: Ordered<Arc<ReadDirSpec>>) -> bool {
!self.read_dir_spec_queue.push(read_dir_spec).is_err()
self.read_dir_spec_queue.push(read_dir_spec).is_ok()
}

fn push_read_dir_result(&self, read_dir_result: Ordered<Result<ReadDir>>) -> bool {
!self.read_dir_result_queue.push(read_dir_result).is_err()
self.read_dir_result_queue.push(read_dir_result).is_ok()
}

fn complete_item(&self) {
Expand Down Expand Up @@ -137,10 +137,10 @@ fn multi_threaded_walk(
stop,
read_dir_spec_queue,
read_dir_result_queue,
client_function: client_function,
client_function,
};

read_dir_spec_iter.into_iter().par_bridge().for_each_with(
read_dir_spec_iter.par_bridge().for_each_with(
run_context,
|run_context, ordered_read_dir_spec| {
multi_threaded_walk_dir(ordered_read_dir_spec, run_context);
Expand Down Expand Up @@ -187,17 +187,19 @@ fn multi_threaded_walk_dir(read_dir_spec: Ordered<Arc<ReadDirSpec>>, run_context
run_context.complete_item();
}

pub(crate) type ClientFunctionResults = (
Ordered<Result<ReadDir>>,
Option<Vec<Ordered<Arc<ReadDirSpec>>>>,
);

pub(crate) fn run_client_function(
client_function: &Arc<ClientReadDirFunction>,
ordered_read_dir_spec: Ordered<Arc<ReadDirSpec>>,
) -> (
Ordered<Result<ReadDir>>,
Option<Vec<Ordered<Arc<ReadDirSpec>>>>,
) {
) -> ClientFunctionResults {
let Ordered {
value: read_dir_spec,
index_path,
child_count: _,
..
} = ordered_read_dir_spec;

let read_dir_result = client_function(read_dir_spec);
Expand Down
8 changes: 3 additions & 5 deletions src/core/ordered_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,10 @@ where

if let Some(ordered_work) = self.receive_buffer.pop() {
return Some(ordered_work);
} else if self.pending_count() == 0 {
return None;
} else {
if self.pending_count() == 0 {
return None;
} else {
thread::yield_now();
}
thread::yield_now();
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Fast recursive directory walk.
#![warn(clippy::all)]

//! Filesystem walk.
//!
//! - Performed in parallel using rayon
//! - Results are streamed in sorted order
//! - Entries streamed in sorted order
//! - Custom sort/filter/skip
//!
//! # Example
Expand Down Expand Up @@ -39,13 +41,15 @@ pub struct WalkDir {
options: WalkDirOptions,
}

pub type ProcessEntriesFunction = Fn(&mut Vec<Result<DirEntry>>) + Send + Sync + 'static;

struct WalkDirOptions {
sort: bool,
max_depth: usize,
skip_hidden: bool,
num_threads: usize,
preload_metadata: bool,
process_entries: Option<Arc<Fn(&mut Vec<Result<DirEntry>>) + Send + Sync>>,
process_entries: Option<Arc<ProcessEntriesFunction>>,
}

impl WalkDir {
Expand Down Expand Up @@ -138,7 +142,7 @@ impl IntoIterator for WalkDir {
let preload_metadata = self.options.preload_metadata;
let process_entries = self.options.process_entries.clone();

let dir_entry_iter = core::walk(&self.root, num_threads, move |read_dir_spec| {
core::walk(&self.root, num_threads, move |read_dir_spec| {
let depth = read_dir_spec.depth + 1;
let mut dir_entry_results: Vec<_> = fs::read_dir(&read_dir_spec.path)?
.filter_map(|dir_entry_result| {
Expand Down Expand Up @@ -191,14 +195,12 @@ impl IntoIterator for WalkDir {
});
}

process_entries.as_ref().map(|process_entries| {
if let Some(process_entries) = process_entries.as_ref() {
process_entries(&mut dir_entry_results);
});
}

Ok(ReadDir::new(dir_entry_results))
});

dir_entry_iter
})
}
}

Expand All @@ -218,6 +220,6 @@ impl Clone for WalkDirOptions {
fn is_hidden(file_name: &OsStr) -> bool {
file_name
.to_str()
.map(|s| s.starts_with("."))
.map(|s| s.starts_with('.'))
.unwrap_or(false)
}

0 comments on commit 8fd5ae1

Please sign in to comment.