1 #![allow(incomplete_features)]
2 #![feature(generic_associated_types)]
3 #![feature(associated_type_defaults)]
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/
9 // check that we don't normalize with trait defaults.
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>;
20 fn add(&mut self, value: T);
22 fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
25 trait CollectionFamily {
26 type Member<T>: Collection<T, Family = Self>;
31 impl CollectionFamily for VecFamily {
32 type Member<T> = Vec<T>;
35 impl<T> Collection<T> for Vec<T> {
36 type Iter<'iter> where T: 'iter = std::slice::Iter<'iter, T>;
37 type Family = VecFamily;
43 fn add(&mut self, value: T) {
47 fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
52 fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
56 let mut res = <C::Family as CollectionFamily>::Member::<f32>::empty();
57 for &v in ints.iterate() {
61 //~^ ERROR mismatched types
65 let a = vec![1i32, 2, 3];
66 let c = floatify_sibling(&a);
67 assert_eq!(Some(&1.0), c.iterate().next());