Skip to content

Commit c4de01f

Browse files
committed
Associate WalkDir lifetime with WalkDirOptions, sorter, and IntoIter
Narrows lifetime of callbacks for sort_by and sort_by_key so they no longer need to be 'static.
1 parent 4f26be4 commit c4de01f

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

src/lib.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ pub type Result<T> = ::std::result::Result<T, Error>;
231231
/// Note that when following symbolic/soft links, loops are detected and an
232232
/// error is reported.
233233
#[derive(Debug)]
234-
pub struct WalkDir {
235-
opts: WalkDirOptions,
234+
pub struct WalkDir<'slf> {
235+
opts: WalkDirOptions<'slf>,
236236
root: PathBuf,
237237
}
238238

239-
struct WalkDirOptions {
239+
struct WalkDirOptions<'slf> {
240240
follow_links: bool,
241241
follow_root_links: bool,
242242
max_open: usize,
@@ -247,14 +247,14 @@ struct WalkDirOptions {
247247
dyn FnMut(&DirEntry, &DirEntry) -> Ordering
248248
+ Send
249249
+ Sync
250-
+ 'static,
250+
+ 'slf,
251251
>,
252252
>,
253253
contents_first: bool,
254254
same_file_system: bool,
255255
}
256256

257-
impl fmt::Debug for WalkDirOptions {
257+
impl<'slf> fmt::Debug for WalkDirOptions<'slf> {
258258
fn fmt(
259259
&self,
260260
f: &mut fmt::Formatter<'_>,
@@ -278,7 +278,7 @@ impl fmt::Debug for WalkDirOptions {
278278
}
279279
}
280280

281-
impl WalkDir {
281+
impl<'slf> WalkDir<'slf> {
282282
/// Create a builder for a recursive directory iterator starting at the
283283
/// file path `root`. If `root` is a directory, then it is the first item
284284
/// yielded by the iterator. If `root` is a file, then it is the first
@@ -416,7 +416,7 @@ impl WalkDir {
416416
/// ```
417417
pub fn sort_by<F>(mut self, cmp: F) -> Self
418418
where
419-
F: FnMut(&DirEntry, &DirEntry) -> Ordering + Send + Sync + 'static,
419+
F: FnMut(&DirEntry, &DirEntry) -> Ordering + Send + Sync + 'slf,
420420
{
421421
self.opts.sorter = Some(Box::new(cmp));
422422
self
@@ -438,7 +438,7 @@ impl WalkDir {
438438
/// ```
439439
pub fn sort_by_key<K, F>(self, mut cmp: F) -> Self
440440
where
441-
F: FnMut(&DirEntry) -> K + Send + Sync + 'static,
441+
F: FnMut(&DirEntry) -> K + Send + Sync + 'slf,
442442
K: Ord,
443443
{
444444
self.sort_by(move |a, b| cmp(a).cmp(&cmp(b)))
@@ -533,11 +533,11 @@ impl WalkDir {
533533
}
534534
}
535535

536-
impl IntoIterator for WalkDir {
536+
impl<'slf> IntoIterator for WalkDir<'slf> {
537537
type Item = Result<DirEntry>;
538-
type IntoIter = IntoIter;
538+
type IntoIter = IntoIter<'slf>;
539539

540-
fn into_iter(self) -> IntoIter {
540+
fn into_iter(self) -> IntoIter<'slf> {
541541
IntoIter {
542542
opts: self.opts,
543543
start: Some(self.root),
@@ -563,9 +563,9 @@ impl IntoIterator for WalkDir {
563563
/// [`WalkDir`]: struct.WalkDir.html
564564
/// [`.into_iter()`]: struct.WalkDir.html#into_iter.v
565565
#[derive(Debug)]
566-
pub struct IntoIter {
566+
pub struct IntoIter<'slf> {
567567
/// Options specified in the builder. Depths, max fds, etc.
568-
opts: WalkDirOptions,
568+
opts: WalkDirOptions<'slf>,
569569
/// The start path.
570570
///
571571
/// This is only `Some(...)` at the beginning. After the first iteration,
@@ -676,7 +676,7 @@ enum DirList {
676676
Closed(vec::IntoIter<Result<DirEntry>>),
677677
}
678678

679-
impl Iterator for IntoIter {
679+
impl Iterator for IntoIter<'_> {
680680
type Item = Result<DirEntry>;
681681
/// Advances the iterator and returns the next value.
682682
///
@@ -734,7 +734,7 @@ impl Iterator for IntoIter {
734734
}
735735
}
736736

737-
impl IntoIter {
737+
impl IntoIter<'_> {
738738
/// Skips the current directory.
739739
///
740740
/// This causes the iterator to stop traversing the contents of the least
@@ -1002,7 +1002,7 @@ impl IntoIter {
10021002
}
10031003
}
10041004

1005-
impl iter::FusedIterator for IntoIter {}
1005+
impl iter::FusedIterator for IntoIter<'_> {}
10061006

10071007
impl DirList {
10081008
fn close(&mut self) {
@@ -1057,7 +1057,7 @@ pub struct FilterEntry<I, P> {
10571057
predicate: P,
10581058
}
10591059

1060-
impl<P> Iterator for FilterEntry<IntoIter, P>
1060+
impl<P> Iterator for FilterEntry<IntoIter<'_>, P>
10611061
where
10621062
P: FnMut(&DirEntry) -> bool,
10631063
{
@@ -1086,12 +1086,12 @@ where
10861086
}
10871087
}
10881088

1089-
impl<P> iter::FusedIterator for FilterEntry<IntoIter, P> where
1089+
impl<P> iter::FusedIterator for FilterEntry<IntoIter<'_>, P> where
10901090
P: FnMut(&DirEntry) -> bool
10911091
{
10921092
}
10931093

1094-
impl<P> FilterEntry<IntoIter, P>
1094+
impl<P> FilterEntry<IntoIter<'_>, P>
10951095
where
10961096
P: FnMut(&DirEntry) -> bool,
10971097
{

0 commit comments

Comments
 (0)