]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/auto-ref-slice-plus-ref.rs
auto merge of #6282 : alexcrichton/rust/issue-5517-add-test, r=thestinger
[rust.git] / src / test / run-pass / auto-ref-slice-plus-ref.rs
1 // Copyright 2012 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 // Testing that method lookup automatically both borrows vectors to slices
12 // and also references them to create the &self pointer
13
14 trait MyIter {
15     fn test_imm(&self);
16     fn test_const(&const self);
17 }
18
19 impl<'self> MyIter for &'self [int] {
20     fn test_imm(&self) { assert_eq!(self[0], 1) }
21     fn test_const(&const self) { assert_eq!(self[0], 1) }
22 }
23
24 impl<'self> MyIter for &'self str {
25     fn test_imm(&self) { assert_eq!(*self, "test") }
26     fn test_const(&const self) { assert_eq!(self[0], 't' as u8) }
27 }
28
29 pub fn main() {
30     // NB: Associativity of ~, etc. in this context is surprising. These must be parenthesized
31
32     ([1]).test_imm();
33     (~[1]).test_imm();
34     (@[1]).test_imm();
35     (&[1]).test_imm();
36     ("test").test_imm();
37     (~"test").test_imm();
38     (@"test").test_imm();
39     (&"test").test_imm();
40
41     // XXX: Other types of mutable vecs don't currently exist
42
43     ([1]).test_const();
44     (~[1]).test_const();
45     (@[1]).test_const();
46     (&[1]).test_const();
47     ("test").test_const();
48     (~"test").test_const();
49     (@"test").test_const();
50     (&"test").test_const();
51
52     // NB: We don't do this double autoreffing for &mut self because that would
53     // allow creating a mutable pointer to a temporary, which would be a source
54     // of confusion
55 }