1 // run-pass
2
3 #![feature(generic_associated_types)]
4
5 pub trait Iter {
6 type Item<'a> where Self: 'a;
7
next<'a>(&'a mut self) -> Option<Self::Item<'a>>8 fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;
9
for_each<F>(mut self, mut f: F) where Self: Sized, F: for<'a> FnMut(Self::Item<'a>)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> {
new(items: Vec<T>, len: usize) -> Self26 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> where T: 'a = &'a mut [T];
33
next<'a>(&'a mut self) -> Option<Self::Item<'a>>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
main()41 fn main() {
42 Windows::new(vec![1, 2, 3, 4, 5], 3)
43 .for_each(|slice| println!("{:?}", slice));
44 }
45