Skip to content

Commit 05a6185

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

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/map.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,7 @@ impl<K, V> IterMut<'_, K, V> {
23992399
/// assert_eq!(iter.next(), None);
24002400
/// assert_eq!(iter.next(), None);
24012401
/// ```
2402+
#[derive(Clone)]
24022403
pub struct IntoIter<K, V, A: Allocator = Global> {
24032404
inner: RawIntoIter<(K, V), A>,
24042405
}
@@ -2443,6 +2444,7 @@ impl<K, V, A: Allocator> IntoIter<K, V, A> {
24432444
/// assert_eq!(keys.next(), None);
24442445
/// assert_eq!(keys.next(), None);
24452446
/// ```
2447+
#[derive(Clone)]
24462448
pub struct IntoKeys<K, V, A: Allocator = Global> {
24472449
inner: IntoIter<K, V, A>,
24482450
}
@@ -2521,6 +2523,7 @@ impl<K: Debug, V: Debug, A: Allocator> fmt::Debug for IntoKeys<K, V, A> {
25212523
/// assert_eq!(values.next(), None);
25222524
/// assert_eq!(values.next(), None);
25232525
/// ```
2526+
#[derive(Clone)]
25242527
pub struct IntoValues<K, V, A: Allocator = Global> {
25252528
inner: IntoIter<K, V, A>,
25262529
}

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/set.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,7 @@ pub struct Iter<'a, K> {
17011701
///
17021702
/// [`HashSet`]: struct.HashSet.html
17031703
/// [`into_iter`]: struct.HashSet.html#method.into_iter
1704+
#[derive(Clone)]
17041705
pub struct IntoIter<K, A: Allocator = Global> {
17051706
iter: map::IntoIter<K, (), A>,
17061707
}

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()).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<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()).finish()
2012+
}
2013+
}
19602014
impl<T, A> Iterator for IntoIter<T, A>
19612015
where
19622016
A: Allocator,

0 commit comments

Comments
 (0)