File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
use std:: collections:: VecDeque ;
2
2
3
+ fn test_all_refs < ' a , T : ' a > ( dummy : & mut T , iter : impl Iterator < Item = & ' a mut T > ) {
4
+ // Gather all those references.
5
+ let mut refs: Vec < & mut T > = iter. collect ( ) ;
6
+ // Use them all. Twice, to be sure we got all interleavings.
7
+ for r in refs. iter_mut ( ) {
8
+ std:: mem:: swap ( dummy, r) ;
9
+ }
10
+ for r in refs {
11
+ std:: mem:: swap ( dummy, r) ;
12
+ }
13
+ }
14
+
3
15
fn main ( ) {
4
16
let mut dst = VecDeque :: new ( ) ;
5
17
dst. push_front ( Box :: new ( 1 ) ) ;
@@ -18,6 +30,21 @@ fn main() {
18
30
println ! ( "{:?}" , VecDeque :: <u32 >:: new( ) . iter( ) ) ;
19
31
20
32
for a in dst {
21
- assert_eq ! ( * a, 2 ) ;
33
+ assert_eq ! ( * a, 2 ) ;
22
34
}
35
+
36
+ // # Aliasing tests.
37
+ let mut v = std:: collections:: VecDeque :: new ( ) ;
38
+ v. push_back ( 1 ) ;
39
+ v. push_back ( 2 ) ;
40
+
41
+ // Test `fold` bad aliasing.
42
+ let mut it = v. iter_mut ( ) ;
43
+ let ref0 = it. next ( ) . unwrap ( ) ;
44
+ let sum = it. fold ( 0 , |x, y| x + * y) ;
45
+ assert_eq ! ( * ref0 + sum, 3 ) ;
46
+
47
+ // Test general iterator aliasing.
48
+ v. push_front ( 0 ) ;
49
+ test_all_refs ( & mut 0 , v. iter_mut ( ) ) ;
23
50
}
You can’t perform that action at this time.
0 commit comments