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

Tracking Issue for Vec::pop_if #122741

Closed
3 of 5 tasks
avandesa opened this issue Mar 19, 2024 · 11 comments · Fixed by #135488
Closed
3 of 5 tasks

Tracking Issue for Vec::pop_if #122741

avandesa opened this issue Mar 19, 2024 · 11 comments · Fixed by #135488
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Milestone

Comments

@avandesa
Copy link
Contributor

avandesa commented Mar 19, 2024

Feature gate: #![feature(vec_pop_if)]

This feature adds the Vec::pop_if method, which takes a predicate, evaluates it with the last element in the Vec if present, and returns the item if the predicate returns true. This makes it possible to conditionally remove the last element without the use of unwrap.

Public API

impl<T> Vec<T> {
    pub fn pop_if(&mut self, f: impl FnOnce(&mut T) -> bool) -> Option<T>;
}

Steps / History

Unresolved Questions

Footnotes

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

@avandesa avandesa added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Mar 19, 2024
@avandesa avandesa mentioned this issue Mar 26, 2024
2 tasks
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 27, 2024
Implement `Vec::pop_if`

This PR adds `Vec::pop_if` to the public API, behind the `vec_pop_if` feature.

```rust
impl<T> Vec<T> {
    pub fn pop_if<F>(&mut self, f: F) -> Option<T>
        where F: FnOnce(&mut T) -> bool;
}
```

Tracking issue: rust-lang#122741

## Open questions

- [ ] Should the first unit test be split up?
- [ ] I don't see any guidance on ordering of methods in impl blocks, should I move the method elsewhere?
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 27, 2024
Rollup merge of rust-lang#123107 - avandesa:vec_pop_if, r=joboet

Implement `Vec::pop_if`

This PR adds `Vec::pop_if` to the public API, behind the `vec_pop_if` feature.

```rust
impl<T> Vec<T> {
    pub fn pop_if<F>(&mut self, f: F) -> Option<T>
        where F: FnOnce(&mut T) -> bool;
}
```

Tracking issue: rust-lang#122741

## Open questions

- [ ] Should the first unit test be split up?
- [ ] I don't see any guidance on ordering of methods in impl blocks, should I move the method elsewhere?
@GrigorenkoPV
Copy link
Contributor

Found a piece of code in the wild that could benefit (get rid of an .unwrap()) with this API stabilized:
https://github.com/XAMPPRocky/tokei/blob/edbd5d5cbb0b7ea0081df0265a8ae6e7742a5051/src/language/syntax.rs#L663-L668

Also I myself have found this feature to be useful on couple occasions in personal projects.

This does not look too controversial, so maybe let's stabilize it?

The corresponding VecDeque methods can be added and stabilized later without blocking this.

I don't think I have right to start an FCP, so I will just open a PR instead.

@tgross35
Copy link
Contributor

Docs should probably mention that the function does not get run if the vector is empty.

I don't think I have right to start an FCP, so I will just open a PR instead.

You can add the I-libs-api-nominated label with rustbot

@GrigorenkoPV
Copy link
Contributor

You can add the I-libs-api-nominated label with rustbot

Thanks, that makes sense

@rustbot label +I-libs-api-nominated

Docs should probably mention that the function does not get run if the vector is empty.

True, it might have side-effects, so it's probably worth mentioning. I will adjust my PR.

@rustbot rustbot added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jan 17, 2025
@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Jan 21, 2025
@Amanieu
Copy link
Member

Amanieu commented Jan 21, 2025

@rfcbot merge

@rfcbot
Copy link
Collaborator

rfcbot commented Jan 21, 2025

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

No concerns currently listed.

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 Jan 21, 2025
@GrigorenkoPV
Copy link
Contributor

GrigorenkoPV commented Jan 22, 2025

@rfcbot concern impl-trait-syntax (well, I guess not enough rights)

Is there a reason why we use f: F ... where F: Fn... syntax instead of just f: impl Fn...? I recall having (or reading) a discussion about this with a reviewer like a year ago, and IIRC, they said that impl Fn is generally preferred, but since this syntax wasn't there since Rust 1.0, some APIs got stuck with an explicit generic argument.

Should I change this from

impl<T> Vec<T> {
    pub fn pop_if<F>(&mut self, f: F) -> Option<T>
        where F: FnOnce(&mut T) -> bool;
}

to

impl<T> Vec<T> {
    pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T>;
}

in stabilization PR (#135488)?

@Amanieu
Copy link
Member

Amanieu commented Jan 22, 2025

Yes, please do. It's always backwards-compatible to change an impl Trait to a generic parameter, but not the other way around.

@GrigorenkoPV
Copy link
Contributor

Done

@fmease
Copy link
Member

fmease commented Jan 23, 2025

It's always backwards-compatible to change an impl Trait to a generic parameter

This is not true: Such a change breaks call/usage sites where the generic argument list is specified (aka turbofished call/usage sites).

However, in the case of Vec::pop_if which doesn't have own generic parameters, such code is incredibly unlikely to be written in practice (only macro-generated code would realistically contain usages like v.pop_if::<>(f)).

@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 29, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Jan 29, 2025

🔔 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 Feb 8, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Feb 8, 2025

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.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Feb 8, 2025
Urgau added a commit to Urgau/rust that referenced this issue Feb 9, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 9, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 10, 2025
Rollup merge of rust-lang#135488 - GrigorenkoPV:vec_pop_if, r=jhpratt

Stabilize `vec_pop_if`

Tracking issue: rust-lang#122741

FCP completed in rust-lang#122741 (comment)
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Feb 13, 2025
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this issue Mar 11, 2025
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this issue Mar 11, 2025
@cuviper cuviper added this to the 1.86.0 milestone Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants