Skip to content

Commit df3d686

Browse files
committed
Reduce genericity in Enumerate
1 parent ac113f0 commit df3d686

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

@@ -994,14 +994,12 @@ impl<I> Iterator for Enumerate<I> where I: Iterator {
994994
///
995995
/// Might panic if the index of the element overflows a `usize`.
996996
#[inline]
997-
#[rustc_inherit_overflow_checks]
998997
fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
999-
self.iter.next().map(|a| {
1000-
let ret = (self.count, a);
1001-
// Possible undefined overflow.
1002-
self.count += 1;
1003-
ret
1004-
})
998+
let a = self.iter.next()?;
999+
let i = self.count;
1000+
// Possible undefined overflow.
1001+
AddAssign::add_assign(&mut self.count, 1);
1002+
Some((i, a))
10051003
}
10061004

10071005
#[inline]
@@ -1010,13 +1008,12 @@ impl<I> Iterator for Enumerate<I> where I: Iterator {
10101008
}
10111009

10121010
#[inline]
1013-
#[rustc_inherit_overflow_checks]
10141011
fn nth(&mut self, n: usize) -> Option<(usize, I::Item)> {
1015-
self.iter.nth(n).map(|a| {
1016-
let i = self.count + n;
1017-
self.count = i + 1;
1018-
(i, a)
1019-
})
1012+
let a = self.iter.nth(n)?;
1013+
// Possible undefined overflow.
1014+
let i = Add::add(self.count, n);
1015+
self.count = Add::add(i, 1);
1016+
Some((i, a))
10201017
}
10211018

10221019
#[inline]
@@ -1025,29 +1022,43 @@ impl<I> Iterator for Enumerate<I> where I: Iterator {
10251022
}
10261023

10271024
#[inline]
1028-
#[rustc_inherit_overflow_checks]
1029-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
1025+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
10301026
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
10311027
{
1032-
let count = &mut self.count;
1033-
self.iter.try_fold(init, move |acc, item| {
1034-
let acc = fold(acc, (*count, item));
1035-
*count += 1;
1036-
acc
1037-
})
1028+
#[inline]
1029+
fn enumerate<'a, T, Acc, R>(
1030+
count: &'a mut usize,
1031+
mut fold: impl FnMut(Acc, (usize, T)) -> R + 'a,
1032+
) -> impl FnMut(Acc, T) -> R + 'a {
1033+
move |acc, item| {
1034+
let acc = fold(acc, (*count, item));
1035+
// Possible undefined overflow.
1036+
AddAssign::add_assign(count, 1);
1037+
acc
1038+
}
1039+
}
1040+
1041+
self.iter.try_fold(init, enumerate(&mut self.count, fold))
10381042
}
10391043

10401044
#[inline]
1041-
#[rustc_inherit_overflow_checks]
1042-
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
1045+
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
10431046
where Fold: FnMut(Acc, Self::Item) -> Acc,
10441047
{
1045-
let mut count = self.count;
1046-
self.iter.fold(init, move |acc, item| {
1047-
let acc = fold(acc, (count, item));
1048-
count += 1;
1049-
acc
1050-
})
1048+
#[inline]
1049+
fn enumerate<T, Acc>(
1050+
mut count: usize,
1051+
mut fold: impl FnMut(Acc, (usize, T)) -> Acc,
1052+
) -> impl FnMut(Acc, T) -> Acc {
1053+
move |acc, item| {
1054+
let acc = fold(acc, (count, item));
1055+
// Possible undefined overflow.
1056+
AddAssign::add_assign(&mut count, 1);
1057+
acc
1058+
}
1059+
}
1060+
1061+
self.iter.fold(init, enumerate(self.count, fold))
10511062
}
10521063
}
10531064

@@ -1057,48 +1068,60 @@ impl<I> DoubleEndedIterator for Enumerate<I> where
10571068
{
10581069
#[inline]
10591070
fn next_back(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
1060-
self.iter.next_back().map(|a| {
1061-
let len = self.iter.len();
1062-
// Can safely add, `ExactSizeIterator` promises that the number of
1063-
// elements fits into a `usize`.
1064-
(self.count + len, a)
1065-
})
1071+
let a = self.iter.next_back()?;
1072+
let len = self.iter.len();
1073+
// Can safely add, `ExactSizeIterator` promises that the number of
1074+
// elements fits into a `usize`.
1075+
Some((self.count + len, a))
10661076
}
10671077

10681078
#[inline]
10691079
fn nth_back(&mut self, n: usize) -> Option<(usize, <I as Iterator>::Item)> {
1070-
self.iter.nth_back(n).map(|a| {
1071-
let len = self.iter.len();
1072-
// Can safely add, `ExactSizeIterator` promises that the number of
1073-
// elements fits into a `usize`.
1074-
(self.count + len, a)
1075-
})
1080+
let a = self.iter.nth_back(n)?;
1081+
let len = self.iter.len();
1082+
// Can safely add, `ExactSizeIterator` promises that the number of
1083+
// elements fits into a `usize`.
1084+
Some((self.count + len, a))
10761085
}
10771086

10781087
#[inline]
1079-
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
1088+
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
10801089
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
10811090
{
10821091
// Can safely add and subtract the count, as `ExactSizeIterator` promises
10831092
// that the number of elements fits into a `usize`.
1084-
let mut count = self.count + self.iter.len();
1085-
self.iter.try_rfold(init, move |acc, item| {
1086-
count -= 1;
1087-
fold(acc, (count, item))
1088-
})
1093+
fn enumerate<T, Acc, R>(
1094+
mut count: usize,
1095+
mut fold: impl FnMut(Acc, (usize, T)) -> R,
1096+
) -> impl FnMut(Acc, T) -> R {
1097+
move |acc, item| {
1098+
count -= 1;
1099+
fold(acc, (count, item))
1100+
}
1101+
}
1102+
1103+
let count = self.count + self.iter.len();
1104+
self.iter.try_rfold(init, enumerate(count, fold))
10891105
}
10901106

10911107
#[inline]
1092-
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
1108+
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
10931109
where Fold: FnMut(Acc, Self::Item) -> Acc,
10941110
{
10951111
// Can safely add and subtract the count, as `ExactSizeIterator` promises
10961112
// that the number of elements fits into a `usize`.
1097-
let mut count = self.count + self.iter.len();
1098-
self.iter.rfold(init, move |acc, item| {
1099-
count -= 1;
1100-
fold(acc, (count, item))
1101-
})
1113+
fn enumerate<T, Acc>(
1114+
mut count: usize,
1115+
mut fold: impl FnMut(Acc, (usize, T)) -> Acc,
1116+
) -> impl FnMut(Acc, T) -> Acc {
1117+
move |acc, item| {
1118+
count -= 1;
1119+
fold(acc, (count, item))
1120+
}
1121+
}
1122+
1123+
let count = self.count + self.iter.len();
1124+
self.iter.rfold(init, enumerate(count, fold))
11021125
}
11031126
}
11041127

0 commit comments

Comments
 (0)