]> git.lizzy.rs Git - rust.git/blob - src/test/ui/traits/assignability-trait.rs
Auto merge of #87284 - Aaron1011:remove-paren-special, r=petrochenkov
[rust.git] / src / test / ui / traits / assignability-trait.rs
1 // run-pass
2 #![allow(non_camel_case_types)]
3
4 // Tests that type assignability is used to search for instances when
5 // making method calls, but only if there aren't any matches without
6 // it.
7
8 trait iterable<A> {
9     fn iterate<F>(&self, blk: F) -> bool where F: FnMut(&A) -> bool;
10 }
11
12 impl<'a,A> iterable<A> for &'a [A] {
13     fn iterate<F>(&self, f: F) -> bool where F: FnMut(&A) -> bool {
14         self.iter().all(f)
15     }
16 }
17
18 impl<A> iterable<A> for Vec<A> {
19     fn iterate<F>(&self, f: F) -> bool where F: FnMut(&A) -> bool {
20         self.iter().all(f)
21     }
22 }
23
24 fn length<A, T: iterable<A>>(x: T) -> usize {
25     let mut len = 0;
26     x.iterate(|_y| {
27         len += 1;
28         true
29     });
30     return len;
31 }
32
33 pub fn main() {
34     let x: Vec<isize> = vec![0,1,2,3];
35     // Call a method
36     x.iterate(|y| { assert_eq!(x[*y as usize], *y); true });
37     // Call a parameterized function
38     assert_eq!(length(x.clone()), x.len());
39     // Call a parameterized function, with type arguments that require
40     // a borrow
41     assert_eq!(length::<isize, &[isize]>(&*x), x.len());
42
43     // Now try it with a type that *needs* to be borrowed
44     let z = [0,1,2,3];
45     // Call a parameterized function
46     assert_eq!(length::<isize, &[isize]>(&z), z.len());
47 }