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