Skip to content

Commit 492a914

Browse files
committed
support in-place iteration for most adapters
`Take` is not included since users probably call it with small constants and it doesn't make sense to hold onto huge allocations in that case
1 parent 366afd9 commit 492a914

File tree

1 file changed

+114
-0
lines changed
  • src/libcore/iter/adapters

1 file changed

+114
-0
lines changed

src/libcore/iter/adapters/mod.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,22 @@ where
10931093
#[stable(feature = "fused", since = "1.26.0")]
10941094
impl<I: FusedIterator, P> FusedIterator for Filter<I, P> where P: FnMut(&I::Item) -> bool {}
10951095

1096+
#[unstable(issue = "0", feature = "inplace_iteration")]
1097+
unsafe impl<S: Iterator, P, I: Iterator> SourceIter for Filter<I, P> where
1098+
P: FnMut(&I::Item) -> bool,
1099+
I: SourceIter<Source = S>
1100+
{
1101+
type Source = S;
1102+
1103+
#[inline]
1104+
fn as_inner(&mut self) -> &mut S {
1105+
SourceIter::as_inner(&mut self.iter)
1106+
}
1107+
}
1108+
1109+
#[unstable(issue = "0", feature = "inplace_iteration")]
1110+
unsafe impl<I: InPlaceIterable, P> InPlaceIterable for Filter<I, P> where P: FnMut(&I::Item) -> bool {}
1111+
10961112
/// An iterator that uses `f` to both filter and map elements from `iter`.
10971113
///
10981114
/// This `struct` is created by the [`filter_map`] method on [`Iterator`]. See its
@@ -1219,6 +1235,23 @@ where
12191235
#[stable(feature = "fused", since = "1.26.0")]
12201236
impl<B, I: FusedIterator, F> FusedIterator for FilterMap<I, F> where F: FnMut(I::Item) -> Option<B> {}
12211237

1238+
#[unstable(issue = "0", feature = "inplace_iteration")]
1239+
unsafe impl<S: Iterator, B, I: Iterator, F> SourceIter for FilterMap<I, F> where
1240+
F: FnMut(I::Item) -> Option<B>,
1241+
I: SourceIter<Source = S>
1242+
{
1243+
type Source = S;
1244+
1245+
#[inline]
1246+
fn as_inner(&mut self) -> &mut S {
1247+
SourceIter::as_inner(&mut self.iter)
1248+
}
1249+
}
1250+
1251+
#[unstable(issue = "0", feature = "inplace_iteration")]
1252+
unsafe impl<B, I: InPlaceIterable, F> InPlaceIterable for FilterMap<I, F> where F: FnMut(I::Item) -> Option<B> {}
1253+
1254+
12221255
/// An iterator that yields the current count and the element during iteration.
12231256
///
12241257
/// This `struct` is created by the [`enumerate`] method on [`Iterator`]. See its
@@ -1777,6 +1810,22 @@ where
17771810
{
17781811
}
17791812

1813+
#[unstable(issue = "0", feature = "inplace_iteration")]
1814+
unsafe impl<S: Iterator, P, I: Iterator> SourceIter for SkipWhile<I, P> where
1815+
P: FnMut(&I::Item) -> bool,
1816+
I: SourceIter<Source = S>
1817+
{
1818+
type Source = S;
1819+
1820+
#[inline]
1821+
fn as_inner(&mut self) -> &mut S {
1822+
SourceIter::as_inner(&mut self.iter)
1823+
}
1824+
}
1825+
1826+
#[unstable(issue = "0", feature = "inplace_iteration")]
1827+
unsafe impl<I: InPlaceIterable, F> InPlaceIterable for SkipWhile<I, F> where F: FnMut(&I::Item) -> bool {}
1828+
17801829
/// An iterator that only accepts elements while `predicate` returns `true`.
17811830
///
17821831
/// This `struct` is created by the [`take_while`] method on [`Iterator`]. See its
@@ -1966,6 +2015,24 @@ where
19662015
{
19672016
}
19682017

