Skip to content

Tracking Issue for implementing DoubleEndedIterator for Ancestors #153070

@asder8215

Description

@asder8215

Feature gate: #![feature(reverse_ancestor)]

This is a tracking issue for rust-lang/libs-team#745.

It implements the DoubleEndedIterator trait on the Ancestor in std::path, so that it's possible to traverse a path both in the back direction through path.ancestors()and in the forward direction through path.ancestors().rev(). Currently, the latter is not supported because Ancestors doesn't have a DoubleEndedIterator trait implemented.

Public API Example

With this ACP you can do something like:

use std::path::Path;
let path = Path::new("/foo/bar/text.txt");

for ancestor in path.ancestors() {
    println!("{:?}", ancestor);
}

for descendant in path.ancestors().rev() {
    println!("{:?}", descendant);
} 

Iterating through path.ancestors() would give:

"/foo/bar/text.txt"
"/foo/bar"
"/foo"
"/"

And iterating through path.ancestors().rev() would give:

"/"
"/foo"
"/foo/bar"
"/foo/bar/text.txt"

The benefit of this is that you don't need to do something like path.ancestors().collect()/path.ancestors().take(#).collect() to collect the ancestor paths into a Vec<Path> and be able to traverse the path in reverse direction (avoiding an allocation essentially).

Steps / History

(Remember to update the S-tracking-* label when checking boxes.)

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

Metadata

Metadata

Assignees

Labels

C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions