Graydon Hoare | d1affff | 2012-12-11 01:32:48 | [diff] [blame] | 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 | |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 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 | |
Lindsey Kuper | 439afaa | 2012-07-31 17:27:51 | [diff] [blame] | 15 | trait iterable<A> { |
Alex Crichton | b05aae2 | 2013-05-03 20:33:33 | [diff] [blame] | 16 | fn iterate(&self, blk: &fn(x: &A) -> bool) -> bool; |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 17 | } |
| 18 | |
Patrick Walton | 8b56a83 | 2013-03-25 20:21:04 | [diff] [blame] | 19 | impl<'self,A> iterable<A> for &'self [A] { |
Alex Crichton | b05aae2 | 2013-05-03 20:33:33 | [diff] [blame] | 20 | fn iterate(&self, f: &fn(x: &A) -> bool) -> bool { |
Daniel Micay | e67c48a | 2013-06-24 22:34:20 | [diff] [blame] | 21 | self.iter().advance(f) |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
Patrick Walton | 9143688 | 2013-02-14 19:47:00 | [diff] [blame] | 25 | impl<A> iterable<A> for ~[A] { |
Alex Crichton | b05aae2 | 2013-05-03 20:33:33 | [diff] [blame] | 26 | fn iterate(&self, f: &fn(x: &A) -> bool) -> bool { |
Daniel Micay | e67c48a | 2013-06-24 22:34:20 | [diff] [blame] | 27 | self.iter().advance(f) |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
| 31 | fn length<A, T: iterable<A>>(x: T) -> uint { |
| 32 | let mut len = 0; |
Daniel Micay | b3ad685 | 2013-08-02 06:17:20 | [diff] [blame] | 33 | do x.iterate() |_y| { len += 1; true }; |
Brian Anderson | b355936 | 2012-08-02 00:30:05 | [diff] [blame] | 34 | return len; |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 35 | } |
| 36 | |
Graydon Hoare | 89c8ef7 | 2013-02-02 03:43:17 | [diff] [blame] | 37 | pub fn main() { |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 38 | let x = ~[0,1,2,3]; |
| 39 | // Call a method |
Daniel Micay | b3ad685 | 2013-08-02 06:17:20 | [diff] [blame] | 40 | do x.iterate() |y| { assert!(x[*y] == *y); true }; |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 41 | // Call a parameterized function |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 42 | assert_eq!(length(x.clone()), x.len()); |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 43 | // Call a parameterized function, with type arguments that require |
| 44 | // a borrow |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 45 | assert_eq!(length::<int, &[int]>(x), x.len()); |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 46 | |
| 47 | // Now try it with a type that *needs* to be borrowed |
Ben Striegel | ac81fff | 2012-10-10 04:28:04 | [diff] [blame] | 48 | let z = [0,1,2,3]; |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 49 | // Call a method |
Daniel Micay | b3ad685 | 2013-08-02 06:17:20 | [diff] [blame] | 50 | do z.iterate() |y| { assert!(z[*y] == *y); true }; |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 51 | // Call a parameterized function |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 52 | assert_eq!(length::<int, &[int]>(z), z.len()); |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 53 | } |