Skip to content

Tracking issue for HashMap::extract_if and HashSet::extract_if #59618

Closed
@DutchGhost

Description

@DutchGhost

The feature gate for the issue is #![feature(hash_extract_if)] (previously hash_drain_filter)

Currently only Vec and LinkedList have a drain_filter method, while other collections such as HashMap and HashSet do not.

This means that currently, removing items from a collection, and getting ownership of those items is fairly...unidiomatic and cumbersome.

For references, see rust-lang/rfcs#2140

pub mod collections {
    pub mod hash_map {
        impl<K, V, S> HashMap<K, V, S> {
            pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, K, V, F>
            where
                F: FnMut(&K, &mut V) -> bool;
        }

        pub struct ExtractIf<'a, K, V, F>
        where
            F: FnMut(&K, &mut V) -> bool;

        impl<K, V, F> Iterator for ExtractIf<'_, K, V, F>
        where
            F: FnMut(&K, &mut V) -> bool;

        impl<K, V, F> FusedIterator for ExtractIf<'_, K, V, F>
        where
            F: FnMut(&K, &mut V) -> bool;

        impl<K, V, F> Debug for ExtractIf<'_, K, V, F>
        where
            F: FnMut(&K, &mut V) -> bool;
    }

    pub mod hash_set {
        impl<T, S> HashSet<T, S> {
            pub fn extract_if<F>(&mut self, pred: F) -> ExtractIf<'_, T, F>
            where
                F: FnMut(&T) -> bool;
        }

        pub struct ExtractIf<'a, K, F>
        where
            F: FnMut(&K) -> bool;

        impl<K, F> Iterator for ExtractIf<'_, K, F>
        where
            F: FnMut(&K) -> bool;

        impl<K, F> FusedIterator for ExtractIf<'_, K, F>
        where
            F: FnMut(&K) -> bool;

        impl<K, F> Debug for ExtractIf<'_, K, F>
        where
            F: FnMut(&K) -> bool;
    }
}

Implementation History

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.to-announceAnnounce this issue on triage meeting

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions