Skip to content

Conversation

Stupremee
Copy link
Contributor

This PR stabilizes the peekable_next_if feature

Resolves #72480

@rust-highfive
Copy link
Contributor

r? @withoutboats

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 13, 2020
@Stupremee
Copy link
Contributor Author

r? @jyn514

@rust-highfive rust-highfive assigned jyn514 and unassigned withoutboats Dec 13, 2020
@jyn514
Copy link
Member

jyn514 commented Dec 13, 2020

I'm not on T-libs, I shouldn't be the reviewer. r? @dtolnay maybe since they reviewed the initial PR, but it will need FCP either way.

@rust-highfive rust-highfive assigned dtolnay and unassigned jyn514 Dec 13, 2020
@jyn514 jyn514 added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-iterators Area: Iterators labels Dec 13, 2020
@dtolnay
Copy link
Member

dtolnay commented Dec 13, 2020

@rust-lang/libs:
@rfcbot fcp merge

These two methods on std::iter::Peekable:

  impl<I: Iterator> Peekable<I> {
+     pub fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item>;

+     pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item>
+     where
+         T: ?Sized,
+         I::Item: PartialEq<T>;
  }
let alnum = peekable.next_if(char::is_ascii_alphanumeric);

// equivalent to:
let alnum = match peekable.peek() {
    Some(ch) if ch.is_ascii_alphanumeric() => Some(peekable.next().unwrap()),
    _ => None,
};

https://doc.rust-lang.org/nightly/std/iter/struct.Peekable.html#method.next_if
https://doc.rust-lang.org/nightly/std/iter/struct.Peekable.html#method.next_if_eq

@rfcbot
Copy link

rfcbot commented Dec 13, 2020

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Dec 13, 2020
@m-ou-se
Copy link
Member

m-ou-se commented Dec 14, 2020

@rfcbot concern mut

Now that Peekable::peek_mut also exists (#77491), I'm wondering if next_if should provide a &mut Item rather than a &Item. (To not make the same mistake as with Vec::retain, which in hindsight could've/should've given a &mut but only gives a &.) What do you all think?

@jyn514
Copy link
Member

jyn514 commented Dec 14, 2020

Why would you need to mutate the item?

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2021
@KodrAus
Copy link
Contributor

KodrAus commented Jan 21, 2021

I'm wondering if next_if should provide a &mut Item rather than a &Item

Oh that's an interesting philosophical question... Should we always try offer up the most exclusive reference we can?

Why would you need to mutate the item?

I wonder if we asked the same question about Vec::retain. The problem there is that in order to both retain and mutate you have to iterate the collection twice, isn't it?

With next_if it looks like there isn't much difference between next_if(filter).map(mutate) and a next_if_mut(filter_and_mutate_in_place). Since we stash the value in peeked, the semantics of next_if(|v| { mutate(v); false }) also seem a bit strange.

But I think is a shared reference sufficient when we could use an exclusive one? is a good question we should keep asking!

@KodrAus
Copy link
Contributor

KodrAus commented Jan 21, 2021

A next_if_mut method also doesn’t seem too offensive to me if we did want one. At least when reviewing code the _mut suffix would suggest there’s something more going on than just filtering and yielding values.

@KodrAus
Copy link
Contributor

KodrAus commented Jan 21, 2021

@m-ou-se Do you feel strongly that next_if should accept a &mut item, or are you satisfied with some combination of .peek_mut(mutate).next_if(condition_after_mutate) or .peek().next_if(condition).map(mutate), with a possible next_if_mut alternative introduced later if it proves useful?

@m-ou-se
Copy link
Member

m-ou-se commented Jan 21, 2021

That sounds good to me.

@rfcbot resolve mut

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jan 21, 2021
@rfcbot
Copy link

rfcbot commented Jan 21, 2021

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jan 31, 2021
@rfcbot
Copy link

rfcbot commented Jan 31, 2021

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jan 31, 2021
@dtolnay
Copy link
Member

dtolnay commented Feb 5, 2021

@bors r+

@bors
Copy link
Collaborator

bors commented Feb 5, 2021

📌 Commit ceda547 has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 5, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Feb 5, 2021

Note: If #81810 fails but #81792 (which is in the queue right after) gets merged, this will need to be updated to since = "1.52.0" instead.

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 6, 2021
Rollup of 7 pull requests

Successful merges:

 - rust-lang#80011 (Stabilize `peekable_next_if`)
 - rust-lang#81580 (Document how `MaybeUninit<Struct>` can be initialized.)
 - rust-lang#81610 (BTreeMap: make Ord bound explicit, compile-test its absence)
 - rust-lang#81664 (Avoid a hir access inside get_static)
 - rust-lang#81675 (Make rustdoc respect `--error-format short` in doctests)
 - rust-lang#81753 (Never MIR inline functions with a different instruction set)
 - rust-lang#81795 (Small refactor with Iterator::reduce)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit cc882fc into rust-lang:master Feb 6, 2021
@rustbot rustbot added this to the 1.51.0 milestone Feb 6, 2021
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-iterators Area: Iterators disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking Issue for peekable_next_if