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