Skip to content

Rollup of 10 pull requests #42258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f8b66a0
trace_macro: Show both the macro call and its expansion. #42072.
jorendorff May 19, 2017
dec23d4
Update Rc and Arc documentation.
nical May 21, 2017
57f260d
Override size_hint and propagate ExactSizeIterator for iter::StepBy
scottmcm May 23, 2017
4be488c
Add iterator_step_by to the unstable book's summary
scottmcm May 21, 2017
fcb3a71
Update description of iter::StepBy
scottmcm May 21, 2017
21dd71f
incr.comp.: Track expanded spans instead of FileMaps.
michaelwoerister May 23, 2017
fb9ca16
Remove all instances of fragment_infos and fragment sets
Nashenas88 May 24, 2017
87950b7
Stabilize non capturing closure to fn coercion
est31 May 23, 2017
a563f35
Remove irrelevant tests and unused testing attribute
Nashenas88 May 25, 2017
c87f6d8
regression test for #39974
venkatagiri May 25, 2017
1a9c7b2
Updated locked version of libgit2
alexcrichton May 26, 2017
9d01840
extend `struct_tail` to operate over closures
nikomatsakis May 26, 2017
bf87e17
Allow variadic functions with cdecl calling convention.
Mark-Simulacrum May 26, 2017
53cbcef
Rollup merge of #42103 - jorendorff:master, r=estebank
Mark-Simulacrum May 27, 2017
de5549e
Rollup merge of #42137 - nical:doc-clone, r=BurntSushi
Mark-Simulacrum May 27, 2017
d11e112
Rollup merge of #42162 - est31:closure-to-fn-coercion, r=aturon
Mark-Simulacrum May 27, 2017
e313011
Rollup merge of #42167 - scottmcm:iter-stepby-sizehint, r=alexcrichton
Mark-Simulacrum May 27, 2017
342a709
Rollup merge of #42175 - michaelwoerister:filemap-hashing-fix-1, r=ni…
Mark-Simulacrum May 27, 2017
20135a8
Rollup merge of #42207 - Nashenas88:remove_fragment_info, r=eddyb
Mark-Simulacrum May 27, 2017
ec03463
Rollup merge of #42217 - venkatagiri:issue_39974, r=Mark-Simulacrum
Mark-Simulacrum May 27, 2017
8884b82
Rollup merge of #42246 - alexcrichton:update-cargo, r=nikomatsakis
Mark-Simulacrum May 27, 2017
c582790
Rollup merge of #42249 - Mark-Simulacrum:issue-40244, r=eddyb
Mark-Simulacrum May 27, 2017
7ffeb85
Rollup merge of #42251 - nikomatsakis:issue-42210-regr-unsized-tail, …
Mark-Simulacrum May 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
- [io](library-features/io.md)
- [ip](library-features/ip.md)
- [iter_rfind](library-features/iter-rfind.md)
- [iterator_step_by](library-features/iterator-step-by.md)
- [libstd_io_internals](library-features/libstd-io-internals.md)
- [libstd_sys_internals](library-features/libstd-sys-internals.md)
- [libstd_thread_internals](library-features/libstd-thread-internals.md)
Expand Down
21 changes: 20 additions & 1 deletion src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ impl<I> Iterator for Cycle<I> where I: Clone + Iterator {
#[unstable(feature = "fused", issue = "35602")]
impl<I> FusedIterator for Cycle<I> where I: Clone + Iterator {}

/// An iterator that steps by n elements every iteration.
/// An adapter for stepping iterators by a custom amount.
///
/// This `struct` is created by the [`step_by`] method on [`Iterator`]. See
/// its documentation for more.
Expand Down Expand Up @@ -553,8 +553,27 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
self.iter.nth(self.step)
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let inner_hint = self.iter.size_hint();

if self.first_take {
let f = |n| if n == 0 { 0 } else { 1 + (n-1)/(self.step+1) };
(f(inner_hint.0), inner_hint.1.map(f))
} else {
let f = |n| n / (self.step+1);
(f(inner_hint.0), inner_hint.1.map(f))
}
}
}

// StepBy can only make the iterator shorter, so the len will still fit.
#[unstable(feature = "iterator_step_by",
reason = "unstable replacement of Range::step_by",
issue = "27741")]
impl<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator {}

/// An iterator that strings two iterators together.
///
/// This `struct` is created by the [`chain`] method on [`Iterator`]. See its
Expand Down
73 changes: 73 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,79 @@ fn test_iterator_step_by_zero() {
it.next();
}

#[test]
fn test_iterator_step_by_size_hint() {
struct StubSizeHint(usize, Option<usize>);
impl Iterator for StubSizeHint {
type Item = ();
fn next(&mut self) -> Option<()> {
self.0 -= 1;
if let Some(ref mut upper) = self.1 {
*upper -= 1;
}
Some(())
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.0, self.1)
}
}

// The two checks in each case are needed because the logic
// is different before the first call to `next()`.

let mut it = StubSizeHint(10, Some(10)).step_by(1);
assert_eq!(it.size_hint(), (10, Some(10)));
it.next();
assert_eq!(it.size_hint(), (9, Some(9)));

// exact multiple
let mut it = StubSizeHint(10, Some(10)).step_by(3);
assert_eq!(it.size_hint(), (4, Some(4)));
it.next();
assert_eq!(it.size_hint(), (3, Some(3)));

// larger base range, but not enough to get another element
let mut it = StubSizeHint(12, Some(12)).step_by(3);
assert_eq!(it.size_hint(), (4, Some(4)));
it.next();
assert_eq!(it.size_hint(), (3, Some(3)));

// smaller base range, so fewer resulting elements
let mut it = StubSizeHint(9, Some(9)).step_by(3);
assert_eq!(it.size_hint(), (3, Some(3)));
it.next();
assert_eq!(it.size_hint(), (2, Some(2)));

// infinite upper bound
let mut it = StubSizeHint(usize::MAX, None).step_by(1);
assert_eq!(it.size_hint(), (usize::MAX, None));
it.next();
assert_eq!(it.size_hint(), (usize::MAX-1, None));

// still infinite with larger step
let mut it = StubSizeHint(7, None).step_by(3);
assert_eq!(it.size_hint(), (3, None));
it.next();
assert_eq!(it.size_hint(), (2, None));

// propagates ExactSizeIterator
let a = [1,2,3,4,5];
let it = a.iter().step_by(2);
assert_eq!(it.len(), 3);

// Cannot be TrustedLen as a step greater than one makes an iterator
// with (usize::MAX, None) no longer meet the safety requirements
trait TrustedLenCheck { fn test(self) -> bool; }
impl<T:Iterator> TrustedLenCheck for T {
default fn test(self) -> bool { false }
}
impl<T:TrustedLen> TrustedLenCheck for T {
fn test(self) -> bool { true }
}
assert!(TrustedLenCheck::test(a.iter()));
assert!(!TrustedLenCheck::test(a.iter().step_by(1)));
}

#[test]
fn test_filter_map() {
let it = (0..).step_by(1).take(10)
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
#![feature(slice_patterns)]
#![feature(sort_internals)]
#![feature(sort_unstable)]
#![feature(specialization)]
#![feature(step_by)]
#![feature(step_trait)]
#![feature(test)]
#![feature(trusted_len)]
#![feature(try_from)]
#![feature(unicode)]
#![feature(unique)]
Expand Down