blob: 5269f7dab5aad92570635f8608b7adf0924674b5 [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> {
Alex Crichtonb05aae22013-05-03 20:33:3316 fn iterate(&self, blk: &fn(x: &A) -> bool) -> bool;
Michael Sullivan038f9252012-07-06 22:50:5017}
18
Patrick Walton8b56a832013-03-25 20:21:0419impl<'self,A> iterable<A> for &'self [A] {
Alex Crichtonb05aae22013-05-03 20:33:3320 fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
Daniel Micaye67c48a2013-06-24 22:34:2021 self.iter().advance(f)
Michael Sullivan038f9252012-07-06 22:50:5022 }
23}
24
Patrick Walton91436882013-02-14 19:47:0025impl<A> iterable<A> for ~[A] {
Alex Crichtonb05aae22013-05-03 20:33:3326 fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
Daniel Micaye67c48a2013-06-24 22:34:2027 self.iter().advance(f)
Michael Sullivan038f9252012-07-06 22:50:5028 }
29}
30
31fn length<A, T: iterable<A>>(x: T) -> uint {
32 let mut len = 0;
Daniel Micayb3ad6852013-08-02 06:17:2033 do x.iterate() |_y| { len += 1; true };
Brian Andersonb3559362012-08-02 00:30:0534 return len;
Michael Sullivan038f9252012-07-06 22:50:5035}
36
Graydon Hoare89c8ef72013-02-02 03:43:1737pub fn main() {
Michael Sullivan038f9252012-07-06 22:50:5038 let x = ~[0,1,2,3];
39 // Call a method
Daniel Micayb3ad6852013-08-02 06:17:2040 do x.iterate() |y| { assert!(x[*y] == *y); true };
Michael Sullivan038f9252012-07-06 22:50:5041 // Call a parameterized function
Corey Richardsoncc57ca02013-05-19 02:02:4542 assert_eq!(length(x.clone()), x.len());
Michael Sullivan038f9252012-07-06 22:50:5043 // Call a parameterized function, with type arguments that require
44 // a borrow
Corey Richardsoncc57ca02013-05-19 02:02:4545 assert_eq!(length::<int, &[int]>(x), x.len());
Michael Sullivan038f9252012-07-06 22:50:5046
47 // Now try it with a type that *needs* to be borrowed
Ben Striegelac81fff2012-10-10 04:28:0448 let z = [0,1,2,3];
Michael Sullivan038f9252012-07-06 22:50:5049 // Call a method
Daniel Micayb3ad6852013-08-02 06:17:2050 do z.iterate() |y| { assert!(z[*y] == *y); true };
Michael Sullivan038f9252012-07-06 22:50:5051 // Call a parameterized function
Corey Richardsoncc57ca02013-05-19 02:02:4552 assert_eq!(length::<int, &[int]>(z), z.len());
Michael Sullivan038f9252012-07-06 22:50:5053}