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):
We think this problem seems worth solving, and the standard library might be the right place to solve it.
We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
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:
Solution sketch
Alternatives
matches!(entry, Entry::Vacant(_))
works but isn't as idiomatic and requires importingEntry
. 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: