Skip to content

Commit c288eb9

Browse files
committed
Reduce genericity in Enumerate
1 parent 2e1d5ca commit c288eb9

File tree

1 file changed

+77
-54
lines changed
  • src/libcore/iter/adapters

1 file changed

+77
-54
lines changed

src/libcore/iter/adapters/mod.rs

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cmp;
22
use crate::fmt;
3-
use crate::ops::Try;
3+
use crate::ops::{Add, AddAssign, Try};
44
use crate::usize;
55
use crate::intrinsics;
66

@@ -961,14 +961,12 @@ impl<I> Iterator for Enumerate<I> where I: Iterator {
961961
///
962962
/// Might panic if the index of the element overflows a `usize`.
963963
#[inline]
964-
#[rustc_inherit_overflow_checks]
965964
fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
966-
self.iter.next().map(|a| {
967-
let ret = (self.count, a);
968-
// Possible undefined overflow.
969-
self.count += 1;
970-
ret
971-
})
965+
let a = self.iter.next()?;
966+
let i = self.count;
967+
// Possible undefined overflow.
968+
AddAssign::add_assign(&mut self.count, 1);
969+
Some((i, a))
972970
}
973971

974972
#[inline]
@@ -977,13 +975,12 @@ impl<I> Iterator for Enumerate<I> where I: Iterator {
977975
}
978976

979977
#[inline]
980-
#[rustc_inherit_overflow_checks]
981978
fn nth(&mut self, n: usize) -> Option<(usize, I::Item)> {
982-
self.iter.nth(n).map(|a| {
983-
let i = self.count + n;
984-
self.count = i + 1;
985-
(i, a)
986-
})
979+
let a = self.iter.nth(n)?;
980+
// Possible undefined overflow.
981+
let i = Add::add(self.count, n);
982+
self.count = Add::add(i, 1);
983+
Some((i, a))
987984
}
988985

989986
#[inline]
@@ -992,29 +989,43 @@ impl<I> Iterator for Enumerate<I> where I: Iterator {
992989
}
993990

994991
#[inline]
995-
#[rustc_inherit_overflow_checks]
996-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
992+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
997993
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
998994
{
999-
let count = &mut self.count;
1000-
self.iter.try_fold(init, move |acc, item| {
1001-
let acc = fold(acc, (*count, item));
1002-
*count += 1;
1003-
acc
1004-
})
995+
#[inline]
996+
fn enumerate<'a, T, Acc, R>(
997+
count: &'a mut usize,
998+
mut fold: impl FnMut(Acc, (usize, T)) -> R + 'a,
999+
) -> impl FnMut(Acc, T) -> R + 'a {
1000+
move |acc, item| {
1001+
let acc = fold(acc, (*count, item));
1002+
// Possible undefined overflow.
1003+
AddAssign::add_assign(count, 1);
1004+
acc
1005+
}
1006+
}
1007+
1008+
self.iter.try_fold(init, enumerate(&mut self.count, fold))
10051009
}
10061010

10071011
#[inline]
1008-
#[rustc_inherit_overflow_checks]
1009-
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
1012+
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
10101013
where Fold: FnMut(Acc, Self::Item) -> Acc,
10111014
{
1012-
let mut count = self.count;
1013-
self.iter.fold(init, move |acc, item| {
1014-
let acc = fold(acc, (count, item));
1015-
count += 1;
1016-
acc
1017-
})
1015+
#[inline]
1016+
fn enumerate<T, Acc>(
1017+
mut count: usize,
1018+
mut fold: impl FnMut(Acc, (usize, T)) -> Acc,
1019+
) -> impl FnMut(Acc, T) -> Acc {
1020+
move |acc, item| {
1021+
let acc = fold(acc, (count, item));
1022+
// Possible undefined overflow.
1023+
AddAssign::add_assign(&mut count, 1);
1024+
acc
1025+
}
1026+
}
1027+
1028+
self.iter.fold(init, enumerate(self.count, fold))
10181029
}
10191030
}
10201031

@@ -1024,48 +1035,60 @@ impl<I> DoubleEndedIterator for Enumerate<I> where
10241035
{
10251036
#[inline]
10261037
fn next_back(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
1027-
self.iter.next_back().map(|a| {
1028-
let len = self.iter.len();
1029-
// Can safely add, `ExactSizeIterator` promises that the number of
1030-
// elements fits into a `usize`.
1031-
(self.count + len, a)
1032-
})
1038+
let a = self.iter.next_back()?;
1039+
let len = self.iter.len();
1040+
// Can safely add, `ExactSizeIterator` promises that the number of
1041+
// elements fits into a `usize`.
1042+
Some((self.count + len, a))
10331043
}
10341044

10351045
#[inline]
10361046
fn nth_back(&mut self, n: usize) -> Option<(usize, <I as Iterator>::Item)> {
1037-
self.iter.nth_back(n).map(|a| {
1038-
let len = self.iter.len();
1039-
// Can safely add, `ExactSizeIterator` promises that the number of
1040-
// elements fits into a `usize`.
1041-
(self.count + len, a)
1042-
})
1047+
let a = self.iter.nth_back(n)?;
1048+
let len = self.iter.len();
1049+
// Can safely add, `ExactSizeIterator` promises that the number of
1050+
// elements fits into a `usize`.
1051+
Some((self.count + len, a))
10431052
}
10441053

10451054
#[inline]
1046-
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
1055+
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
10471056
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
10481057
{
10491058
// Can safely add and subtract the count, as `ExactSizeIterator` promises
10501059
// that the number of elements fits into a `usize`.
1051-
let mut count = self.count + self.iter.len();
1052-
self.iter.try_rfold(init, move |acc, item| {
1053-
count -= 1;
1054-
fold(acc, (count, item))
1055-
})
1060+
fn enumerate<T, Acc, R>(
1061+
mut count: usize,
1062+
mut fold: impl FnMut(Acc, (usize, T)) -> R,
1063+
) -> impl FnMut(Acc, T) -> R {
1064+
move |acc, item| {
1065+
count -= 1;
1066+
fold(acc, (count, item))
1067+
}
1068+
}
1069+
1070+
let count = self.count + self.iter.len();
1071+
self.iter.try_rfold(init, enumerate(count, fold))
10561072
}
10571073

10581074
#[inline]
1059-
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
1075+
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
10601076
where Fold: FnMut(Acc, Self::Item) -> Acc,
10611077
{
10621078
// Can safely add and subtract the count, as `ExactSizeIterator` promises
10631079
// that the number of elements fits into a `usize`.
1064-
let mut count = self.count + self.iter.len();
1065-
self.iter.rfold(init, move |acc, item| {
1066-
count -= 1;
1067-
fold(acc, (count, item))
1068-
})
1080+
fn enumerate<T, Acc>(
1081+
mut count: usize,
1082+
mut fold: impl FnMut(Acc, (usize, T)) -> Acc,
1083+
) -> impl FnMut(Acc, T) -> Acc {
1084+
move |acc, item| {
1085+
count -= 1;
1086+
fold(acc, (count, item))
1087+
}
1088+
}
1089+
1090+
let count = self.count + self.iter.len();
1091+
self.iter.rfold(init, enumerate(count, fold))
10691092
}
10701093
}
10711094

0 commit comments

Comments
 (0)