Rollup merge of #56538 - xfix:patch-13, r=bluss

Use inner iterator may_have_side_effect for Cloned

Previous implementation wasn't correct, as an inner iterator could have had side effects. Noticed by @bluss in #56534.
This commit is contained in:
Pietro Albini 2018-12-05 23:54:39 +01:00 committed by GitHub
commit f8ee5ab803
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -602,7 +602,9 @@ unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
}
#[inline]
fn may_have_side_effect() -> bool { false }
fn may_have_side_effect() -> bool {
I::may_have_side_effect()
}
}
#[unstable(feature = "trusted_len", issue = "37572")]

View file

@ -1249,6 +1249,23 @@ fn test_cloned() {
assert_eq!(it.next_back(), None);
}
#[test]
fn test_cloned_side_effects() {
let mut count = 0;
{
let iter = [1, 2, 3]
.iter()
.map(|x| {
count += 1;
x
})
.cloned()
.zip(&[1]);
for _ in iter {}
}
assert_eq!(count, 2);
}
#[test]
fn test_double_ended_map() {
let xs = [1, 2, 3, 4, 5, 6];