6
6
//! For information on how to use the [`WalkDir`] type, have a look at
7
7
//! * [`jwalk::WalkDir`](https://docs.rs/jwalk/0.5.1/jwalk/type.WalkDir.html) if `parallel` feature is enabled
8
8
//! * [walkdir::WalkDir](https://docs.rs/walkdir/2.3.1/walkdir/struct.WalkDir.html) otherwise
9
- #[ cfg( feature = "fs-walkdir-parallel" ) ]
9
+
10
+ #[ cfg( any( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
11
+ mod shared {
12
+ /// The desired level of parallelism.
13
+ pub enum Parallelism {
14
+ /// Do not parallelize at all by making a serial traversal on the current thread.
15
+ Serial ,
16
+ /// Create a new thread pool for each traversal with up to 16 threads or the amount of logical cores of the machine.
17
+ ThreadPoolPerTraversal {
18
+ /// The base name of the threads we create as part of the thread-pool.
19
+ thread_name : & ' static str ,
20
+ } ,
21
+ }
22
+ }
23
+
10
24
///
25
+ #[ cfg( feature = "fs-walkdir-parallel" ) ]
11
26
pub mod walkdir {
12
- use std:: path:: Path ;
13
-
27
+ pub use super :: shared:: Parallelism ;
14
28
pub use jwalk:: { DirEntry as DirEntryGeneric , DirEntryIter as DirEntryIterGeneric , Error , WalkDir } ;
29
+ use std:: path:: Path ;
15
30
16
31
/// An alias for an uncustomized directory entry to match the one of the non-parallel version offered by `walkdir`.
17
32
pub type DirEntry = DirEntryGeneric < ( ( ) , ( ) ) > ;
18
33
19
- /// Instantiate a new directory iterator which will not skip hidden files.
20
- pub fn walkdir_new ( root : impl AsRef < Path > ) -> WalkDir {
21
- WalkDir :: new ( root)
22
- . skip_hidden ( false )
23
- . parallelism ( jwalk:: Parallelism :: Serial )
34
+ impl From < Parallelism > for jwalk:: Parallelism {
35
+ fn from ( v : Parallelism ) -> Self {
36
+ match v {
37
+ Parallelism :: Serial => jwalk:: Parallelism :: Serial ,
38
+ Parallelism :: ThreadPoolPerTraversal { thread_name } => {
39
+ let pool = jwalk:: rayon:: ThreadPoolBuilder :: new ( )
40
+ . num_threads ( num_cpus:: get ( ) . min ( 16 ) )
41
+ . stack_size ( 128 * 1024 )
42
+ . thread_name ( move |idx| format ! ( "{thread_name} {idx}" ) )
43
+ . build ( )
44
+ . expect ( "we only set options that can't cause a build failure" ) ;
45
+ jwalk:: Parallelism :: RayonExistingPool {
46
+ pool : pool. into ( ) ,
47
+ busy_timeout : None ,
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ /// Instantiate a new directory iterator which will not skip hidden files, with the given level of `parallelism`.
55
+ pub fn walkdir_new ( root : impl AsRef < Path > , parallelism : Parallelism ) -> WalkDir {
56
+ WalkDir :: new ( root) . skip_hidden ( false ) . parallelism ( parallelism. into ( ) )
24
57
}
25
58
26
59
/// Instantiate a new directory iterator which will not skip hidden files and is sorted
27
- pub fn walkdir_sorted_new ( root : impl AsRef < Path > ) -> WalkDir {
60
+ pub fn walkdir_sorted_new ( root : impl AsRef < Path > , parallelism : Parallelism ) -> WalkDir {
28
61
WalkDir :: new ( root)
29
62
. skip_hidden ( false )
30
63
. sort ( true )
31
- . parallelism ( jwalk :: Parallelism :: Serial )
64
+ . parallelism ( parallelism . into ( ) )
32
65
}
33
66
34
67
/// The Iterator yielding directory items
@@ -38,17 +71,18 @@ pub mod walkdir {
38
71
#[ cfg( all( feature = "walkdir" , not( feature = "fs-walkdir-parallel" ) ) ) ]
39
72
///
40
73
pub mod walkdir {
74
+ pub use super :: shared:: Parallelism ;
41
75
use std:: path:: Path ;
42
76
43
77
pub use walkdir:: { DirEntry , Error , WalkDir } ;
44
78
45
- /// Instantiate a new directory iterator which will not skip hidden files.
46
- pub fn walkdir_new ( root : impl AsRef < Path > ) -> WalkDir {
79
+ /// Instantiate a new directory iterator which will not skip hidden files, with the given level of `parallelism` .
80
+ pub fn walkdir_new ( root : impl AsRef < Path > , _ : Parallelism ) -> WalkDir {
47
81
WalkDir :: new ( root)
48
82
}
49
83
50
- /// Instantiate a new directory iterator which will not skip hidden files and is sorted
51
- pub fn walkdir_sorted_new ( root : impl AsRef < Path > ) -> WalkDir {
84
+ /// Instantiate a new directory iterator which will not skip hidden files and is sorted, with the given level of `parallelism`.
85
+ pub fn walkdir_sorted_new ( root : impl AsRef < Path > , _ : Parallelism ) -> WalkDir {
52
86
WalkDir :: new ( root) . sort_by_file_name ( )
53
87
}
54
88
0 commit comments