Allow FilterEntry::filter_entry to accept a different predicate type - #214
Open
amitmishra11 wants to merge 1 commit into
Open
Allow FilterEntry::filter_entry to accept a different predicate type#214amitmishra11 wants to merge 1 commit into
amitmishra11 wants to merge 1 commit into
Conversation
Previously FilterEntry<I, P>::filter_entry took a predicate of the same
type P as the outer FilterEntry, making it impossible to chain two
filter_entry calls with distinct closures. The compiler would reject
code like:
WalkDir::new(.)
.into_iter()
.filter_entry(|e| e.file_name() != foo)
.filter_entry(|e| e.file_name() != bar)
because the two closures are different types.
Fix this by:
- Introducing a SkipCurrentDir trait that abstracts over IntoIter and
FilterEntry so that the blanket Iterator impl for FilterEntry<I, P>
can use a generic I.
- Changing FilterEntry::filter_entry to introduce a fresh type
parameter Q for the new predicate instead of reusing the outer P.
- Generalising the Iterator and FusedIterator impls on FilterEntry
to work over any I: SkipCurrentDir rather than only IntoIter.
Fixes BurntSushi#130.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #130.
Bug
FilterEntry<I, P>::filter_entryaccepted a new predicate of exactlythe same type
Pas the one already stored in theFilterEntry. Sinceevery Rust closure has a unique, anonymous type, this made it impossible
to chain two
filter_entrycalls with two different closures:Users were forced to combine the two predicates into one, or use boxed
closures / function pointers.
The second comment in #130 shows the confusing compiler error this
produces in practice.
Fix
Introduced a public
SkipCurrentDirtrait that abstracts theskip_current_dircapability shared byIntoIterandFilterEntry.Then:
Iteratorimpl forFilterEntry<I, P>is generalized to workfor any
I: SkipCurrentDirinstead of onlyIntoIter.FilterEntry::filter_entrynow introduces a fresh type parameterQfor the new predicate, returning
FilterEntry<Self, Q>, matching thesignature of
IntoIter::filter_entry.This is a minor semver-compatible addition (new public trait
SkipCurrentDir). The only theoretical breakage is using an emptyturbofish on
FilterEntry::filter_entry::<>to observe the former lackof type parameters, which is not a realistic usage pattern.
Test
Added
filter_entry_chainedinsrc/tests/recursive.rsthat chainstwo
filter_entrycalls with distinct closure types and verifies thatboth predicates are applied correctly.