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