]> git.proxmox.com Git - rustc.git/blob - src/test/ui/array-slice-vec/slice.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / src / test / ui / array-slice-vec / slice.rs
1 // run-pass
2 #![allow(unused_variables)]
3
4 // Test slicing sugar.
5
6 extern crate core;
7 use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull};
8
9 static mut COUNT: usize = 0;
10
11 struct Foo;
12
13 impl Index<Range<Foo>> for Foo {
14 type Output = Foo;
15 fn index(&self, index: Range<Foo>) -> &Foo {
16 unsafe { COUNT += 1; }
17 self
18 }
19 }
20 impl Index<RangeTo<Foo>> for Foo {
21 type Output = Foo;
22 fn index(&self, index: RangeTo<Foo>) -> &Foo {
23 unsafe { COUNT += 1; }
24 self
25 }
26 }
27 impl Index<RangeFrom<Foo>> for Foo {
28 type Output = Foo;
29 fn index(&self, index: RangeFrom<Foo>) -> &Foo {
30 unsafe { COUNT += 1; }
31 self
32 }
33 }
34 impl Index<RangeFull> for Foo {
35 type Output = Foo;
36 fn index(&self, _index: RangeFull) -> &Foo {
37 unsafe { COUNT += 1; }
38 self
39 }
40 }
41
42 impl IndexMut<Range<Foo>> for Foo {
43 fn index_mut(&mut self, index: Range<Foo>) -> &mut Foo {
44 unsafe { COUNT += 1; }
45 self
46 }
47 }
48 impl IndexMut<RangeTo<Foo>> for Foo {
49 fn index_mut(&mut self, index: RangeTo<Foo>) -> &mut Foo {
50 unsafe { COUNT += 1; }
51 self
52 }
53 }
54 impl IndexMut<RangeFrom<Foo>> for Foo {
55 fn index_mut(&mut self, index: RangeFrom<Foo>) -> &mut Foo {
56 unsafe { COUNT += 1; }
57 self
58 }
59 }
60 impl IndexMut<RangeFull> for Foo {
61 fn index_mut(&mut self, _index: RangeFull) -> &mut Foo {
62 unsafe { COUNT += 1; }
63 self
64 }
65 }
66
67
68 fn main() {
69 let mut x = Foo;
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];
78 unsafe {
79 assert_eq!(COUNT, 8);
80 }
81 }