]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/slice.rs
rollup merge of #17355 : gamazeps/issue17210
[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::{Slice,SliceMut};
15
16 static mut COUNT: uint = 0;
17
18 struct Foo;
19
20 impl Slice<Foo, Foo> for Foo {
21     fn as_slice_<'a>(&'a self) -> &'a Foo {
22         unsafe { COUNT += 1; }
23         self
24     }
25     fn slice_from_<'a>(&'a self, _from: &Foo) -> &'a Foo {
26         unsafe { COUNT += 1; }
27         self
28     }
29     fn slice_to_<'a>(&'a self, _to: &Foo) -> &'a Foo {
30         unsafe { COUNT += 1; }
31         self
32     }
33     fn slice_<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
34         unsafe { COUNT += 1; }
35         self
36     }
37 }
38
39 impl SliceMut<Foo, Foo> for Foo {
40     fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
41         unsafe { COUNT += 1; }
42         self
43     }
44     fn slice_from_mut_<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
45         unsafe { COUNT += 1; }
46         self
47     }
48     fn slice_to_mut_<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
49         unsafe { COUNT += 1; }
50         self
51     }
52     fn slice_mut_<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
53         unsafe { COUNT += 1; }
54         self
55     }
56 }
57 fn main() {
58     let mut x = Foo;
59     x[];
60     x[Foo..];
61     x[..Foo];
62     x[Foo..Foo];
63     x[mut];
64     x[mut Foo..];
65     x[mut ..Foo];
66     x[mut Foo..Foo];
67     unsafe {
68         assert!(COUNT == 8);
69     }
70 }