rust-lang / libs-team

The home of the library team
Apache License 2.0
110 stars 18 forks source link

ACP: `std::collections::{hash_map, btree_map}::Entry::{is_vacant, is_occupied}` #357

Open finnbear opened 3 months ago

finnbear commented 3 months ago

Proposal

Problem statement

Checking whether a map Entry is vacant or occupied isn't maximally idiomatic.

Motivating examples or use cases

Ran into this today:

/// Can add items that belong. Will keep items that are active.
struct Cache {
    map: HashMap<Id, Value>
}

impl Cache {
    /// Returns `true` iff `id` belongs in the cache
    /// (in my case, a remote server can change which id's to keep).
    fn can_add(id: Id) -> bool;
    /// Returns `true` iff `id` is currently in use
    /// (in my case, values are in use if there are ongoing client connections involving them).
    fn should_keep(id: Id) -> bool;

    /// Get a mutable reference to a value in the cache, adding it if necessary and possible.
    /// Returns `None` if the value is missing and shouldn't be added.
    pub fn get_mut(&mut self, id: Id) -> Option<&mut Value> {
        let mut entry = self.map.entry(id);
        if entry.is_vacant() && !Self::can_add(id) { // <------------ HERE
            return None;
        }
        Some(entry.or_default())
    }

    /// Garbage collector. Call this ocasionally.
    pub fn prune(&mut self) {
        self.map.retain(|id, _| Self::should_keep(*id));
    }
}

Solution sketch

impl std::collections::hash_map::Entry {
    /// Returns `true` if the entry is [`Vacant`].
    #[inline]
    pub fn is_vacant(&self) -> bool {
        matches!(self, Self::Vacant(_));
    }

    /// Returns `true` if the entry is [`Occupied`].
    #[inline]
    pub fn is_occupied(&self) -> bool {
        matches!(self, Self::Occupied(_));
    }
}

impl std::collections::btree_map::Entry {
    /// Returns `true` if the entry is [`Vacant`].
    #[inline]
    pub fn is_vacant(&self) -> bool {
        matches!(self, Self::Vacant(_));
    }

    /// Returns `true` if the entry is [`Occupied`].
    #[inline]
    pub fn is_occupied(&self) -> bool {
        matches!(self, Self::Occupied(_));
    }
}

Alternatives

matches!(entry, Entry::Vacant(_)) works but isn't as idiomatic and requires importing Entry. This functionality is too small to belong in a crate.

Links and related work

I looked at a few other map crates, and didn't find similar API's. Not sure why. Maybe they're mirroring std.

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution: