From b2d3cce51e8ada6c73663708ffb6b375f33b3af2 Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 5 Aug 2024 18:10:37 -0400 Subject: [PATCH] Implement Clone, Debug for iterators --- src/raw/mod.rs | 1 + src/table.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/raw/mod.rs b/src/raw/mod.rs index d69ac59a7..420b4c7e2 100644 --- a/src/raw/mod.rs +++ b/src/raw/mod.rs @@ -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 { iter: RawIter, allocation: Option<(NonNull, Layout, A)>, diff --git a/src/table.rs b/src/table.rs index 13f67573b..f7947839c 100644 --- a/src/table.rs +++ b/src/table.rs @@ -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 { @@ -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; @@ -1889,6 +1903,16 @@ pub struct IterMut<'a, T> { inner: RawIter, 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)] @@ -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().clone()).finish() + } +} impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; @@ -1942,12 +1971,32 @@ impl 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 where A: Allocator, { inner: RawIntoIter, } +impl IntoIter { + /// 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 Default for IntoIter { #[cfg_attr(feature = "inline-more", inline)] @@ -1957,6 +2006,11 @@ impl Default for IntoIter { } } } +impl<'a, T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.iter().clone()).finish() + } +} impl Iterator for IntoIter where A: Allocator,