]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/slice.rs
6a382c076d7b87e59fd4e9edb0637758bf985c50
[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(slicing_syntax)]
14 #![feature(associated_types)]
15
16 extern crate core;
17 use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, FullRange};
18
19 static mut COUNT: uint = 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<FullRange> for Foo {
45     type Output = Foo;
46     fn index(&self, _index: &FullRange) -> &Foo {
47         unsafe { COUNT += 1; }
48         self
49     }
50 }
51
52 impl IndexMut<Range<Foo>> for Foo {
53     type Output = Foo;
54     fn index_mut(&mut self, index: &Range<Foo>) -> &mut Foo {
55         unsafe { COUNT += 1; }
56         self
57     }
58 }
59 impl IndexMut<RangeTo<Foo>> for Foo {
60     type Output = Foo;
61     fn index_mut(&mut self, index: &RangeTo<Foo>) -> &mut Foo {
62         unsafe { COUNT += 1; }
63         self
64     }
65 }
66 impl IndexMut<RangeFrom<Foo>> for Foo {
67     type Output = Foo;
68     fn index_mut(&mut self, index: &RangeFrom<Foo>) -> &mut Foo {
69         unsafe { COUNT += 1; }
70         self
71     }
72 }
73 impl IndexMut<FullRange> for Foo {
74     type Output = Foo;
75     fn index_mut(&mut self, _index: &FullRange) -> &mut Foo {
76         unsafe { COUNT += 1; }
77         self
78     }
79 }
80
81
82 fn main() {
83     let mut x = Foo;
84     &x[];
85     &x[Foo..];
86     &x[..Foo];
87     &x[Foo..Foo];
88     &mut x[];
89     &mut x[Foo..];
90     &mut x[..Foo];
91     &mut x[Foo..Foo];
92     unsafe {
93         assert!(COUNT == 8);
94     }
95 }