From 45615adc63e6eba9bc7f64ac1b44fbb7948a66ff Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Fri, 20 Sep 2024 13:10:29 +0100 Subject: [PATCH] Implement `Debug`, `FusedIterator` and `Iterator::fold` for all `HashTable` iterators --- src/table.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/src/table.rs b/src/table.rs index 0b8b66424..333c75735 100644 --- a/src/table.rs +++ b/src/table.rs @@ -2051,6 +2051,20 @@ impl ExactSizeIterator for IterMut<'_, T> { impl FusedIterator for IterMut<'_, T> {} +impl fmt::Debug for IterMut<'_, T> +where + T: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list() + .entries(Iter { + inner: self.inner.clone(), + marker: PhantomData, + }) + .finish() + } +} + /// An iterator over the entries of a `HashTable` that could match a given hash. /// The iterator element type is `&'a T`. /// @@ -2074,8 +2088,19 @@ impl<'a, T> Iterator for IterHash<'a, T> { None => None, } } + + fn fold(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner + .fold(init, |acc, bucket| unsafe { f(acc, bucket.as_ref()) }) + } } +impl FusedIterator for IterHash<'_, T> {} + /// A mutable iterator over the entries of a `HashTable` that could match a given hash. /// The iterator element type is `&'a mut T`. /// @@ -2099,8 +2124,19 @@ impl<'a, T> Iterator for IterHashMut<'a, T> { None => None, } } + + fn fold(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner + .fold(init, |acc, bucket| unsafe { f(acc, bucket.as_mut()) }) + } } +impl FusedIterator for IterHashMut<'_, T> {} + /// An owning iterator over the entries of a `HashTable` in arbitrary order. /// The iterator element type is `T`. /// @@ -2126,6 +2162,7 @@ impl Default for IntoIter { } } } + impl Iterator for IntoIter where A: Allocator, @@ -2160,6 +2197,21 @@ where impl FusedIterator for IntoIter where A: Allocator {} +impl fmt::Debug for IntoIter +where + T: fmt::Debug, + A: Allocator, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list() + .entries(Iter { + inner: self.inner.iter(), + marker: PhantomData, + }) + .finish() + } +} + /// A draining iterator over the items of a `HashTable`. /// /// This `struct` is created by the [`drain`] method on [`HashTable`]. @@ -2171,36 +2223,42 @@ pub struct Drain<'a, T, A: Allocator = Global> { inner: RawDrain<'a, T, A>, } -impl Drain<'_, T, A> { - /// Returns a iterator of references over the remaining items. - fn iter(&self) -> Iter<'_, T> { - Iter { - inner: self.inner.iter(), - marker: PhantomData, - } - } -} - impl Iterator for Drain<'_, T, A> { type Item = T; fn next(&mut self) -> Option { self.inner.next() } + fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } + + fn fold(self, init: B, f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + self.inner.fold(init, f) + } } + impl ExactSizeIterator for Drain<'_, T, A> { fn len(&self) -> usize { self.inner.len() } } + impl FusedIterator for Drain<'_, T, A> {} impl fmt::Debug for Drain<'_, T, A> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_list().entries(self.iter()).finish() + f.debug_list() + .entries(Iter { + inner: self.inner.iter(), + marker: PhantomData, + }) + .finish() } }