-
Notifications
You must be signed in to change notification settings - Fork 321
Add hash_table::UnsafeIter, iter() method to various iterators
#667
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
base: master
Are you sure you want to change the base?
Conversation
src/table.rs
Outdated
| /// // Even though the key is mutably borrowed here, this is sound | ||
| /// // because we never actually mutate the key before yielding the | ||
| /// // immutable reference | ||
| /// let (ref key, ref mut val) = self.inner.next()?; | ||
| /// Some((key, val)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is just an example, and not part of the actual implementation, I chose to use a clearer implementation instead of the one we actually use.
3f35627 to
e2a7538
Compare
|
Rather than adding a whole new iterator kind, would it make more sense to direct users that need custom variance to the bucket iterator instead? |
|
That wouldn't really work because you would then need to mutably borrow the entire table inside the iterator, alongside the bucket iterator, to make it work, which would then give the same variance as Also because you might be confusing the raw bucket API with the one for |
|
I'm not super happy with this API, but I understand why it is necessary to implement |
|
I think giving it a longer, wordier name would probably be okay; part of my thought process was that it was better to offer a variant mut iterator over just offering an iterator without any lifetimes (i.e., over pointers), but in hindsight, I don't really know if that's worth it, since a correct use requires adding markers anyway. So, perhaps we could just convert this into an I've decided to adopt this new approach: it still has a lifetime, but only superficially. |
e2a7538 to
db9ea5d
Compare
db9ea5d to
0b15891
Compare
hash_table::IterUnsafeMut, iter() method to various iteratorshash_table::UnsafeIter, iter() method to various iterators
| /// Any part of the returned elements which is mutated must be made invariant. | ||
| /// See the documentation for [`UnsafeIter`] for an example and further | ||
| /// explanation. | ||
| pub unsafe fn unsafe_iter(&mut self) -> UnsafeIter<'_, T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this now returns NonNull, I don't think this method needs to be unsafe.
Both of these would be useful for #666, and presumably other downstream hash table implementations.
First, the
iter()methods make it possible to implement downstreamDebugimplementations more easily, by letting iterators likeDrainbe paused and viewed immutably.Second, the new
unsafe_itermethod and correspondingUnsafeIterexists to make implementinghash_map::IterMuta bit more reasonable without being as unsafe. The struct docs should hopefully make its existence clear, but I would definitely love some extra scrutiny on that.