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, Debug for iterators #543

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,7 @@ impl<K, V> IterMut<'_, K, V> {
/// assert_eq!(iter.next(), None);
/// assert_eq!(iter.next(), None);
/// ```
#[derive(Clone)]
pub struct IntoIter<K, V, A: Allocator = Global> {
inner: RawIntoIter<(K, V), A>,
}
Expand Down Expand Up @@ -2443,6 +2444,7 @@ impl<K, V, A: Allocator> IntoIter<K, V, A> {
/// assert_eq!(keys.next(), None);
/// assert_eq!(keys.next(), None);
/// ```
#[derive(Clone)]
pub struct IntoKeys<K, V, A: Allocator = Global> {
inner: IntoIter<K, V, A>,
}
Expand Down Expand Up @@ -2521,6 +2523,7 @@ impl<K: Debug, V: Debug, A: Allocator> fmt::Debug for IntoKeys<K, V, A> {
/// assert_eq!(values.next(), None);
/// assert_eq!(values.next(), None);
/// ```
#[derive(Clone)]
pub struct IntoValues<K, V, A: Allocator = Global> {
inner: IntoIter<K, V, A>,
}
Expand Down
1 change: 1 addition & 0 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4280,6 +4280,7 @@ impl ExactSizeIterator for FullBucketsIndices {}
impl FusedIterator for FullBucketsIndices {}

/// Iterator which consumes a table and returns elements.
#[derive(Clone)]
pub struct RawIntoIter<T, A: Allocator = Global> {
iter: RawIter<T>,
allocation: Option<(NonNull<u8>, Layout, A)>,
Copy link
Member

Choose a reason for hiding this comment

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

This is not going to work correctly since it will end up double-freeing the underlying allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that makes sense.

I'll look into actually cloning the allocation instead.

Expand Down
1 change: 1 addition & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,7 @@ pub struct Iter<'a, K> {
///
/// [`HashSet`]: struct.HashSet.html
/// [`into_iter`]: struct.HashSet.html#method.into_iter
#[derive(Clone)]
pub struct IntoIter<K, A: Allocator = Global> {
iter: map::IntoIter<K, (), A>,
}
Expand Down
54 changes: 54 additions & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,15 @@ pub struct Iter<'a, T> {
marker: PhantomData<&'a T>,
}

impl<'a, T> Clone for Iter<'a, T> {
#[cfg_attr(feature = "inline-more", inline)]
fn clone(&self) -> Self {
Iter {
inner: self.inner.clone(),
marker: PhantomData,
}
}
}
impl<'a, T> Default for Iter<'a, T> {
#[cfg_attr(feature = "inline-more", inline)]
fn default() -> Self {
Expand All @@ -1844,6 +1853,11 @@ impl<'a, T> Default for Iter<'a, T> {
}
}
}
impl<'a, T: fmt::Debug> fmt::Debug for Iter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

Expand Down Expand Up @@ -1889,6 +1903,16 @@ pub struct IterMut<'a, T> {
inner: RawIter<T>,
marker: PhantomData<&'a mut T>,
}
impl<'a, T> IterMut<'a, T> {
/// Borrows as a non-mutable iterator.
#[cfg_attr(feature = "inline-more", inline)]
pub fn iter(&self) -> Iter<'_, T> {
Iter {
inner: self.inner.clone(),
marker: PhantomData,
}
}
}

impl<'a, T> Default for IterMut<'a, T> {
#[cfg_attr(feature = "inline-more", inline)]
Expand All @@ -1899,6 +1923,11 @@ impl<'a, T> Default for IterMut<'a, T> {
}
}
}
impl<'a, T: fmt::Debug> fmt::Debug for IterMut<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;

Expand Down Expand Up @@ -1942,12 +1971,32 @@ impl<T> FusedIterator for IterMut<'_, T> {}
/// [`into_iter`]: struct.HashTable.html#method.into_iter
/// [`HashTable`]: struct.HashTable.html
/// [`IntoIterator`]: https://doc.rust-lang.org/core/iter/trait.IntoIterator.html
#[derive(Clone)]
pub struct IntoIter<T, A = Global>
where
A: Allocator,
{
inner: RawIntoIter<T, A>,
}
impl<T, A: Allocator> IntoIter<T, A> {
/// Borrows the remaining elements as an iterator.
#[cfg_attr(feature = "inline-more", inline)]
pub fn iter(&self) -> Iter<'_, T> {
Iter {
inner: self.inner.iter(),
marker: PhantomData,
}
}

/// Borrows the remaining elements as a mutable iterator.
#[cfg_attr(feature = "inline-more", inline)]
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
IterMut {
inner: self.inner.iter(),
marker: PhantomData,
}
}
}

impl<T, A: Allocator> Default for IntoIter<T, A> {
#[cfg_attr(feature = "inline-more", inline)]
Expand All @@ -1957,6 +2006,11 @@ impl<T, A: Allocator> Default for IntoIter<T, A> {
}
}
}
impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<T, A> Iterator for IntoIter<T, A>
where
A: Allocator,
Expand Down
Loading