Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Clone for WalkDir #78

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::result;
use std::vec;
use std::sync::Arc;

use same_file::is_same_file;

Expand Down Expand Up @@ -241,18 +242,19 @@ pub type Result<T> = ::std::result::Result<T, Error>;
///
/// Note that when following symbolic/soft links, loops are detected and an
/// error is reported.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct WalkDir {
opts: WalkDirOptions,
root: PathBuf,
}

#[derive(Clone)]
struct WalkDirOptions {
follow_links: bool,
max_open: usize,
min_depth: usize,
max_depth: usize,
sorter: Option<Box<FnMut(&DirEntry,&DirEntry) -> Ordering + Send + Sync + 'static>>,
sorter: Option<Arc<FnMut(&DirEntry,&DirEntry) -> Ordering + Send + Sync + 'static>>,
contents_first: bool,
}

Expand Down Expand Up @@ -383,7 +385,7 @@ impl WalkDir {
/// ```
pub fn sort_by<F>(mut self, cmp: F) -> Self
where F: FnMut(&DirEntry, &DirEntry) -> Ordering + Send + Sync + 'static {
self.opts.sorter = Some(Box::new(cmp));
self.opts.sorter = Some(Arc::new(cmp));
self
}

Expand Down Expand Up @@ -784,6 +786,7 @@ impl IntoIter {
entries.sort_by(|a, b| {
match (a, b) {
(&Ok(ref a), &Ok(ref b)) => {
let cmp = Arc::get_mut(cmp).unwrap();
cmp(a, b)
}
(&Err(_), &Err(_)) => Ordering::Equal,
Expand Down Expand Up @@ -920,7 +923,7 @@ impl DirEntry {
/// # Errors
///
/// Similar to [`std::fs::metadata`], returns errors for path values that the program does not
/// have permissions to access or if the path does not exist.
/// have permissions to access or if the path does not exist.
///
/// [`WalkDir`]: struct.WalkDir.html
/// [`follow_links`]: struct.WalkDir.html#method.follow_links
Expand Down