Skip to content

Commit da6dcbc

Browse files
authored
Rollup merge of rust-lang#47944 - oberien:unboundediterator-trustedlen, r=bluss
Implement TrustedLen for Take<Repeat> and Take<RangeFrom> This will allow optimization of simple `repeat(x).take(n).collect()` iterators, which are currently not vectorized and have capacity checks. This will only support a few aggregates on `Repeat` and `RangeFrom`, which might be enough for simple cases, but doesn't optimize more complex ones. Namely, Cycle, StepBy, Filter, FilterMap, Peekable, SkipWhile, Skip, FlatMap, Fuse and Inspect are not marked `TrustedLen` when the inner iterator is infinite. Previous discussion can be found in rust-lang#47082 r? @alexcrichton
2 parents 0ba8712 + 6caec2c commit da6dcbc

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

src/libcore/iter/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2322,6 +2322,9 @@ impl<I> ExactSizeIterator for Take<I> where I: ExactSizeIterator {}
23222322
#[unstable(feature = "fused", issue = "35602")]
23232323
impl<I> FusedIterator for Take<I> where I: FusedIterator {}
23242324

2325+
#[unstable(feature = "trusted_len", issue = "37572")]
2326+
unsafe impl<I: TrustedLen> TrustedLen for Take<I> {}
2327+
23252328
/// An iterator to maintain state while iterating another iterator.
23262329
///
23272330
/// This `struct` is created by the [`scan`] method on [`Iterator`]. See its

src/libcore/iter/range.rs

+3
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
325325
#[unstable(feature = "fused", issue = "35602")]
326326
impl<A: Step> FusedIterator for ops::RangeFrom<A> {}
327327

328+
#[unstable(feature = "trusted_len", issue = "37572")]
329+
unsafe impl<A: Step> TrustedLen for ops::RangeFrom<A> {}
330+
328331
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
329332
impl<A: Step> Iterator for ops::RangeInclusive<A> {
330333
type Item = A;

src/libcore/iter/sources.rs

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ impl<A: Clone> DoubleEndedIterator for Repeat<A> {
4444
#[unstable(feature = "fused", issue = "35602")]
4545
impl<A: Clone> FusedIterator for Repeat<A> {}
4646

47+
#[unstable(feature = "trusted_len", issue = "37572")]
48+
unsafe impl<A: Clone> TrustedLen for Repeat<A> {}
49+
4750
/// Creates a new iterator that endlessly repeats a single element.
4851
///
4952
/// The `repeat()` function repeats a single value over and over and over and

src/libcore/iter/traits.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -970,9 +970,11 @@ impl<'a, I: FusedIterator + ?Sized> FusedIterator for &'a mut I {}
970970
/// The iterator reports a size hint where it is either exact
971971
/// (lower bound is equal to upper bound), or the upper bound is [`None`].
972972
/// The upper bound must only be [`None`] if the actual iterator length is
973-
/// larger than [`usize::MAX`].
973+
/// larger than [`usize::MAX`]. In that case, the lower bound must be
974+
/// [`usize::MAX`], resulting in a [`.size_hint`] of `(usize::MAX, None)`.
974975
///
975-
/// The iterator must produce exactly the number of elements it reported.
976+
/// The iterator must produce exactly the number of elements it reported
977+
/// or diverge before reaching the end.
976978
///
977979
/// # Safety
978980
///

src/libcore/tests/iter.rs

+43
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,29 @@ fn test_range_from_nth() {
13711371
assert_eq!(r, 16..);
13721372
assert_eq!(r.nth(10), Some(26));
13731373
assert_eq!(r, 27..);
1374+
1375+
assert_eq!((0..).size_hint(), (usize::MAX, None));
1376+
}
1377+
1378+
fn is_trusted_len<I: TrustedLen>(_: I) {}
1379+
1380+
#[test]
1381+
fn test_range_from_take() {
1382+
let mut it = (0..).take(3);
1383+
assert_eq!(it.next(), Some(0));
1384+
assert_eq!(it.next(), Some(1));
1385+
assert_eq!(it.next(), Some(2));
1386+
assert_eq!(it.next(), None);
1387+
is_trusted_len((0..).take(3));
1388+
assert_eq!((0..).take(3).size_hint(), (3, Some(3)));
1389+
assert_eq!((0..).take(0).size_hint(), (0, Some(0)));
1390+
assert_eq!((0..).take(usize::MAX).size_hint(), (usize::MAX, Some(usize::MAX)));
1391+
}
1392+
1393+
#[test]
1394+
fn test_range_from_take_collect() {
1395+
let v: Vec<_> = (0..).take(3).collect();
1396+
assert_eq!(v, vec![0, 1, 2]);
13741397
}
13751398

13761399
#[test]
@@ -1485,6 +1508,26 @@ fn test_repeat() {
14851508
assert_eq!(it.next(), Some(42));
14861509
assert_eq!(it.next(), Some(42));
14871510
assert_eq!(it.next(), Some(42));
1511+
assert_eq!(repeat(42).size_hint(), (usize::MAX, None));
1512+
}
1513+
1514+
#[test]
1515+
fn test_repeat_take() {
1516+
let mut it = repeat(42).take(3);
1517+
assert_eq!(it.next(), Some(42));
1518+
assert_eq!(it.next(), Some(42));
1519+
assert_eq!(it.next(), Some(42));
1520+
assert_eq!(it.next(), None);
1521+
is_trusted_len(repeat(42).take(3));
1522+
assert_eq!(repeat(42).take(3).size_hint(), (3, Some(3)));
1523+
assert_eq!(repeat(42).take(0).size_hint(), (0, Some(0)));
1524+
assert_eq!(repeat(42).take(usize::MAX).size_hint(), (usize::MAX, Some(usize::MAX)));
1525+
}
1526+
1527+
#[test]
1528+
fn test_repeat_take_collect() {
1529+
let v: Vec<_> = repeat(42).take(3).collect();
1530+
assert_eq!(v, vec![42, 42, 42]);
14881531
}
14891532

14901533
#[test]
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -O
12+
// ignore-tidy-linelength
13+
14+
#![crate_type = "lib"]
15+
16+
use std::iter;
17+
18+
// CHECK-LABEL: @repeat_take_collect
19+
#[no_mangle]
20+
pub fn repeat_take_collect() -> Vec<u8> {
21+
// CHECK: call void @llvm.memset.p0i8
22+
iter::repeat(42).take(100000).collect()
23+
}

0 commit comments

Comments
 (0)