Skip to content

Commit 35b36eb

Browse files
authored
Merge pull request #196 from cuviper/iter
Iterator polish
2 parents a6e0188 + e594967 commit 35b36eb

File tree

4 files changed

+127
-32
lines changed

4 files changed

+127
-32
lines changed

src/macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,9 @@ macro_rules! double_ended_iterator_methods {
116116
fn next_back(&mut self) -> Option<Self::Item> {
117117
self.iter.next_back().map($map_elt)
118118
}
119+
120+
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
121+
self.iter.nth_back(n).map($map_elt)
122+
}
119123
};
120124
}

src/map.rs

+44-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::vec::{self, Vec};
1212
use ::core::cmp::Ordering;
1313
use ::core::fmt;
1414
use ::core::hash::{BuildHasher, Hash, Hasher};
15-
use ::core::iter::FromIterator;
15+
use ::core::iter::{FromIterator, FusedIterator};
1616
use ::core::ops::{Index, IndexMut, RangeBounds};
1717
use ::core::slice::{Iter as SliceIter, IterMut as SliceIterMut};
1818

@@ -813,9 +813,7 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
813813
}
814814

815815
impl<K, V> DoubleEndedIterator for Keys<'_, K, V> {
816-
fn next_back(&mut self) -> Option<Self::Item> {
817-
self.iter.next_back().map(Bucket::key_ref)
818-
}
816+
double_ended_iterator_methods!(Bucket::key_ref);
819817
}
820818

821819
impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
@@ -824,6 +822,8 @@ impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
824822
}
825823
}
826824

825+
impl<K, V> FusedIterator for Keys<'_, K, V> {}
826+
827827
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
828828
impl<K, V> Clone for Keys<'_, K, V> {
829829
fn clone(&self) -> Self {
@@ -857,9 +857,7 @@ impl<K, V> Iterator for IntoKeys<K, V> {
857857
}
858858

859859
impl<K, V> DoubleEndedIterator for IntoKeys<K, V> {
860-
fn next_back(&mut self) -> Option<Self::Item> {
861-
self.iter.next_back().map(Bucket::key)
862-
}
860+
double_ended_iterator_methods!(Bucket::key);
863861
}
864862

865863
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
@@ -868,6 +866,8 @@ impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
868866
}
869867
}
870868

869+
impl<K, V> FusedIterator for IntoKeys<K, V> {}
870+
871871
impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
872872
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
873873
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
@@ -893,9 +893,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
893893
}
894894

895895
impl<K, V> DoubleEndedIterator for Values<'_, K, V> {
896-
fn next_back(&mut self) -> Option<Self::Item> {
897-
self.iter.next_back().map(Bucket::value_ref)
898-
}
896+
double_ended_iterator_methods!(Bucket::value_ref);
899897
}
900898

901899
impl<K, V> ExactSizeIterator for Values<'_, K, V> {
@@ -904,6 +902,8 @@ impl<K, V> ExactSizeIterator for Values<'_, K, V> {
904902
}
905903
}
906904

905+
impl<K, V> FusedIterator for Values<'_, K, V> {}
906+
907907
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
908908
impl<K, V> Clone for Values<'_, K, V> {
909909
fn clone(&self) -> Self {
@@ -937,9 +937,7 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
937937
}
938938

939939
impl<K, V> DoubleEndedIterator for ValuesMut<'_, K, V> {
940-
fn next_back(&mut self) -> Option<Self::Item> {
941-
self.iter.next_back().map(Bucket::value_mut)
942-
}
940+
double_ended_iterator_methods!(Bucket::value_mut);
943941
}
944942

945943
impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
@@ -948,6 +946,10 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
948946
}
949947
}
950948

949+
impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}
950+
951+
// TODO: `impl Debug for ValuesMut` once we have MSRV 1.53 for `slice::IterMut::as_slice`
952+
951953
/// An owning iterator over the values of a `IndexMap`.
952954
///
953955
/// This `struct` is created by the [`into_values`] method on [`IndexMap`].
@@ -966,9 +968,7 @@ impl<K, V> Iterator for IntoValues<K, V> {
966968
}
967969

968970
impl<K, V> DoubleEndedIterator for IntoValues<K, V> {
969-
fn next_back(&mut self) -> Option<Self::Item> {
970-
self.iter.next_back().map(Bucket::value)
971-
}
971+
double_ended_iterator_methods!(Bucket::value);
972972
}
973973

974974
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
@@ -977,6 +977,8 @@ impl<K, V> ExactSizeIterator for IntoValues<K, V> {
977977
}
978978
}
979979

980+
impl<K, V> FusedIterator for IntoValues<K, V> {}
981+
980982
impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
981983
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
982984
let iter = self.iter.as_slice().iter().map(Bucket::value_ref);
@@ -1002,9 +1004,7 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
10021004
}
10031005

