]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/assignability-trait.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / src / test / run-pass / assignability-trait.rs
CommitLineData
1a4d82fc 1// Copyright 2012-4 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
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// Tests that type assignability is used to search for instances when
12// making method calls, but only if there aren't any matches without
13// it.
14
15trait iterable<A> {
1a4d82fc 16 fn iterate<F>(&self, blk: F) -> bool where F: FnMut(&A) -> bool;
223e47cc
LB
17}
18
1a4d82fc
JJ
19impl<'a,A> iterable<A> for &'a [A] {
20 fn iterate<F>(&self, f: F) -> bool where F: FnMut(&A) -> bool {
21 self.iter().all(f)
223e47cc
LB
22 }
23}
24
1a4d82fc
JJ
25impl<A> iterable<A> for Vec<A> {
26 fn iterate<F>(&self, f: F) -> bool where F: FnMut(&A) -> bool {
27 self.iter().all(f)
223e47cc
LB
28 }
29}
30
c34b1796 31fn length<A, T: iterable<A>>(x: T) -> usize {
223e47cc 32 let mut len = 0;
1a4d82fc
JJ
33 x.iterate(|_y| {
34 len += 1;
35 true
36 });
223e47cc
LB
37 return len;
38}
39
40pub fn main() {
c34b1796 41 let x: Vec<isize> = vec!(0,1,2,3);
223e47cc 42 // Call a method
62682a34 43 x.iterate(|y| { assert_eq!(x[*y as usize], *y); true });
223e47cc 44 // Call a parameterized function
970d7e83 45 assert_eq!(length(x.clone()), x.len());
223e47cc
LB
46 // Call a parameterized function, with type arguments that require
47 // a borrow
c34b1796 48 assert_eq!(length::<isize, &[isize]>(&*x), x.len());
223e47cc
LB
49
50 // Now try it with a type that *needs* to be borrowed
51 let z = [0,1,2,3];
223e47cc 52 // Call a parameterized function
c34b1796 53 assert_eq!(length::<isize, &[isize]>(&z), z.len());
223e47cc 54}