Skip to content

std::str::SplitWhitespace does not implement Clone #41655

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
phaylon opened this issue Apr 30, 2017 · 1 comment
Closed

std::str::SplitWhitespace does not implement Clone #41655

phaylon opened this issue Apr 30, 2017 · 1 comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@phaylon
Copy link
Contributor

phaylon commented Apr 30, 2017

It seems currently impossible to iterate over a &str split by whitespace multiple times, due to Clone not being available in any solution.

Given the code

fn takes_iter<'i, I>(_: I) where I: Iterator<Item = &'i str> + Clone {}

fn main() {
    takes_iter("foo bar".split_whitespace());
}

I get the following error

rustc 1.19.0-nightly (afa1240e5 2017-04-29)
error[E0277]: the trait bound `std::str::SplitWhitespace<'_>: std::clone::Clone` is not satisfied
 --> <anon>:4:5
  |
4 |     takes_iter("foo bar".split_whitespace());
  |     ^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `std::str::SplitWhitespace<'_>`
  |
  = note: required by `takes_iter`

error: aborting due to previous error

Other workarounds ("foo bar".split(char::is_whitespace) and "foo bar".split(|c: char| c.is_whitespace()) also run into Clone issues.

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 30, 2017
@bluss
Copy link
Member

bluss commented May 1, 2017

Something like "foo bar".split(&[' ', '\n'][..]) or "foo bar".split(char::is_whitespace as fn(_) -> bool) should work too, since the pattern is clonable. Unless there's an issue with that function pointer, hope not..

bors added a commit that referenced this issue May 10, 2017
impl Clone for .split_whitespace()

Use custom closure structs for the predicates so that the iterator's
clone can simply be derived. This should also reduce virtual call
overhead by not using function pointers.

Fixes #41655
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
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