1 #![feature(associated_type_defaults)]
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/
7 // check that we don't normalize with trait defaults.
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>;
18 fn add(&mut self, value: T);
20 fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
23 trait CollectionFamily {
24 type Member<T>: Collection<T, Family = Self>;
29 impl CollectionFamily for VecFamily {
30 type Member<T> = Vec<T>;
33 impl<T> Collection<T> for Vec<T> {
34 type Iter<'iter> = std::slice::Iter<'iter, T> where T: 'iter;
35 type Family = VecFamily;
41 fn add(&mut self, value: T) {
45 fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
50 fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
54 let mut res = <C::Family as CollectionFamily>::Member::<f32>::empty();
55 for &v in ints.iterate() {
59 //~^ ERROR mismatched types
63 let a = vec![1i32, 2, 3];
64 let c = floatify_sibling(&a);
65 assert_eq!(Some(&1.0), c.iterate().next());