]> git.lizzy.rs Git - rust.git/blob - tests/ui/generic-associated-types/collections.rs
Rollup merge of #103702 - WaffleLapkin:lift-sized-bounds-from-pointer-methods-where...
[rust.git] / tests / ui / generic-associated-types / collections.rs
1 #![feature(associated_type_defaults)]
2
3 // A Collection trait and collection families. Based on
4 // https://smallcultfollowing.com/babysteps/blog/2016/11/03/
5 // associated-type-constructors-part-2-family-traits/
6
7 // run-pass
8
9 trait Collection<T> {
10     type Iter<'iter>: Iterator<Item=&'iter T> where T: 'iter, Self: 'iter;
11     type Family: CollectionFamily;
12     // Test associated type defaults with parameters
13     type Sibling<U>: Collection<U> =
14         <<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
15
16     fn empty() -> Self;
17
18     fn add(&mut self, value: T);
19
20     fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
21 }
22
23 trait CollectionFamily {
24     type Member<T>: Collection<T, Family = Self>;
25 }
26
27 struct VecFamily;
28
29 impl CollectionFamily for VecFamily {
30     type Member<T> = Vec<T>;
31 }
32
33 impl<T> Collection<T> for Vec<T> {
34     type Iter<'iter> = std::slice::Iter<'iter, T> where T: 'iter;
35     type Family = VecFamily;
36
37     fn empty() -> Self {
38         Vec::new()
39     }
40
41     fn add(&mut self, value: T) {
42         self.push(value)
43     }
44
45     fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
46         self.iter()
47     }
48 }
49
50 fn floatify<C>(ints: &C) -> <<C as Collection<i32>>::Family as CollectionFamily>::Member<f32>
51 where
52     C: Collection<i32>,
53 {
54     let mut res = <C::Family as CollectionFamily>::Member::<f32>::empty();
55     for &v in ints.iterate() {
56         res.add(v as f32);
57     }
58     res
59 }
60
61 fn use_floatify() {
62     let a = vec![1, 2, 3];
63     let b = floatify(&a);
64     assert_eq!(Some(&1.0), b.iterate().next());
65 }
66
67 fn main() {
68     use_floatify();
69 }