2018+
2019+
#[unstable(issue = "0", feature = "inplace_iteration")]
2020+
unsafe impl<S: Iterator, P, I: Iterator> SourceIter for TakeWhile<I, P> where
2021+
P: FnMut(&I::Item) -> bool,
2022+
I: SourceIter<Source = S>
2023+
{
2024+
type Source = S;
2025+
2026+
#[inline]
2027+
fn as_inner(&mut self) -> &mut S {
2028+
SourceIter::as_inner(&mut self.iter)
2029+
}
2030+
}
2031+
2032+
#[unstable(issue = "0", feature = "inplace_iteration")]
2033+
unsafe impl<I: InPlaceIterable, F> InPlaceIterable for TakeWhile<I, F> where F: FnMut(&I::Item) -> bool {}
2034+
2035+
19692036
/// An iterator that skips over `n` elements of `iter`.
19702037
///
19712038
/// This `struct` is created by the [`skip`] method on [`Iterator`]. See its
@@ -2259,6 +2326,19 @@ where
22592326
}
22602327
}
22612328

2329+
#[unstable(issue = "0", feature = "inplace_iteration")]
2330+
unsafe impl<S: Iterator, I: Iterator> SourceIter for Take<I> where I: SourceIter<Source = S> {
2331+
type Source = S;
2332+
2333+
#[inline]
2334+
fn as_inner(&mut self) -> &mut S {
2335+
SourceIter::as_inner(&mut self.iter)
2336+
}
2337+
}
2338+
2339+
#[unstable(issue = "0", feature = "inplace_iteration")]
2340+
unsafe impl<I: InPlaceIterable> InPlaceIterable for Take<I> {}
2341+
22622342
#[stable(feature = "double_ended_take_iterator", since = "1.38.0")]
22632343
impl<I> DoubleEndedIterator for Take<I>
22642344
where
@@ -2391,6 +2471,24 @@ where
23912471
}
23922472
}
23932473

2474+
#[unstable(issue = "0", feature = "inplace_iteration")]
2475+
unsafe impl<St, F, B, S: Iterator, I: Iterator> SourceIter for Scan<I, St, F>
2476+
where I: SourceIter<Source = S>,
2477+
F: FnMut(&mut St, I::Item) -> Option<B>,
2478+
{
2479+
type Source = S;
2480+
2481+
#[inline]
2482+
fn as_inner(&mut self) -> &mut S {
2483+
SourceIter::as_inner(&mut self.iter)
2484+
}
2485+
}
2486+
2487+
#[unstable(issue = "0", feature = "inplace_iteration")]
2488+
unsafe impl<St, F, B, I: InPlaceIterable> InPlaceIterable for Scan<I, St, F>
2489+
where F: FnMut(&mut St, I::Item) -> Option<B>,
2490+
{}
2491+
23942492
/// An iterator that yields `None` forever after the underlying iterator
23952493
/// yields `None` once.
23962494
///
@@ -2808,6 +2906,22 @@ where
28082906
#[stable(feature = "fused", since = "1.26.0")]
28092907
impl<I: FusedIterator, F> FusedIterator for Inspect<I, F> where F: FnMut(&I::Item) {}
28102908

2909+
#[unstable(issue = "0", feature = "inplace_iteration")]
2910+
unsafe impl<S: Iterator, I: Iterator, F> SourceIter for Inspect<I, F> where
2911+
F: FnMut(&I::Item),
2912+
I: SourceIter<Source = S>
2913+
{
2914+
type Source = S;
2915+
2916+
#[inline]
2917+
fn as_inner(&mut self) -> &mut S {
2918+
SourceIter::as_inner(&mut self.iter)
2919+
}
2920+
}
2921+
2922+
#[unstable(issue = "0", feature = "inplace_iteration")]
2923+
unsafe impl<I: InPlaceIterable, F> InPlaceIterable for Inspect<I, F> where F: FnMut(&I::Item) {}
2924+
28112925
/// An iterator adapter that produces output as long as the underlying
28122926
/// iterator produces `Result::Ok` values.
28132927
///

0 commit comments

Comments
 (0)