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> { |
Patrick Walton | 8fa66e8 | 2013-03-13 02:32:14 | [diff] [blame] | 16 | fn iterate(&self, blk: &fn(x: &A) -> 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] { |
Patrick Walton | 8fa66e8 | 2013-03-13 02:32:14 | [diff] [blame] | 20 | fn iterate(&self, f: &fn(x: &A) -> bool) { |
Patrick Walton | ac60d53 | 2013-03-13 05:36:24 | [diff] [blame] | 21 | for vec::each(*self) |e| { |
Niko Matsakis | 9cf271f | 2012-09-19 04:41:37 | [diff] [blame] | 22 | if !f(e) { break; } |
| 23 | } |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
Patrick Walton | 9143688 | 2013-02-14 19:47:00 | [diff] [blame] | 27 | impl<A> iterable<A> for ~[A] { |
Patrick Walton | 8fa66e8 | 2013-03-13 02:32:14 | [diff] [blame] | 28 | fn iterate(&self, f: &fn(x: &A) -> bool) { |
Patrick Walton | ac60d53 | 2013-03-13 05:36:24 | [diff] [blame] | 29 | for vec::each(*self) |e| { |
Niko Matsakis | 9cf271f | 2012-09-19 04:41:37 | [diff] [blame] | 30 | if !f(e) { break; } |
| 31 | } |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | fn length<A, T: iterable<A>>(x: T) -> uint { |
| 36 | let mut len = 0; |
| 37 | for x.iterate() |_y| { len += 1 } |
Brian Anderson | b355936 | 2012-08-02 00:30:05 | [diff] [blame] | 38 | return len; |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 39 | } |
| 40 | |
Graydon Hoare | 89c8ef7 | 2013-02-02 03:43:17 | [diff] [blame] | 41 | pub fn main() { |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 42 | let x = ~[0,1,2,3]; |
| 43 | // Call a method |
Patrick Walton | d7e74b5 | 2013-03-06 21:58:02 | [diff] [blame] | 44 | for x.iterate() |y| { fail_unless!(x[*y] == *y); } |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 45 | // Call a parameterized function |
Ben Striegel | ee58424 | 2013-03-15 22:27:15 | [diff] [blame] | 46 | fail_unless!(length(x.clone()) == vec::len(x)); |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 47 | // Call a parameterized function, with type arguments that require |
| 48 | // a borrow |
Patrick Walton | d7e74b5 | 2013-03-06 21:58:02 | [diff] [blame] | 49 | fail_unless!(length::<int, &[int]>(x) == vec::len(x)); |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 50 | |
| 51 | // Now try it with a type that *needs* to be borrowed |
Ben Striegel | ac81fff | 2012-10-10 04:28:04 | [diff] [blame] | 52 | let z = [0,1,2,3]; |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 53 | // Call a method |
Patrick Walton | d7e74b5 | 2013-03-06 21:58:02 | [diff] [blame] | 54 | for z.iterate() |y| { fail_unless!(z[*y] == *y); } |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 55 | // Call a parameterized function |
Patrick Walton | d7e74b5 | 2013-03-06 21:58:02 | [diff] [blame] | 56 | fail_unless!(length::<int, &[int]>(z) == vec::len(z)); |
Michael Sullivan | 038f925 | 2012-07-06 22:50:50 | [diff] [blame] | 57 | } |