10041006
impl<K, V> DoubleEndedIterator for Iter<'_, K, V> {
1005-
fn next_back(&mut self) -> Option<Self::Item> {
1006-
self.iter.next_back().map(Bucket::refs)
1007-
}
1007+
double_ended_iterator_methods!(Bucket::refs);
10081008
}
10091009

10101010
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
@@ -1013,6 +1013,8 @@ impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
10131013
}
10141014
}
10151015

1016+
impl<K, V> FusedIterator for Iter<'_, K, V> {}
1017+
10161018
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
10171019
impl<K, V> Clone for Iter<'_, K, V> {
10181020
fn clone(&self) -> Self {
@@ -1046,9 +1048,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
10461048
}
10471049

10481050
impl<K, V> DoubleEndedIterator for IterMut<'_, K, V> {
1049-
fn next_back(&mut self) -> Option<Self::Item> {
1050-
self.iter.next_back().map(Bucket::ref_mut)
1051-
}
1051+
double_ended_iterator_methods!(Bucket::ref_mut);
10521052
}
10531053

10541054
impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
@@ -1057,6 +1057,10 @@ impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
10571057
}
10581058
}
10591059

1060+
impl<K, V> FusedIterator for IterMut<'_, K, V> {}
1061+
1062+
// TODO: `impl Debug for IterMut` once we have MSRV 1.53 for `slice::IterMut::as_slice`
1063+
10601064
/// An owning iterator over the entries of a `IndexMap`.
10611065
///
10621066
/// This `struct` is created by the [`into_iter`] method on [`IndexMap`]
@@ -1075,9 +1079,7 @@ impl<K, V> Iterator for IntoIter<K, V> {
10751079
}
10761080

10771081
impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
1078-
fn next_back(&mut self) -> Option<Self::Item> {
1079-
self.iter.next_back().map(Bucket::key_value)
1080-
}
1082+
double_ended_iterator_methods!(Bucket::key_value);
10811083
}
10821084

10831085
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
@@ -1086,6 +1088,8 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
10861088
}
10871089
}
10881090

1091+
impl<K, V> FusedIterator for IntoIter<K, V> {}
1092+
10891093
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
10901094
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10911095
let iter = self.iter.as_slice().iter().map(Bucket::refs);
@@ -1114,6 +1118,21 @@ impl<K, V> DoubleEndedIterator for Drain<'_, K, V> {
11141118
double_ended_iterator_methods!(Bucket::key_value);
11151119
}
11161120

1121+
impl<K, V> ExactSizeIterator for Drain<'_, K, V> {
1122+
fn len(&self) -> usize {
1123+
self.iter.len()
1124+
}
1125+
}
1126+
1127+
impl<K, V> FusedIterator for Drain<'_, K, V> {}
1128+
1129+
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Drain<'_, K, V> {
1130+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1131+
let iter = self.iter.as_slice().iter().map(Bucket::refs);
1132+
f.debug_list().entries(iter).finish()
1133+
}
1134+
}
1135+
11171136
impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S> {
11181137
type Item = (&'a K, &'a V);
11191138
type IntoIter = Iter<'a, K, V>;

src/rayon/map.rs

+14
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ pub struct ParIterMut<'a, K, V> {
139139
entries: &'a mut [Bucket<K, V>],
140140
}
141141

142+
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for ParIterMut<'_, K, V> {
143+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144+
let iter = self.entries.iter().map(Bucket::refs);
145+
f.debug_list().entries(iter).finish()
146+
}
147+
}
148+
142149
impl<'a, K: Sync + Send, V: Send> ParallelIterator for ParIterMut<'a, K, V> {
143150
type Item = (&'a K, &'a mut V);
144151

@@ -339,6 +346,13 @@ pub struct ParValuesMut<'a, K, V> {
339346
entries: &'a mut [Bucket<K, V>],
340347
}
341348

