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

Make hashmap iterators implement ExactSize #19327

Closed
wants to merge 3 commits into from

Conversation

cgaebel
Copy link
Contributor

@cgaebel cgaebel commented Nov 26, 2014

No description provided.

impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a V)> for Entries<'a, K, V> {
#[inline]
fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
self.inner.next_back()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hashmap is fundamentally unordered. Semantically, what does it even mean to iterate backwards? Couldn't this just call next and call it a day? Or am I missing a usecase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be surprising if forward iteration and backward iteration returned the same series of elements. Anyhow, I only implemented DoubleEndedIterator because it's needed for ExactSize for some reason.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm fine with impl'ing DoubleEnded and ExactSize for usability with that tooling. I don't however think people should be able to rely on the order of elements when you call next/next_back between iterations. I think it's fine to have next_back be a synonym for next here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would go against the spirit of DoubleEndedIterator. We should at least be doing something "backwards-ish" or at least as backwards as makes sense for a hashtable. People might have a usecase for it and I feel like we shouldn't just lie in the interface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the argument either way, honestly. Plus we can explicitly document that behaviour can't be relied on for different sequences of next/next_back. Which is backwards-compatible to "undo". I'm not a huge fan of duplicating a bunch on unsafe code in the already-beefy rawtable. Making next=next_back seems like a simple way to do that.

Interested in getting some other opinions though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too. I wish someone "owned" this code. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably @pczarn if anyone. Interested in @aturon's API-thoughts though.

@alexcrichton
Copy link
Member

Closing out of inactivity (and to help clear out @bors's queue), but feel free to reopen with a rebase!

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.

3 participants