Skip to content

Collecting into a PathBuf from Iterator<Item=Component> is hard. #41866

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Mark-Simulacrum opened this issue May 9, 2017 · 3 comments
Closed
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Mark-Simulacrum
Copy link
Member

See discussion here: #41531.

To summarize, the following should work, but currently doesn't. It's likely that implementing impl<P: AsRef<OsStr>> FromIterator<P> for PathBuf would solve this, but I haven't tested.

use std::path::{PathBuf, Path};

fn main() {
    let path = Path::new("a/b/c/d");
    println!("{:?}", path.components().filter(|c| c.as_os_str() != "b").collect::<PathBuf>());
}
@Mark-Simulacrum Mark-Simulacrum added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 9, 2017
@ollie27
Copy link
Member

ollie27 commented May 9, 2017

The following already works fine:

use std::path::{Component, PathBuf, Path};

fn main() {
    let path = Path::new("a/b/c/d");
    println!("{:?}", path.components().map(Component::as_os_str).filter(|c| *c != "b").collect::<PathBuf>());
}

Maybe Component should implement AsRef<Path> as it already implements AsRef<OsStr> and OsStr implements AsRef<Path>.

@Mark-Simulacrum
Copy link
Member Author

Component implementing AsRef<Path> seems like a good idea.

@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed I-papercut labels Jul 27, 2017
@dtolnay dtolnay added C-feature-accepted Category: A feature request that has been accepted pending implementation. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 19, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 19, 2017

Sounds good to me, let's do it.

kennytm added a commit to kennytm/rust that referenced this issue Jan 12, 2018
…xcrichton

Implement AsRef<Path> for Component

Fixes rust-lang#41866
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants