]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc1598-generic-associated-types/streaming_iterator.rs
Rollup merge of #53413 - eddyb:featured-in-the-latest-edition, r=varkor
[rust.git] / src / test / ui / rfc1598-generic-associated-types / streaming_iterator.rs
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 //~^ WARNING the feature `generic_associated_types` is incomplete
13
14 // FIXME(#44265): "lifetime parameter not allowed on this type" errors will be addressed in a
15 // follow-up PR
16
17 use std::fmt::Display;
18
19 trait StreamingIterator {
20     type Item<'a>;
21     // Applying the lifetime parameter `'a` to `Self::Item` inside the trait.
22     fn next<'a>(&'a self) -> Option<Self::Item<'a>>;
23     //~^ ERROR lifetime parameters are not allowed on this type [E0110]
24 }
25
26 struct Foo<T: StreamingIterator> {
27     // Applying a concrete lifetime to the constructor outside the trait.
28     bar: <T as StreamingIterator>::Item<'static>,
29     //~^ ERROR lifetime parameters are not allowed on this type [E0110]
30 }
31
32 // Users can bound parameters by the type constructed by that trait's associated type constructor
33 // of a trait using HRTB. Both type equality bounds and trait bounds of this kind are valid:
34 //FIXME(sunjay): This next line should parse and be valid
35 //fn foo<T: for<'a> StreamingIterator<Item<'a>=&'a [i32]>>(iter: T) { /* ... */ }
36 fn foo<T>(iter: T) where T: StreamingIterator, for<'a> T::Item<'a>: Display { /* ... */ }
37 //~^ ERROR lifetime parameters are not allowed on this type [E0110]
38
39 // Full example of enumerate iterator
40
41 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
42 struct StreamEnumerate<I> {
43     iter: I,
44     count: usize,
45 }
46
47 impl<I: StreamingIterator> StreamingIterator for StreamEnumerate<I> {
48     type Item<'a> = (usize, I::Item<'a>);
49     //~^ ERROR lifetime parameters are not allowed on this type [E0110]
50     fn next<'a>(&'a self) -> Option<Self::Item<'a>> {
51         //~^ ERROR lifetime parameters are not allowed on this type [E0110]
52         match self.iter.next() {
53             None => None,
54             Some(val) => {
55                 let r = Some((self.count, val));
56                 self.count += 1;
57                 r
58             }
59         }
60     }
61 }
62
63 impl<I> StreamEnumerate<I> {
64     pub fn new(iter: I) -> Self {
65         StreamEnumerate {
66             count: 0,
67             iter: iter,
68         }
69     }
70 }
71
72 fn test_stream_enumerate() {
73     let v = vec!["a", "b", "c"];
74     let se = StreamEnumerate::new(v.iter());
75     let a: &str = se.next().unwrap().1;
76     for (i, s) in se {
77         println!("{} {}", i, s);
78     }
79     println!("{}", a);
80 }
81
82
83 fn main() {}