Skip to content

feat: add an MashMap::entry_(mut_)iter api#1

Open
llogiq wants to merge 1 commit into
imartayan:mainfrom
llogiq:entry
Open

feat: add an MashMap::entry_(mut_)iter api#1
llogiq wants to merge 1 commit into
imartayan:mainfrom
llogiq:entry

Conversation

@llogiq

@llogiq llogiq commented May 8, 2024

Copy link
Copy Markdown

To avoid borrowing errors when returning as an impl Iterator, I needed a method that would consume the key. So I added it, and a mutable version to complete the API.

To avoid borrowing errors when returning as an `impl Iterator`, I needed
a method that would consume the key. So I added it, and a mutable
version to complete the API.
@imartayan

Copy link
Copy Markdown
Owner

Thanks for the PR!
I agree that this method is useful, ideally I'd like to have a method similar to HashMap's entry, returning an iterator of Entry instead of Bucket.
Would this approach suit your needs?
Also, could you give an example where you need to consume the key?

@llogiq

llogiq commented May 9, 2024

Copy link
Copy Markdown
Author

Sure. Giving a key is useful to build a function that returns an impl Iterator<Item = Foo> + '_ without needing to borrow into both the key and the MashMap. E.g.

fn iter(&self, key: u32) -> impl Iterator<Item = &str> + '_ {
    self.map.entry_iter(key).map(|s| &s)
}

With the current get_iter method, this would run into a "returning a value borrowing a temporary" error.

Also in async code, borrows can be problematic because the compiler has a harder time figuring out the lifetimes.

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.

2 participants