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

Draft: Generalize dirEntries to take a DirEntry predicate as parameter #8586

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

nordlow
Copy link
Contributor

@nordlow nordlow commented Oct 5, 2022

The current state of dirEntries is not generic enough because it cannot traverse any directory tree X (via SpanMode.depth) without having to traverse all sub-directories of X such as .git directory trees.

Adding a template parameter

alias pred  = (scope DirEntry entry) => true

to dirEntries alleviates this problem.

No tests yet and no template restriction enabled yet because I'm uncertain if we should require the pred to take the DirEntry by ref or not given that DirEntry.sizeof is 156.

Moreover, it's unfortunate that the template parameter useDIP1000 was recently added to

dirEntries(bool useDIP1000 = dip1000Enabled, alias pred = (scope DirEntry entry) => true)

as I presume that pred is gonna be a much more common non-default template parameter than then useDIP1000 is gonna be.

Shall we add a new function say treeEntries, dirTreeEntries, dirEntriesTree or dirEntriesFiltered or whatever instead so we can make pred the first template parameter?

Note that DirEntry member

@property bool isDir() scope

is non-const on posix so that pred cannot be

(const scope DirEntry entry) => true

in the general case. Will add documentation and tests when we have settled on solutions to these issues.

For the corresponding filtering in Rust see https://crates.io/crates/walkdir section "Example: skip hidden files and directories efficiently on unix".

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs documentation

@nordlow nordlow changed the title Generalize dirEntries to take a pred as parameter Generalize dirEntries to take a DirEntrye predicate as parameter Oct 5, 2022
@nordlow nordlow changed the title Generalize dirEntries to take a DirEntrye predicate as parameter Generalize dirEntries to take a DirEntry predicate as parameter Oct 5, 2022
@dukc
Copy link
Contributor

dukc commented Oct 5, 2022

Moreover, it's unfortunate that the template parameter useDIP1000 was recently added

I don't think you need to worry about breakage when moving that to last position of the parameter list. The documentation says "set automatically - do not touch", it asserts null if set against what the compiler switch says, and it has existed for only a short while.

@nordlow
Copy link
Contributor Author

nordlow commented Oct 5, 2022

I don't think you need to worry about breakage when moving that to last position of the parameter list. The documentation says "set automatically - do not touch", it asserts null if set against what the compiler switch says, and it has existed for only a short while.

Great. Will adjust then.

std/file.d Outdated Show resolved Hide resolved
@atilaneves
Copy link
Contributor

Needs tests.

@nordlow nordlow changed the title Generalize dirEntries to take a DirEntry predicate as parameter Draft: Generalize dirEntries to take a DirEntry predicate as parameter Oct 5, 2022
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @nordlow! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8586"

@nordlow
Copy link
Contributor Author

nordlow commented Oct 6, 2022

Lots of seemingly unrelated CI errors...

std/file.d Outdated Show resolved Hide resolved
@rikkimax
Copy link
Contributor

The predicate should not be a template parameter.

It increases symbol count, and prevents configurability at runtime.

@thewilsonator
Copy link
Contributor

@nordlow if you could add some documentation and a chengelog entry to this we can definitely merge this

@LightBender LightBender added the Salvage This PR needs work, but we want to keep it and it can be salvaged. label Oct 27, 2024
@nordlow
Copy link
Contributor Author

nordlow commented Oct 27, 2024

Great. What shall the default value of the predicate be? The one that I provided with DirEntry passed as ref or simply pred = void?

@rikkimax
Copy link
Contributor

If null, old behavior. If non-null call it.

@nordlow
Copy link
Contributor Author

nordlow commented Oct 27, 2024

If null, old behavior. If non-null call it.

I believe it's more common practise to default predicate lambda parameters to void instead of null. With defaulting to void

we can use static if (is(pred ==)).

Alternatively

alias pred = (scope ref DirEntry entry) => true

could be used.

@rikkimax
Copy link
Contributor

If null, old behavior. If non-null call it.

I believe it's more common practise to default predicate lambda parameters to void instead of null. With defaulting to void

we can use static if (is(pred ==)).

Alternatively

alias pred = (scope ref DirEntry entry) => true

could be used.

All of which are related to template parameters. My comment was about converting it to a function parameter.

@nordlow
Copy link
Contributor Author

nordlow commented Oct 27, 2024

If null, old behavior. If non-null call it.
All of which are related to template parameters. My comment was about converting it to a function parameter.

Ahh. I see.

@nordlow
Copy link
Contributor Author

nordlow commented Oct 27, 2024

Are we ok with using

bool delegate(scope ref DirEntry entry) pred;

as a run-time parameter and store this in DirIteratorImpl as

private struct DirIteratorImpl
{
    bool delegate(scope ref DirEntry entry) pred;

    this(bool delegate(scope ref DirEntry entry) pred)
    {
        this.pred = pred;
    }
    ....

be used?

@rikkimax
Copy link
Contributor

It would need to be @safe (none others are needed here I think).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Salvage This PR needs work, but we want to keep it and it can be salvaged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants