]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | // Test slicing sugar. | |
12 | ||
c34b1796 AL |
13 | // pretty-expanded FIXME #23616 |
14 | ||
15 | #![feature(core)] | |
1a4d82fc JJ |
16 | |
17 | extern crate core; | |
85aaf69f | 18 | use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull}; |
1a4d82fc | 19 | |
c34b1796 | 20 | static mut COUNT: usize = 0; |
1a4d82fc JJ |
21 | |
22 | struct Foo; | |
23 | ||
24 | impl Index<Range<Foo>> for Foo { | |
25 | type Output = Foo; | |
c34b1796 | 26 | fn index(&self, index: Range<Foo>) -> &Foo { |
1a4d82fc JJ |
27 | unsafe { COUNT += 1; } |
28 | self | |
29 | } | |
30 | } | |
31 | impl Index<RangeTo<Foo>> for Foo { | |
32 | type Output = Foo; | |
c34b1796 | 33 | fn index(&self, index: RangeTo<Foo>) -> &Foo { |
1a4d82fc JJ |
34 | unsafe { COUNT += 1; } |
35 | self | |
36 | } | |
37 | } | |
38 | impl Index<RangeFrom<Foo>> for Foo { | |
39 | type Output = Foo; | |
c34b1796 | 40 | fn index(&self, index: RangeFrom<Foo>) -> &Foo { |
1a4d82fc JJ |
41 | unsafe { COUNT += 1; } |
42 | self | |
43 | } | |
44 | } | |
85aaf69f | 45 | impl Index<RangeFull> for Foo { |
1a4d82fc | 46 | type Output = Foo; |
c34b1796 | 47 | fn index(&self, _index: RangeFull) -> &Foo { |
1a4d82fc JJ |
48 | unsafe { COUNT += 1; } |
49 | self | |
50 | } | |
51 | } | |
52 | ||
53 | impl IndexMut<Range<Foo>> for Foo { | |
c34b1796 | 54 | fn index_mut(&mut self, index: Range<Foo>) -> &mut Foo { |
1a4d82fc JJ |
55 | unsafe { COUNT += 1; } |
56 | self | |
57 | } | |
58 | } | |
59 | impl IndexMut<RangeTo<Foo>> for Foo { | |
c34b1796 | 60 | fn index_mut(&mut self, index: RangeTo<Foo>) -> &mut Foo { |
1a4d82fc JJ |
61 | unsafe { COUNT += 1; } |
62 | self | |
63 | } | |
64 | } | |
65 | impl IndexMut<RangeFrom<Foo>> for Foo { | |
c34b1796 | 66 | fn index_mut(&mut self, index: RangeFrom<Foo>) -> &mut Foo { |
1a4d82fc JJ |
67 | unsafe { COUNT += 1; } |
68 | self | |
69 | } | |
70 | } | |
85aaf69f | 71 | impl IndexMut<RangeFull> for Foo { |
c34b1796 | 72 | fn index_mut(&mut self, _index: RangeFull) -> &mut Foo { |
1a4d82fc JJ |
73 | unsafe { COUNT += 1; } |
74 | self | |
75 | } | |
76 | } | |
77 | ||
78 | ||
79 | fn main() { | |
80 | let mut x = Foo; | |
85aaf69f | 81 | &x[..]; |
1a4d82fc JJ |
82 | &x[Foo..]; |
83 | &x[..Foo]; | |
84 | &x[Foo..Foo]; | |
85aaf69f | 85 | &mut x[..]; |
1a4d82fc JJ |
86 | &mut x[Foo..]; |
87 | &mut x[..Foo]; | |
88 | &mut x[Foo..Foo]; | |
89 | unsafe { | |
90 | assert!(COUNT == 8); | |
91 | } | |
92 | } |