pub struct LookAhead<const C: usize, T, U>
where
    U: Iterator<Item=T> + Sized
{
    iter: U,
    data: [Option<T>;C],
    cur: usize,
    fill: usize,
}

impl<const C: usize, T, U> LookAhead<C,T,U>
where
    U: Iterator<Item=T> + Sized
{
    pub fn new(iter: U) -> Self {
        Self {
            iter,
            data: [const {None};C],
            cur: 0,
            fill: 0,
        }
    }

    pub fn peek(&mut self, offset: usize) -> Option<&T> {
        assert!(offset<C);
        self.fill_to( offset );
        self.data[(self.cur+offset)%C].as_ref()
    }

    fn fill_to(&mut self, qty: usize) {
        for i in self.fill..=qty {
            if let Some(x) = self.iter.next() {
                self.data[(self.cur+i)%C].replace( x );
                self.fill += 1;
            }
        }
    }
}

impl<const C: usize, T, U> std::iter::Iterator for LookAhead<C,T,U> 
where
    U: Iterator<Item=T> + Sized
{
    type Item=T;

    fn next(&mut self) -> Option<T> {
        if self.fill == 0 {
            self.iter.next()
        } else {
            let old = self.cur;
            self.cur = (self.cur+1)%C;
            self.fill -= 1;
            self.data[old].take()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic() {
        let data = [1, 2, 3, 4, 5, 6, 7, 8];
        let mut i = LookAhead::<3,_,_>::new(data.iter());
        assert_eq!(i.next(), Some(&1));
        assert_eq!(i.peek(1), Some(&&3));
        assert_eq!(i.peek(0), Some(&&2));
        assert_eq!(i.next(), Some(&2));
        assert_eq!(i.peek(0), Some(&&3));
        assert_eq!(i.next(), Some(&3));
        assert_eq!(i.next(), Some(&4));
        assert_eq!(i.next(), Some(&5));
        assert_eq!(i.peek(0), Some(&&6));
        assert_eq!(i.peek(1), Some(&&7));
        assert_eq!(i.peek(2), Some(&&8));
        assert_eq!(i.next(), Some(&6));
        assert_eq!(i.peek(2), None);
        assert_eq!(i.next(), Some(&7));
        assert_eq!(i.peek(2), None);
        assert_eq!(i.next(), Some(&8));
        assert_eq!(i.next(), None);
    }
}
