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

Implement Clone and Debug for HashTable's Iter struct #541

Merged
merged 1 commit into from
Sep 20, 2024

Conversation

geeknoid
Copy link

Found those implementations missing, which is preventing me from implementing the same in encapsulating iterators.

src/table.rs Outdated
@@ -1914,6 +1931,23 @@ impl<T> ExactSizeIterator for IterMut<'_, T> {

impl<T> FusedIterator for IterMut<'_, T> {}

// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
impl<'a, T> Clone for IterMut<'a, T> {
Copy link
Member

Choose a reason for hiding this comment

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

Cloning a mutable iterator is unsound since it could be used to create multiple aliasing mutable references. All the other impls seem fine though.

Copy link
Author

@geeknoid geeknoid Jul 28, 2024

Choose a reason for hiding this comment

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

Sorry, I'm still new to Rust. I understand your point and it makes sense, so I removed the implementation of Clone here. And in fact, looking at my own code, my wrapping iterators haven't been implementing this either.

However, if this leads to unsoundness, how come I was able to implement the logic in safe code and the compiler didn't complain? Should RawIter::clone be marked as unsafe so that what I did would flag a problem?

Copy link
Member

Choose a reason for hiding this comment

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

This isn't entirely safe: if you look above, the Iterator trait implementation for IterMut uses unsafe code. That unsafe code is only valid if it is the only instance of IterMut for a table.

Copy link
Member

Choose a reason for hiding this comment

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

This isn't resolved, it seem you have re-added the incorrect Clone impl.

Copy link
Author

Choose a reason for hiding this comment

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

OK, redeleted. Sorry about that.

Copy link
Member

Choose a reason for hiding this comment

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

On the other hand, Debug for IterMut is fine.

Copy link
Author

Choose a reason for hiding this comment

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

To implement Debug, I needed Clone, so I just punted on IterMut alltogether.

Copy link
Member

Choose a reason for hiding this comment

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

The map IterMut does it through a private iter() that only clones the inner: RawIter.

hashbrown/src/map.rs

Lines 3175 to 3183 in a69af93

impl<K, V> fmt::Debug for IterMut<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}

hashbrown/src/map.rs

Lines 2137 to 2146 in a69af93

impl<K, V> IterMut<'_, K, V> {
/// Returns a iterator of references over the remaining items.
#[cfg_attr(feature = "inline-more", inline)]
pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.clone(),
marker: PhantomData,
}
}
}

.idea/.gitignore Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Sep 2, 2024

☔ The latest upstream changes (presumably #549) made this pull request unmergeable. Please resolve the merge conflicts.

@geeknoid geeknoid force-pushed the geeknoid/iters branch 2 times, most recently from 29df316 to d6a46a9 Compare September 7, 2024 18:07
@geeknoid geeknoid changed the title Implement Clone and Debug for HashTable's Iter and IterMut structs Implement Clone and Debug for HashTable's Iter struct Sep 7, 2024
@cuviper cuviper linked an issue Sep 18, 2024 that may be closed by this pull request
@Amanieu
Copy link
Member

Amanieu commented Sep 20, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Sep 20, 2024

📌 Commit e9fd7b4 has been approved by Amanieu

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 20, 2024

⌛ Testing commit e9fd7b4 with merge cd9a955...

@bors
Copy link
Contributor

bors commented Sep 20, 2024

☀️ Test successful - checks-actions
Approved by: Amanieu
Pushing cd9a955 to master...

@bors bors merged commit cd9a955 into rust-lang:master Sep 20, 2024
24 checks passed
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.

Missing Clone implementation for hash_table::Iter
4 participants