Skip to content

Commit b2d3cce

Browse files
committed
Implement Clone, Debug for iterators
1 parent f677701 commit b2d3cce

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/raw/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4280,6 +4280,7 @@ impl ExactSizeIterator for FullBucketsIndices {}
42804280
impl FusedIterator for FullBucketsIndices {}
42814281

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

src/table.rs

+54
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,15 @@ pub struct Iter<'a, T> {
18351835
marker: PhantomData<&'a T>,
18361836
}
18371837

1838+
impl<'a, T> Clone for Iter<'a, T> {
1839+
#[cfg_attr(feature = "inline-more", inline)]
1840+
fn clone(&self) -> Self {
1841+
Iter {
1842+
inner: self.inner.clone(),
1843+
marker: PhantomData,
1844+
}
1845+
}
1846+
}
18381847
impl<'a, T> Default for Iter<'a, T> {
18391848
#[cfg_attr(feature = "inline-more", inline)]
18401849
fn default() -> Self {
@@ -1844,6 +1853,11 @@ impl<'a, T> Default for Iter<'a, T> {
18441853
}
18451854
}
18461855
}
1856+
impl<'a, T: fmt::Debug> fmt::Debug for Iter<'a, T> {
1857+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1858+
f.debug_list().entries(self.clone()).finish()
1859+
}
1860+
}
18471861
impl<'a, T> Iterator for Iter<'a, T> {
18481862
type Item = &'a T;
18491863

@@ -1889,6 +1903,16 @@ pub struct IterMut<'a, T> {
18891903
inner: RawIter<T>,
18901904
marker: PhantomData<&'a mut T>,
18911905
}
1906+
impl<'a, T> IterMut<'a, T> {
1907+
/// Borrows as a non-mutable iterator.
1908+
#[cfg_attr(feature = "inline-more", inline)]
1909+
pub fn iter(&self) -> Iter<'_, T> {
1910+
Iter {
1911+
inner: self.inner.clone(),
1912+
marker: PhantomData,
1913+
}
1914+
}
1915+
}
18921916

18931917
impl<'a, T> Default for IterMut<'a, T> {
18941918
#[cfg_attr(feature = "inline-more", inline)]
@@ -1899,6 +1923,11 @@ impl<'a, T> Default for IterMut<'a, T> {
18991923
}
19001924
}
19011925
}
1926+
impl<'a, T: fmt::Debug> fmt::Debug for IterMut<'a, T> {
1927+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1928+
f.debug_list().entries(self.iter().clone()).finish()
1929+
}
1930+
}
19021931
impl<'a, T> Iterator for IterMut<'a, T> {
19031932
type Item = &'a mut T;
19041933

@@ -1942,12 +1971,32 @@ impl<T> FusedIterator for IterMut<'_, T> {}
19421971
/// [`into_iter`]: struct.HashTable.html#method.into_iter
19431972
/// [`HashTable`]: struct.HashTable.html
19441973
/// [`IntoIterator`]: https://doc.rust-lang.org/core/iter/trait.IntoIterator.html
1974+
#[derive(Clone)]
19451975
pub struct IntoIter<T, A = Global>
19461976
where
19471977
A: Allocator,
19481978
{
19491979
inner: RawIntoIter<T, A>,
19501980
}
1981+
impl<T, A: Allocator> IntoIter<T, A> {
1982+
/// Borrows the remaining elements as an iterator.
1983+
#[cfg_attr(feature = "inline-more", inline)]
1984+
pub fn iter(&self) -> Iter<'_, T> {
1985+
Iter {
1986+
inner: self.inner.iter(),
1987+
marker: PhantomData,
1988+
}
1989+
}
1990+
1991+
/// Borrows the remaining elements as a mutable iterator.
1992+
#[cfg_attr(feature = "inline-more", inline)]
1993+
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
1994+
IterMut {
1995+
inner: self.inner.iter(),
1996+
marker: PhantomData,
1997+
}
1998+
}
1999+
}
19512000

19522001
impl<T, A: Allocator> Default for IntoIter<T, A> {
19532002
#[cfg_attr(feature = "inline-more", inline)]
@@ -1957,6 +2006,11 @@ impl<T, A: Allocator> Default for IntoIter<T, A> {
19572006
}
19582007
}
19592008
}
2009+
impl<'a, T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
2010+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2011+
f.debug_list().entries(self.iter().clone()).finish()
2012+
}
2013+
}
19602014
impl<T, A> Iterator for IntoIter<T, A>
19612015
where
19622016
A: Allocator,

0 commit comments

Comments
 (0)