349+
impl<K, V: fmt::Debug> fmt::Debug for ParValuesMut<'_, K, V> {
350+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
351+
let iter = self.entries.iter().map(Bucket::value_ref);
352+
f.debug_list().entries(iter).finish()
353+
}
354+
}
355+
342356
impl<'a, K: Send, V: Send> ParallelIterator for ParValuesMut<'a, K, V> {
343357
type Item = &'a mut V;
344358

src/set.rs

+65-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::vec::{self, Vec};
1010
use core::cmp::Ordering;
1111
use core::fmt;
1212
use core::hash::{BuildHasher, Hash};
13-
use core::iter::{Chain, FromIterator};
13+
use core::iter::{Chain, FromIterator, FusedIterator};
1414
use core::ops::{BitAnd, BitOr, BitXor, Index, RangeBounds, Sub};
1515
use core::slice;
1616

@@ -708,9 +708,7 @@ impl<T> Iterator for IntoIter<T> {
708708
}
709709

710710
impl<T> DoubleEndedIterator for IntoIter<T> {
711-
fn next_back(&mut self) -> Option<Self::Item> {
712-
self.iter.next_back().map(Bucket::key)
713-
}
711+
double_ended_iterator_methods!(Bucket::key);
714712
}
715713

716714
impl<T> ExactSizeIterator for IntoIter<T> {
@@ -719,6 +717,8 @@ impl<T> ExactSizeIterator for IntoIter<T> {
719717
}
720718
}
721719

720+
impl<T> FusedIterator for IntoIter<T> {}
721+
722722
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
723723
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
724724
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
@@ -744,9 +744,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
744744
}
745745

746746
impl<T> DoubleEndedIterator for Iter<'_, T> {
747-
fn next_back(&mut self) -> Option<Self::Item> {
748-
self.iter.next_back().map(Bucket::key_ref)
749-
}
747+
double_ended_iterator_methods!(Bucket::key_ref);
750748
}
751749

752750
impl<T> ExactSizeIterator for Iter<'_, T> {
@@ -755,6 +753,8 @@ impl<T> ExactSizeIterator for Iter<'_, T> {
755753
}
756754
}
757755

756+
impl<T> FusedIterator for Iter<'_, T> {}
757+
758758
impl<T> Clone for Iter<'_, T> {
759759
fn clone(&self) -> Self {
760760
Iter {
@@ -790,6 +790,21 @@ impl<T> DoubleEndedIterator for Drain<'_, T> {
790790
double_ended_iterator_methods!(Bucket::key);
791791
}
792792

793+
impl<T> ExactSizeIterator for Drain<'_, T> {
794+
fn len(&self) -> usize {
795+
self.iter.len()
796+
}
797+
}
798+
799+
impl<T> FusedIterator for Drain<'_, T> {}
800+
801+
impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
802+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
803+
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
804+
f.debug_list().entries(iter).finish()
805+
}
806+
}
807+
793808
impl<'a, T, S> IntoIterator for &'a IndexSet<T, S> {
794809
type Item = &'a T;
795810
type IntoIter = Iter<'a, T>;
@@ -957,6 +972,13 @@ where
957972
}
958973
}
959974

975+
impl<T, S> FusedIterator for Difference<'_, T, S>
976+
where
977+
T: Eq + Hash,
978+
S: BuildHasher,
979+
{
980+
}
981+
960982
impl<T, S> Clone for Difference<'_, T, S> {
961983
fn clone(&self) -> Self {
962984
Difference {
@@ -1024,6 +1046,13 @@ where
10241046
}
10251047
}
10261048

1049+
impl<T, S> FusedIterator for Intersection<'_, T, S>
1050+
where
1051+
T: Eq + Hash,
1052+
S: BuildHasher,
1053+
{
1054+
}
1055+
10271056
impl<T, S> Clone for Intersection<'_, T, S> {
10281057
fn clone(&self) -> Self {
10291058
Intersection {
@@ -1087,6 +1116,21 @@ where
10871116
fn next_back(&mut self) -> Option<Self::Item> {
10881117
self.iter.next_back()
10891118
}
1119+
1120+
fn rfold<B, F>(self, init: B, f: F) -> B
1121+
where
1122+
F: FnMut(B, Self::Item) -> B,
1123+
{
1124+
self.iter.rfold(init, f)
1125+
}
1126+
}
1127+
1128+
impl<T, S1, S2> FusedIterator for SymmetricDifference<'_, T, S1, S2>
1129+
where
1130+
T: Eq + Hash,
1131+
S1: BuildHasher,
1132+
S2: BuildHasher,
1133+
{
10901134
}
10911135

10921136
impl<T, S1, S2> Clone for SymmetricDifference<'_, T, S1, S2> {
@@ -1150,6 +1194,20 @@ where
11501194
fn next_back(&mut self) -> Option<Self::Item> {
11511195
self.iter.next_back()
11521196
}
1197+
1198+
fn rfold<B, F>(self, init: B, f: F) -> B
1199+
where
1200+
F: FnMut(B, Self::Item) -> B,
1201+
{
1202+
self.iter.rfold(init, f)
1203+
}
1204+
}
1205+
1206+
impl<T, S> FusedIterator for Union<'_, T, S>
1207+
where
1208+
T: Eq + Hash,
1209+
S: BuildHasher,
1210+
{
11531211
}
11541212

11551213
impl<T, S> Clone for Union<'_, T, S> {

0 commit comments

Comments
 (0)