Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indexing methods to raw entry #305

Merged
merged 2 commits into from
Jan 31, 2024

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Jan 30, 2024

The std-matching methods of RawEntryBuilder only return (&K, &V),
but the index is also useful when working with IndexMap.

impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
    /// Access an entry by hash, including its index.
    pub fn from_hash_full<F>(self, hash: u64, is_match: F) -> Option<(usize, &'a K, &'a V)>
    where
        F: FnMut(&K) -> bool,

    /// Access the index of an entry by hash.
    pub fn index_from_hash<F>(self, hash: u64, is_match: F) -> Option<usize>
    where
        F: FnMut(&K) -> bool,
}

In addition, the RawEntryMut enum can report its index by forwarding
to the existing methods on its variants.

impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
    /// Return the index where the key-value pair exists or may be inserted.
    pub fn index(&self) -> usize
}

The `std`-matching methods of `RawEntryBuilder` only return `(&K, &V)`,
but the index is also useful when working with `IndexMap`.

```rust
impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
    /// Access an entry by hash, including its index.
    pub fn from_hash_full<F>(self, hash: u64, is_match: F) -> Option<(usize, &'a K, &'a V)>
    where
        F: FnMut(&K) -> bool,

    /// Access the index of an entry by hash.
    pub fn index_from_hash<F>(self, hash: u64, is_match: F) -> Option<usize>
    where
        F: FnMut(&K) -> bool,
}
```

In addition, the `RawEntryMut` enum can report its index by forwarding
to the existing methods on its variants.

```rust
impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
    /// Return the index where the key-value pair exists or may be inserted.
    pub fn index(&self) -> usize
}
```
@cuviper cuviper added this pull request to the merge queue Jan 31, 2024
Merged via the queue into indexmap-rs:master with commit 1a50e7b Jan 31, 2024
15 checks passed
@cuviper cuviper deleted the raw_entry-index branch February 1, 2024 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant