Skip to content

Add indexing methods to raw entry#305

Merged
cuviper merged 2 commits intoindexmap-rs:masterfrom
cuviper:raw_entry-index
Jan 31, 2024
Merged

Add indexing methods to raw entry#305
cuviper merged 2 commits intoindexmap-rs:masterfrom
cuviper:raw_entry-index

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
@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