blob: c9e42aadfd4462b0c79c1a86478f0997c2e88929 [file] [log] [blame]
Graydon Hoared1affff2012-12-11 01:32:481// 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 Sullivan038f9252012-07-06 22:50:5011// 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 Kuper439afaa2012-07-31 17:27:5115trait iterable<A> {
Patrick Walton8fa66e82013-03-13 02:32:1416 fn iterate(&self, blk: &fn(x: &A) -> bool);
Michael Sullivan038f9252012-07-06 22:50:5017}
18
Patrick Walton8b56a832013-03-25 20:21:0419impl<'self,A> iterable<A> for &'self [A] {
Patrick Walton8fa66e82013-03-13 02:32:1420 fn iterate(&self, f: &fn(x: &A) -> bool) {
Patrick Waltonac60d532013-03-13 05:36:2421 for vec::each(*self) |e| {
Niko Matsakis9cf271f2012-09-19 04:41:3722 if !f(e) { break; }
23 }
Michael Sullivan038f9252012-07-06 22:50:5024 }
25}
26
Patrick Walton91436882013-02-14 19:47:0027impl<A> iterable<A> for ~[A] {
Patrick Walton8fa66e82013-03-13 02:32:1428 fn iterate(&self, f: &fn(x: &A) -> bool) {
Patrick Waltonac60d532013-03-13 05:36:2429 for vec::each(*self) |e| {
Niko Matsakis9cf271f2012-09-19 04:41:3730 if !f(e) { break; }
31 }
Michael Sullivan038f9252012-07-06 22:50:5032 }
33}
34
35fn length<A, T: iterable<A>>(x: T) -> uint {
36 let mut len = 0;
37 for x.iterate() |_y| { len += 1 }
Brian Andersonb3559362012-08-02 00:30:0538 return len;
Michael Sullivan038f9252012-07-06 22:50:5039}
40
Graydon Hoare89c8ef72013-02-02 03:43:1741pub fn main() {
Michael Sullivan038f9252012-07-06 22:50:5042 let x = ~[0,1,2,3];
43 // Call a method
Patrick Waltond7e74b52013-03-06 21:58:0244 for x.iterate() |y| { fail_unless!(x[*y] == *y); }
Michael Sullivan038f9252012-07-06 22:50:5045 // Call a parameterized function
Ben Striegelee584242013-03-15 22:27:1546 fail_unless!(length(x.clone()) == vec::len(x));
Michael Sullivan038f9252012-07-06 22:50:5047 // Call a parameterized function, with type arguments that require
48 // a borrow
Patrick Waltond7e74b52013-03-06 21:58:0249 fail_unless!(length::<int, &[int]>(x) == vec::len(x));
Michael Sullivan038f9252012-07-06 22:50:5050
51 // Now try it with a type that *needs* to be borrowed
Ben Striegelac81fff2012-10-10 04:28:0452 let z = [0,1,2,3];
Michael Sullivan038f9252012-07-06 22:50:5053 // Call a method
Patrick Waltond7e74b52013-03-06 21:58:0254 for z.iterate() |y| { fail_unless!(z[*y] == *y); }
Michael Sullivan038f9252012-07-06 22:50:5055 // Call a parameterized function
Patrick Waltond7e74b52013-03-06 21:58:0256 fail_unless!(length::<int, &[int]>(z) == vec::len(z));
Michael Sullivan038f9252012-07-06 22:50:5057}