Skip to content

Add LinkedList::{retain,retain_mut} #250

Closed
@TennyZhuang

Description

@TennyZhuang

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions