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
9
10
- #[ cfg( any ( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
10
+ #[ cfg( feature = "walkdir" ) ]
11
11
mod shared {
12
12
/// The desired level of parallelism.
13
13
pub enum Parallelism {
@@ -21,7 +21,7 @@ mod shared {
21
21
}
22
22
}
23
23
24
- #[ cfg( any( feature = "walkdir" , feature = "fs-walkdir-parallel" , feature = "fs- read-dir") ) ]
24
+ #[ cfg( any( feature = "walkdir" , feature = "fs-read-dir" ) ) ]
25
25
mod walkdir_precompose {
26
26
use std:: borrow:: Cow ;
27
27
use std:: ffi:: OsStr ;
@@ -83,13 +83,13 @@ mod walkdir_precompose {
83
83
84
84
/// A platform over entries in a directory, which may or may not precompose unicode after retrieving
85
85
/// paths from the file system.
86
- #[ cfg( any ( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
86
+ #[ cfg( feature = "walkdir" ) ]
87
87
pub struct WalkDir < T > {
88
88
pub ( crate ) inner : Option < T > ,
89
89
pub ( crate ) precompose_unicode : bool ,
90
90
}
91
91
92
- #[ cfg( any ( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
92
+ #[ cfg( feature = "walkdir" ) ]
93
93
pub struct WalkDirIter < T , I , E >
94
94
where
95
95
T : Iterator < Item = Result < I , E > > ,
@@ -99,7 +99,7 @@ mod walkdir_precompose {
99
99
pub ( crate ) precompose_unicode : bool ,
100
100
}
101
101
102
- #[ cfg( any ( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
102
+ #[ cfg( feature = "walkdir" ) ]
103
103
impl < T , I , E > Iterator for WalkDirIter < T , I , E >
104
104
where
105
105
T : Iterator < Item = Result < I , E > > ,
@@ -142,128 +142,7 @@ pub mod read_dir {
142
142
}
143
143
144
144
///
145
- #[ cfg( feature = "fs-walkdir-parallel" ) ]
146
- pub mod walkdir {
147
- use std:: borrow:: Cow ;
148
- use std:: ffi:: OsStr ;
149
- use std:: fs:: FileType ;
150
- use std:: path:: Path ;
151
-
152
- use jwalk:: WalkDir as WalkDirImpl ;
153
- pub use jwalk:: { DirEntry as DirEntryGeneric , DirEntryIter as DirEntryIterGeneric , Error } ;
154
-
155
- pub use super :: shared:: Parallelism ;
156
-
157
- type DirEntryImpl = DirEntryGeneric < ( ( ) , ( ) ) > ;
158
-
159
- /// A directory entry returned by [DirEntryIter].
160
- pub type DirEntry = super :: walkdir_precompose:: DirEntry < DirEntryImpl > ;
161
- /// A platform to create a [DirEntryIter] from.
162
- pub type WalkDir = super :: walkdir_precompose:: WalkDir < WalkDirImpl > ;
163
-
164
- impl super :: walkdir_precompose:: DirEntryApi for DirEntryImpl {
165
- fn path ( & self ) -> Cow < ' _ , Path > {
166
- self . path ( ) . into ( )
167
- }
168
-
169
- fn file_name ( & self ) -> Cow < ' _ , OsStr > {
170
- self . file_name ( ) . into ( )
171
- }
172
-
173
- fn file_type ( & self ) -> std:: io:: Result < FileType > {
174
- Ok ( self . file_type ( ) )
175
- }
176
- }
177
-
178
- impl IntoIterator for WalkDir {
179
- type Item = Result < DirEntry , jwalk:: Error > ;
180
- type IntoIter = DirEntryIter ;
181
-
182
- fn into_iter ( self ) -> Self :: IntoIter {
183
- DirEntryIter {
184
- inner : self . inner . expect ( "always set (builder fix)" ) . into_iter ( ) ,
185
- precompose_unicode : self . precompose_unicode ,
186
- }
187
- }
188
- }
189
-
190
- impl WalkDir {
191
- /// Set the minimum component depth of paths of entries.
192
- pub fn min_depth ( mut self , min : usize ) -> Self {
193
- self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . min_depth ( min) ) ;
194
- self
195
- }
196
- /// Set the maximum component depth of paths of entries.
197
- pub fn max_depth ( mut self , max : usize ) -> Self {
198
- self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . max_depth ( max) ) ;
199
- self
200
- }
201
- /// Follow symbolic links.
202
- pub fn follow_links ( mut self , toggle : bool ) -> Self {
203
- self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . follow_links ( toggle) ) ;
204
- self
205
- }
206
- }
207
-
208
- impl From < Parallelism > for jwalk:: Parallelism {
209
- fn from ( v : Parallelism ) -> Self {
210
- match v {
211
- Parallelism :: Serial => jwalk:: Parallelism :: Serial ,
212
- Parallelism :: ThreadPoolPerTraversal { thread_name } => std:: thread:: available_parallelism ( )
213
- . map_or_else (
214
- |_| Parallelism :: Serial . into ( ) ,
215
- |threads| {
216
- let pool = jwalk:: rayon:: ThreadPoolBuilder :: new ( )
217
- . num_threads ( threads. get ( ) . min ( 16 ) )
218
- . stack_size ( 128 * 1024 )
219
- . thread_name ( move |idx| format ! ( "{thread_name} {idx}" ) )
220
- . build ( )
221
- . expect ( "we only set options that can't cause a build failure" ) ;
222
- jwalk:: Parallelism :: RayonExistingPool {
223
- pool : pool. into ( ) ,
224
- busy_timeout : None ,
225
- }
226
- } ,
227
- ) ,
228
- }
229
- }
230
- }
231
-
232
- /// Instantiate a new directory iterator which will not skip hidden files, with the given level of `parallelism`.
233
- ///
234
- /// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
235
- pub fn walkdir_new ( root : & Path , parallelism : Parallelism , precompose_unicode : bool ) -> WalkDir {
236
- WalkDir {
237
- inner : WalkDirImpl :: new ( root)
238
- . skip_hidden ( false )
239
- . parallelism ( parallelism. into ( ) )
240
- . into ( ) ,
241
- precompose_unicode,
242
- }
243
- }
244
-
245
- /// Instantiate a new directory iterator which will not skip hidden files and is sorted
246
- ///
247
- /// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
248
- pub fn walkdir_sorted_new ( root : & Path , parallelism : Parallelism , precompose_unicode : bool ) -> WalkDir {
249
- WalkDir {
250
- inner : WalkDirImpl :: new ( root)
251
- . skip_hidden ( false )
252
- . sort ( true )
253
- . parallelism ( parallelism. into ( ) )
254
- . into ( ) ,
255
- precompose_unicode,
256
- }
257
- }
258
-
259
- type DirEntryIterImpl = DirEntryIterGeneric < ( ( ) , ( ) ) > ;
260
-
261
- /// The Iterator yielding directory items
262
- pub type DirEntryIter = super :: walkdir_precompose:: WalkDirIter < DirEntryIterImpl , DirEntryImpl , jwalk:: Error > ;
263
- }
264
-
265
- ///
266
- #[ cfg( all( feature = "walkdir" , not( feature = "fs-walkdir-parallel" ) ) ) ]
145
+ #[ cfg( feature = "walkdir" ) ]
267
146
pub mod walkdir {
268
147
use std:: borrow:: Cow ;
269
148
use std:: ffi:: OsStr ;
@@ -338,8 +217,21 @@ pub mod walkdir {
338
217
///
339
218
/// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
340
219
pub fn walkdir_sorted_new ( root : & Path , _: Parallelism , precompose_unicode : bool ) -> WalkDir {
220
+ fn ft_to_number ( ft : std:: fs:: FileType ) -> usize {
221
+ if ft. is_file ( ) {
222
+ 1
223
+ } else {
224
+ 2
225
+ }
226
+ }
341
227
WalkDir {
342
- inner : WalkDirImpl :: new ( root) . sort_by_file_name ( ) . into ( ) ,
228
+ inner : WalkDirImpl :: new ( root)
229
+ . sort_by ( |a, b| {
230
+ ft_to_number ( a. file_type ( ) )
231
+ . cmp ( & ft_to_number ( b. file_type ( ) ) )
232
+ . then_with ( || a. file_name ( ) . cmp ( b. file_name ( ) ) )
233
+ } )
234
+ . into ( ) ,
343
235
precompose_unicode,
344
236
}
345
237
}
@@ -348,7 +240,7 @@ pub mod walkdir {
348
240
pub type DirEntryIter = super :: walkdir_precompose:: WalkDirIter < walkdir:: IntoIter , DirEntryImpl , walkdir:: Error > ;
349
241
}
350
242
351
- #[ cfg( any ( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
243
+ #[ cfg( feature = "walkdir" ) ]
352
244
pub use self :: walkdir:: { walkdir_new, walkdir_sorted_new, WalkDir } ;
353
245
354
246
/// Prepare open options which won't follow symlinks when the file is opened.
0 commit comments