]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/slice.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / run-pass / slice.rs
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
13 extern crate core;
14 use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull};
15
16 static mut COUNT: usize = 0;
17
18 struct Foo;
19
20 impl Index<Range<Foo>> for Foo {
21     type Output = Foo;
22     fn index(&self, index: Range<Foo>) -> &Foo {
23         unsafe { COUNT += 1; }
24         self
25     }
26 }
27 impl Index<RangeTo<Foo>> for Foo {
28     type Output = Foo;
29     fn index(&self, index: RangeTo<Foo>) -> &Foo {
30         unsafe { COUNT += 1; }
31         self
32     }
33 }
34 impl Index<RangeFrom<Foo>> for Foo {
35     type Output = Foo;
36     fn index(&self, index: RangeFrom<Foo>) -> &Foo {
37         unsafe { COUNT += 1; }
38         self
39     }
40 }
41 impl Index<RangeFull> for Foo {
42     type Output = Foo;
43     fn index(&self, _index: RangeFull) -> &Foo {
44         unsafe { COUNT += 1; }
45         self
46     }
47 }
48
49 impl IndexMut<Range<Foo>> for Foo {
50     fn index_mut(&mut self, index: Range<Foo>) -> &mut Foo {
51         unsafe { COUNT += 1; }
52         self
53     }
54 }
55 impl IndexMut<RangeTo<Foo>> for Foo {
56     fn index_mut(&mut self, index: RangeTo<Foo>) -> &mut Foo {
57         unsafe { COUNT += 1; }
58         self
59     }
60 }
61 impl IndexMut<RangeFrom<Foo>> for Foo {
62     fn index_mut(&mut self, index: RangeFrom<Foo>) -> &mut Foo {
63         unsafe { COUNT += 1; }
64         self
65     }
66 }
67 impl IndexMut<RangeFull> for Foo {
68     fn index_mut(&mut self, _index: RangeFull) -> &mut Foo {
69         unsafe { COUNT += 1; }
70         self
71     }
72 }
73
74
75 fn main() {
76     let mut x = Foo;
77     &x[..];
78     &x[Foo..];
79     &x[..Foo];
80     &x[Foo..Foo];
81     &mut x[..];
82     &mut x[Foo..];
83     &mut x[..Foo];
84     &mut x[Foo..Foo];
85     unsafe {
86         assert_eq!(COUNT, 8);
87     }
88 }