Skip to content

Feature request: insertion that bails if reallocation would occur #224

@cole-miller

Description

@cole-miller

For a custom data structure I'm implementing that uses hashbrown::HashMap internally, I'd like to access something like the following interface:

// returns the value of `map.insert(key, val)` (wrapped in `Ok`) if this call would not cause `map` to reallocate
// if reallocation would occur, returns `Err((key, val))` without performing the insertion
fn try_insert_no_grow<K: Eq + Hash, V, S: BuildHasher>(
    map: &mut HashMap<K, V, S>,
    key: K,
    val: V,
) -> Result<Option<V>, (K, V)> {
    unimplemented!()
}

Preemptively comparing map.len() to map.capacity() will not achieve this precisely due to the "lower bound" nature of HashMap::capacity mentioned in the docs. I want this power so that I can determine precisely when reallocation occurs and "rebalance" the containing data structure at the same time.

I asked about how to achieve this on URLO here and didn't get any leads on how to do it using the current interface (including RawTable), but if there is a way of course I'd love to hear about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions