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