summaryrefslogtreecommitdiffstats
path: root/src/test/ui/generic-associated-types/issue-76826.rs
blob: 28eb3b0e750e2dbc866db4f866864fdf12fb860b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// run-pass

#![feature(generic_associated_types)]

pub trait Iter {
    type Item<'a> where Self: 'a;

    fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;

    fn for_each<F>(mut self, mut f: F)
        where Self: Sized, F: for<'a> FnMut(Self::Item<'a>)
    {
        while let Some(item) = self.next() {
            f(item);
        }
    }
}

pub struct Windows<T> {
    items: Vec<T>,
    start: usize,
    len: usize,
}

impl<T> Windows<T> {
    pub fn new(items: Vec<T>, len: usize) -> Self {
        Self { items, start: 0, len }
    }
}

impl<T> Iter for Windows<T> {
    type Item<'a> = &'a mut [T] where T: 'a;

    fn next<'a>(&'a mut self) -> Option<Self::Item<'a>> {
        let slice = self.items.get_mut(self.start..self.start + self.len)?;
        self.start += 1;
        Some(slice)
    }
}

fn main() {
    Windows::new(vec![1, 2, 3, 4, 5], 3)
        .for_each(|slice| println!("{:?}", slice));
}