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:
commit
f8ee5ab803
2 changed files with 20 additions and 1 deletions
|
@ -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")]
|
||||
|
|
|
@ -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];
|
||||
|
|
Loading…
Reference in a new issue