test VecDeque::iter_mut aliasing
This commit is contained in:
parent
6a342a6ecb
commit
3e655665b7
1 changed files with 28 additions and 1 deletions
|
|
@ -1,5 +1,17 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {
|
||||
// Gather all those references.
|
||||
let mut refs: Vec<&mut T> = iter.collect();
|
||||
// Use them all. Twice, to be sure we got all interleavings.
|
||||
for r in refs.iter_mut() {
|
||||
std::mem::swap(dummy, r);
|
||||
}
|
||||
for r in refs {
|
||||
std::mem::swap(dummy, r);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut dst = VecDeque::new();
|
||||
dst.push_front(Box::new(1));
|
||||
|
|
@ -18,6 +30,21 @@ fn main() {
|
|||
println!("{:?}", VecDeque::<u32>::new().iter());
|
||||
|
||||
for a in dst {
|
||||
assert_eq!(*a, 2);
|
||||
assert_eq!(*a, 2);
|
||||
}
|
||||
|
||||
// # Aliasing tests.
|
||||
let mut v = std::collections::VecDeque::new();
|
||||
v.push_back(1);
|
||||
v.push_back(2);
|
||||
|
||||
// Test `fold` bad aliasing.
|
||||
let mut it = v.iter_mut();
|
||||
let ref0 = it.next().unwrap();
|
||||
let sum = it.fold(0, |x, y| x + *y);
|
||||
assert_eq!(*ref0 + sum, 3);
|
||||
|
||||
// Test general iterator aliasing.
|
||||
v.push_front(0);
|
||||
test_all_refs(&mut 0, v.iter_mut());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue