]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
0bf4aa26 | 2 | #![allow(unused_variables)] |
c34b1796 | 3 | |
b7449926 | 4 | // Test slicing sugar. |
1a4d82fc JJ |
5 | |
6 | extern crate core; | |
85aaf69f | 7 | use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull}; |
1a4d82fc | 8 | |
c34b1796 | 9 | static mut COUNT: usize = 0; |
1a4d82fc JJ |
10 | |
11 | struct Foo; | |
12 | ||
13 | impl Index<Range<Foo>> for Foo { | |
14 | type Output = Foo; | |
c34b1796 | 15 | fn index(&self, index: Range<Foo>) -> &Foo { |
1a4d82fc JJ |
16 | unsafe { COUNT += 1; } |
17 | self | |
18 | } | |
19 | } | |
20 | impl Index<RangeTo<Foo>> for Foo { | |
21 | type Output = Foo; | |
c34b1796 | 22 | fn index(&self, index: RangeTo<Foo>) -> &Foo { |
1a4d82fc JJ |
23 | unsafe { COUNT += 1; } |
24 | self | |
25 | } | |
26 | } | |
27 | impl Index<RangeFrom<Foo>> for Foo { | |
28 | type Output = Foo; | |
c34b1796 | 29 | fn index(&self, index: RangeFrom<Foo>) -> &Foo { |
1a4d82fc JJ |
30 | unsafe { COUNT += 1; } |
31 | self | |
32 | } | |
33 | } | |
85aaf69f | 34 | impl Index<RangeFull> for Foo { |
1a4d82fc | 35 | type Output = Foo; |
c34b1796 | 36 | fn index(&self, _index: RangeFull) -> &Foo { |
1a4d82fc JJ |
37 | unsafe { COUNT += 1; } |
38 | self | |
39 | } | |
40 | } | |
41 | ||
42 | impl IndexMut<Range<Foo>> for Foo { | |
c34b1796 | 43 | fn index_mut(&mut self, index: Range<Foo>) -> &mut Foo { |
1a4d82fc JJ |
44 | unsafe { COUNT += 1; } |
45 | self | |
46 | } | |
47 | } | |
48 | impl IndexMut<RangeTo<Foo>> for Foo { | |
c34b1796 | 49 | fn index_mut(&mut self, index: RangeTo<Foo>) -> &mut Foo { |
1a4d82fc JJ |
50 | unsafe { COUNT += 1; } |
51 | self | |
52 | } | |
53 | } | |
54 | impl IndexMut<RangeFrom<Foo>> for Foo { | |
c34b1796 | 55 | fn index_mut(&mut self, index: RangeFrom<Foo>) -> &mut Foo { |
1a4d82fc JJ |
56 | unsafe { COUNT += 1; } |
57 | self | |
58 | } | |
59 | } | |
85aaf69f | 60 | impl IndexMut<RangeFull> for Foo { |
c34b1796 | 61 | fn index_mut(&mut self, _index: RangeFull) -> &mut Foo { |
1a4d82fc JJ |
62 | unsafe { COUNT += 1; } |
63 | self | |
64 | } | |
65 | } | |
66 | ||
67 | ||
68 | fn main() { | |
69 | let mut x = Foo; | |
136023e0 XL |
70 | let _ = &x[..]; |
71 | let _ = &x[Foo..]; | |
72 | let _ = &x[..Foo]; | |
73 | let _ = &x[Foo..Foo]; | |
74 | let _ = &mut x[..]; | |
75 | let _ = &mut x[Foo..]; | |
76 | let _ = &mut x[..Foo]; | |
77 | let _ = &mut x[Foo..Foo]; | |
1a4d82fc | 78 | unsafe { |
62682a34 | 79 | assert_eq!(COUNT, 8); |
1a4d82fc JJ |
80 | } |
81 | } |