Skip to content

Commit

Permalink
Implement Clone, Debug for iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey committed Aug 5, 2024
1 parent f677701 commit b2d3cce
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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)>,
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().clone()).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<'a, 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().clone()).finish()
}
}
impl<T, A> Iterator for IntoIter<T, A>
where
A: Allocator,
Expand Down

0 comments on commit b2d3cce

Please sign in to comment.