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 LinkedList::{retain,retain_mut} #250

Closed
TennyZhuang opened this issue Jul 28, 2023 · 1 comment
Closed

Add LinkedList::{retain,retain_mut} #250

TennyZhuang opened this issue Jul 28, 2023 · 1 comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@TennyZhuang
Copy link

TennyZhuang commented Jul 28, 2023

Proposal

Problem statement

It would be convenient if LinkedList had retain/retain_mut methods just like many other collections. We can migrate between different collections easily.

Motivating examples or use cases

When implementing MVCC transactions in database, it's very common to store the active transactions as a list. They'll be garbage collected by a monotonic version.

txn.version > gc_versionlet mut active_txns = LinkedList::new();;
let mut gc_version = 0;
// ...

while gc_triggerer.next() {
    active_txns.retain(|&txn| txn.version > gc_version);
}

In fact, there are similar use cases like other collections.

Solution sketch

pub struct LinkedList;

impl LinkedList {
    pub fn retain_mut<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut T) -> bool;

    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&T) -> bool;
}

Alternatives

Links and related work

@TennyZhuang TennyZhuang added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jul 28, 2023
@Amanieu
Copy link
Member

Amanieu commented Oct 17, 2023

We discussed this in the libs-api meeting today. We're happy to add this, but with only a single retain method which accepts &mut T. The split between retain and retain_mut was to correct a mistake: retain was stabilized early on with a &T signature and changing it would have broken many crates.

@Amanieu Amanieu closed this as completed Oct 17, 2023
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants