]> git.proxmox.com Git - rustc.git/blob - src/test/ui/generic-associated-types/collections-project-default.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / test / ui / generic-associated-types / collections-project-default.rs
1 #![allow(incomplete_features)]
2 #![feature(generic_associated_types)]
3 #![feature(associated_type_defaults)]
4
5 // A Collection trait and collection families. Based on
6 // http://smallcultfollowing.com/babysteps/blog/2016/11/03/
7 // associated-type-constructors-part-2-family-traits/
8
9 // check that we don't normalize with trait defaults.
10
11 trait Collection<T> {
12 type Iter<'iter>: Iterator<Item=&'iter T> where T: 'iter;
13 type Family: CollectionFamily;
14 // Test associated type defaults with parameters
15 type Sibling<U>: Collection<U> =
16 <<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
17
18 fn empty() -> Self;
19
20 fn add(&mut self, value: T);
21
22 fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
23 }
24
25 trait CollectionFamily {
26 type Member<T>: Collection<T, Family = Self>;
27 }
28
29 struct VecFamily;
30
31 impl CollectionFamily for VecFamily {
32 type Member<T> = Vec<T>;
33 }
34
35 impl<T> Collection<T> for Vec<T> {
36 type Iter<'iter> where T: 'iter = std::slice::Iter<'iter, T>;
37 type Family = VecFamily;
38
39 fn empty() -> Self {
40 Vec::new()
41 }
42
43 fn add(&mut self, value: T) {
44 self.push(value)
45 }
46
47 fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
48 self.iter()
49 }
50 }
51
52 fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
53 where
54 C: Collection<i32>,
55 {
56 let mut res = <C::Family as CollectionFamily>::Member::<f32>::empty();
57 for &v in ints.iterate() {
58 res.add(v as f32);
59 }
60 res
61 //~^ ERROR mismatched types
62 }
63
64 fn use_floatify() {
65 let a = vec![1i32, 2, 3];
66 let c = floatify_sibling(&a);
67 assert_eq!(Some(&1.0), c.iterate().next());
68 }
69
70 fn main() {
71 use_floatify();
72 }