-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add std::path::Path::ancestors #48420
Conversation
This hasn’t been assigned a reviewer, so assigning first person from T-libs, I can think of r? @BurntSushi |
src/libstd/path.rs
Outdated
/// The iterator will yield the [Path] that is returned if the [`parent`] method is used zero | ||
/// or more times. That means, the iterator will yield `&self`, `&self.parent().unwrap()`, | ||
/// `&self.parent().unwrap().parent().unwrap()` and so on. If the [`parent`] method returns | ||
/// [`None`], the iterator will do likewise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to explicitly clarify here that the iterator returned will always yield at least one value.
I do agree with the utility of this function, and I've certainly had to write this iterator out by hand before. So I think this seems fine to me, but let's see if anyone has any objections before merging. cc @rust-lang/libs |
src/libstd/path.rs
Outdated
/// [`parents`]: struct.Path.html#method.parents | ||
/// [`Path`]: struct.Path.html | ||
#[derive(Copy, Clone, Debug)] | ||
#[unstable(feature = "path_parents", issue = "0")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line raises two open questions for me:
- Is the feature name appropriate?
- What issue number can I use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature name seems OK to me, as long as it is distinct from any other feature name. :-)
I believe that if we decide to merge this, you need to create a tracking issue for stabilizing this feature, and that's the number that goes here.
I also think there is some docs where features are documented. But I don't know where it is.
Sounds like a great idea to me! We've actually got basically this method already in Cargo |
The name |
This seems like a weak justification for choosing |
I like these arguments. I will prepare a commit that changes the name to |
maybe |
I think, |
well, I think the name should be more or less generic, because it lists all paths, documentation should clarify the meaning though, another option |
The method does not list all paths at all. It lists all paths that are ancestors of
Calling
while all paths would (in my understanding of the term) be something like
In my opinion, something like @eav I really appreciate your ideas! But what is so wrong with |
Either |
@teiesti Could you squash this down to one commit? Then I think this should be good to go! |
Squashed commit of the following: commit 1b5d55e26f667b1a25c83c5db0cbb072013a5122 Author: Tobias Stolzmann <[email protected]> Date: Wed Feb 28 00:06:15 2018 +0100 Bugfix commit 4265c2db0b0aaa66fdeace5d329665fd2d13903a Author: Tobias Stolzmann <[email protected]> Date: Tue Feb 27 22:59:12 2018 +0100 Rename std::path::Path::parents into std::path::Path::ancestors commit 2548e4b14d377d20adad0f08304a0dd6f8e48e23 Author: Tobias Stolzmann <[email protected]> Date: Tue Feb 27 12:50:37 2018 +0100 Add tracking issue commit 3e2ce51a6eea0e39af05849f76dd2cefd5035e86 Author: Tobias Stolzmann <[email protected]> Date: Mon Feb 26 15:05:15 2018 +0100 impl FusedIterator for Parents commit a7e096420809740311e19d963d4aba6df77be2f9 Author: Tobias Stolzmann <[email protected]> Date: Mon Feb 26 14:38:41 2018 +0100 Clarify that the iterator returned will yield at least one value commit 796a36ea203cd197cc4c810eebd21c7e3433e6f1 Author: Tobias Stolzmann <[email protected]> Date: Thu Feb 22 14:01:21 2018 +0100 Fix examples commit e279383b21f11c97269cb355a5b2a0ecdb65bb0c Author: Tobias Stolzmann <[email protected]> Date: Thu Feb 22 04:47:24 2018 +0100 Add std::path::Path::parents
@BurntSushi Done. |
for me
|
@bors r+ |
📌 Commit b9e9b4a has been approved by |
Tracking issue: #48581
This pull request adds a method called
parents
ancestors
tostd::path::Path
.parents
ancestors
is a generalized version ofparent
. Whileparent
returns the path without its final component,parents
ancestors
returns an iterator that yields every ancestor. This includes the path itself, the parent, the grandparent and so on. This is done by repeatedly callingparent
. In caseparent
returnsNone
, the iterator returned byparents
ancestors
does likewise.Why is such a method useful?
A method like
parents
ancestors
is especially useful, if a task has to be done for the current directory and every directory up the tree. (Think of Cargo searching for the Cargo.toml!)Why does the iterator yield the original path instead of starting with the parent?
It is easier to skip the original path with
.skip(1)
than it is to add the original path if necessary.Why is the method calledparents
instead ofancestors
?When sorted ,
parents
comes right afterparent
. It is therefore grouped with its more specific counterpart within the documentation.Why is the method called
ancestors
instead ofparents
?ancestors
.Ancestor
yields the current path. It is more reasonable that the current path is a "null ancestor" than a "null parent".parents
might be misleading.Two more notes: