]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc1598-generic-associated-types/collections.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / test / ui / rfc1598-generic-associated-types / collections.rs
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
11 #![feature(generic_associated_types)]
12 //~^ WARNING the feature `generic_associated_types` is incomplete
13 #![feature(associated_type_defaults)]
14
15 // FIXME(#44265): "lifetime parameters are not allowed on this type" errors will be addressed in a
16 // follow-up PR.
17
18 // A Collection trait and collection families. Based on
19 // http://smallcultfollowing.com/babysteps/blog/2016/11/03/
20 // associated-type-constructors-part-2-family-traits/
21
22 trait Collection<T> {
23     type Iter<'iter>: Iterator<Item=&'iter T>;
24     type Family: CollectionFamily;
25     // Test associated type defaults with parameters
26     type Sibling<U>: Collection<U> =
27         <<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
28     //~^ ERROR type parameters are not allowed on this type [E0109]
29
30     fn empty() -> Self;
31
32     fn add(&mut self, value: T);
33
34     fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
35     //~^ ERROR lifetime parameters are not allowed on this type [E0110]
36 }
37
38 trait CollectionFamily {
39     type Member<T>: Collection<T, Family = Self>;
40 }
41
42 struct VecFamily;
43
44 impl CollectionFamily for VecFamily {
45     type Member<T> = Vec<T>;
46 }
47
48 impl<T> Collection<T> for Vec<T> {
49     type Iter<'iter> = std::slice::Iter<'iter, T>;
50     type Family = VecFamily;
51
52     fn empty() -> Self {
53         Vec::new()
54     }
55
56     fn add(&mut self, value: T) {
57         self.push(value)
58     }
59
60     fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
61     //~^ ERROR lifetime parameters are not allowed on this type [E0110]
62         self.iter()
63     }
64 }
65
66 fn floatify<C>(ints: &C) -> <<C as Collection<i32>>::Family as CollectionFamily>::Member<f32>
67 //~^ ERROR type parameters are not allowed on this type [E0109]
68 where
69     C: Collection<i32>,
70 {
71     let mut res = C::Family::Member::<f32>::empty();
72     for &v in ints.iterate() {
73         res.add(v as f32);
74     }
75     res
76 }
77
78 fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
79 //~^ ERROR type parameters are not allowed on this type [E0109]
80 where
81     C: Collection<i32>,
82 {
83     let mut res = C::Family::Member::<f32>::empty();
84     for &v in ints.iterate() {
85         res.add(v as f32);
86     }
87     res
88 }
89
90 fn use_floatify() {
91     let a = vec![1i32, 2, 3];
92     let b = floatify(a);
93     println!("{}", b.iterate().next());
94     let c = floatify_sibling(a);
95     println!("{}", c.iterate().next());
96 }
97
98 fn main() {}