3 #![feature(generic_associated_types)]
7 trait StreamingIterator {
9 // Applying the lifetime parameter `'a` to `Self::Item` inside the trait.
10 fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;
13 struct Foo<T: StreamingIterator> {
14 // Applying a concrete lifetime to the constructor outside the trait.
15 bar: <T as StreamingIterator>::Item<'static>,
18 // Users can bound parameters by the type constructed by that trait's associated type constructor
19 // of a trait using HRTB. Both type equality bounds and trait bounds of this kind are valid:
20 //FIXME(#44265): This next line should parse and be valid
21 //fn foo<T: for<'a> StreamingIterator<Item<'a>=&'a [i32]>>(_iter: T) { /* ... */ }
22 fn _foo<T>(_iter: T) where T: StreamingIterator, for<'a> T::Item<'a>: Display { /* ... */ }
24 // Full example of enumerate iterator
26 #[must_use = "iterators are lazy and do nothing unless consumed"]
27 struct StreamEnumerate<I> {
32 impl<I: StreamingIterator> StreamingIterator for StreamEnumerate<I> {
33 type Item<'a> = (usize, I::Item<'a>);
34 fn next<'a>(&'a mut self) -> Option<Self::Item<'a>> {
35 match self.iter.next() {
38 let r = Some((self.count, val));
46 impl<I: Iterator> StreamingIterator for I {
47 type Item<'a> = <I as Iterator>::Item;
48 fn next(&mut self) -> Option<<I as StreamingIterator>::Item<'_>> {
53 impl<I> StreamEnumerate<I> {
54 pub fn new(iter: I) -> Self {
62 fn test_stream_enumerate() {
63 let v = vec!["a", "b", "c"];
64 let mut se = StreamEnumerate::new(v.iter());
65 while let Some(item) = se.next() {
66 assert_eq!(v[item.0], *item.1);
68 let x = Foo::<std::slice::Iter<'static, u32>> {
71 assert_eq!(*x.bar, 0u32);
75 test_stream_enumerate();