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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

llogiq
Copy link

@llogiq llogiq commented May 8, 2024

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
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
Copy link
Author

llogiq commented May 9, 2024

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.

None yet

2 participants