]> git.lizzy.rs Git - rust.git/blob - src/test/ui/generic-associated-types/issue-76826.rs
Auto merge of #99612 - yanchen4791:issue-95079-fix, r=compiler-errors
[rust.git] / src / test / ui / generic-associated-types / issue-76826.rs
1 // run-pass
2
3 #![feature(generic_associated_types)]
4
5 pub trait Iter {
6     type Item<'a> where Self: 'a;
7
8     fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;
9
10     fn for_each<F>(mut self, mut f: F)
11         where Self: Sized, F: for<'a> FnMut(Self::Item<'a>)
12     {
13         while let Some(item) = self.next() {
14             f(item);
15         }
16     }
17 }
18
19 pub struct Windows<T> {
20     items: Vec<T>,
21     start: usize,
22     len: usize,
23 }
24
25 impl<T> Windows<T> {
26     pub fn new(items: Vec<T>, len: usize) -> Self {
27         Self { items, start: 0, len }
28     }
29 }
30
31 impl<T> Iter for Windows<T> {
32     type Item<'a> = &'a mut [T] where T: 'a;
33
34     fn next<'a>(&'a mut self) -> Option<Self::Item<'a>> {
35         let slice = self.items.get_mut(self.start..self.start + self.len)?;
36         self.start += 1;
37         Some(slice)
38     }
39 }
40
41 fn main() {
42     Windows::new(vec![1, 2, 3, 4, 5], 3)
43         .for_each(|slice| println!("{:?}", slice));
44 }