Skip to content

Commit

Permalink
Implement Debug, FusedIterator and Iterator::fold for all `Hash…
Browse files Browse the repository at this point in the history
…Table` iterators
  • Loading branch information
Amanieu committed Sep 20, 2024
1 parent cd9a955 commit 45615ad
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,20 @@ impl<T> ExactSizeIterator for IterMut<'_, T> {

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

impl<T> 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`.
///
Expand All @@ -2074,8 +2088,19 @@ impl<'a, T> Iterator for IterHash<'a, T> {
None => None,
}
}

fn fold<B, F>(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<T> 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`.
///
Expand All @@ -2099,8 +2124,19 @@ impl<'a, T> Iterator for IterHashMut<'a, T> {
None => None,
}
}

fn fold<B, F>(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<T> FusedIterator for IterHashMut<'_, T> {}

/// An owning iterator over the entries of a `HashTable` in arbitrary order.
/// The iterator element type is `T`.
///
Expand All @@ -2126,6 +2162,7 @@ impl<T, A: Allocator> Default for IntoIter<T, A> {
}
}
}

impl<T, A> Iterator for IntoIter<T, A>
where
A: Allocator,
Expand Down Expand Up @@ -2160,6 +2197,21 @@ where

impl<T, A> FusedIterator for IntoIter<T, A> where A: Allocator {}

impl<T, A> fmt::Debug for IntoIter<T, A>
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`].
Expand All @@ -2171,36 +2223,42 @@ pub struct Drain<'a, T, A: Allocator = Global> {
inner: RawDrain<'a, T, A>,
}

impl<T, A: Allocator> 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<T, A: Allocator> Iterator for Drain<'_, T, A> {
type Item = T;

fn next(&mut self) -> Option<T> {
self.inner.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, f)
}
}

impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {
fn len(&self) -> usize {
self.inner.len()
}
}

impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}

impl<T: fmt::Debug, A: Allocator> 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()
}
}

Expand Down

0 comments on commit 45615ad

Please sign in to comment.