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

Add std::str::Chars remaining() function: #12395 #12431

Closed
wants to merge 3 commits into from
Closed

Add std::str::Chars remaining() function: #12395 #12431

wants to merge 3 commits into from

Conversation

mneumann
Copy link
Contributor

No description provided.

@mneumann
Copy link
Contributor Author

#12395

@alexcrichton
Copy link
Member

I'm a little worried that this doesn't update any of

  • rev_chars
  • char_indices
  • char_indices_rev
  • split
  • splitn
  • words
  • ...

The string class has a fair number of iterators. Is there a reason that the chars one should be the only one blessed with this function?

@mneumann
Copy link
Contributor Author

Will hack that tomorrow, too late here atm :)

@alexcrichton
Copy link
Member

I caution you before too much work is put in to this. I pointed out those iterators as an example of where this function may not fit into the library. I would find it a little annoying to have to define remaining for all iterators on strings if we ever add new ones.

I think iterators are really good at doing just that, iterating. Conflating other state with specific iterators reduces the utility of other ones, so we'll want to approach this carefully.

@mneumann
Copy link
Contributor Author

The thing with strings is that it is pretty hard to safely iterate over them on your own as you need to use char_range_at(). Also by using iterators, I assume that the range checks could be optimized away one day (iterators could be implemented unsafe underneath). remaining also allows us to alternate different iterators:

s.chars(|c| if c == ' ' { break }).remaining().split(',', |tok| {println!("{}", tok)});

@alexcrichton
Copy link
Member

Sadly I don't think that iterator will quite work how you expect, because calling chars doesn't actually exhaust the iterator. The return value of remaining() will be the entire string for the code you provided.

@mneumann
Copy link
Contributor Author

Ooops, of course not. Though this should work:

let mut iter = s.chars();
for c in iter { if c == ' ' { break } }
iter.remaining().split(',', |tok| {println!("{}", tok)});

Or if we'd have each() again as we had some time ago, this would be something like this:

s.chars().each(|c| { c != ' ' }).remaining().split(',', |tok| {println!("{}", tok)});

@alexcrichton
Copy link
Member

Closing due to a lack of activity. This may want to get fleshed out and agreed upon first before opening another PR.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
fix: Fix completions disappearing when typing two keys in quick succession

With this PR we now retry requests if they get cancelled due to document changes.

This fixes the completions problem we have where completions seem to randomly disappear, see https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/Completions.20not.20always.20appearing

Fixes rust-lang/rust-analyzer#10187
Fixes rust-lang/rust-analyzer#7560
Fixes rust-lang/rust-analyzer#12153
flip1995 pushed a commit to flip1995/rust that referenced this pull request Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants