]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/slice.rs
auto merge of #20154 : P1start/rust/qualified-assoc-type-generics, r=nikomatsakis
[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
15 extern crate core;
16 use core::ops::{Slice,SliceMut};
17
18 static mut COUNT: uint = 0;
19
20 struct Foo;
21
22 impl Slice<Foo, Foo> for Foo {
23     fn as_slice_<'a>(&'a self) -> &'a Foo {
24         unsafe { COUNT += 1; }
25         self
26     }
27     fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
28         unsafe { COUNT += 1; }
29         self
30     }
31     fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
32         unsafe { COUNT += 1; }
33         self
34     }
35     fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
36         unsafe { COUNT += 1; }
37         self
38     }
39 }
40
41 impl SliceMut<Foo, Foo> for Foo {
42     fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
43         unsafe { COUNT += 1; }
44         self
45     }
46     fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
47         unsafe { COUNT += 1; }
48         self
49     }
50     fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
51         unsafe { COUNT += 1; }
52         self
53     }
54     fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
55         unsafe { COUNT += 1; }
56         self
57     }
58 }
59 fn main() {
60     let mut x = Foo;
61     x[];
62     x[Foo..];
63     x[..Foo];
64     x[Foo..Foo];
65     x[mut];
66     x[mut Foo..];
67     x[mut ..Foo];
68     x[mut Foo..Foo];
69     unsafe {
70         assert!(COUNT == 8);
71     }
72 }