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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
use crate::{Bounded, CursedExt, Cursor, Sequence};

/// A [`Sequence`] element `Iterator` that returns a reference to the
/// element and a [`Cursor`] that points to it.
///
/// [`Cursor`]: ../struct.Cursor.html
/// [`Sequence`]: ../trait.Sequence.html
#[derive(Debug)]
pub struct Iter<'a, C, T> {
    cursor: Option<Cursor<T>>,
    cursed: &'a C,
}

impl<'a, C, T> Iter<'a, C, T> {
    /// Construct a new `Iter`.
    pub fn new(cursed: &'a C, cursor: Cursor<T>) -> Self {
        let cursor = Some(cursor);
        Self { cursed, cursor }
    }
}

impl<'a, C, T: 'a> Iterator for Iter<'a, C, T>
where
    C: Sequence<T>,
{
    type Item = (&'a T, Cursor<T>);

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self.cursor {
            Some(cursor) => self.cursed.remaining(cursor),
            None => (0, Some(0)),
        }
    }

    fn next(&mut self) -> Option<Self::Item> {
        self.cursor.map(|curr_cursor| {
            self.cursor = self.cursed.next(curr_cursor);
            let elem = self.cursed.get(curr_cursor);
            (elem, curr_cursor)
        })
    }
}

impl<'a, C, T: 'a> DoubleEndedIterator for Iter<'a, C, T>
where
    C: Sequence<T>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        self.cursor.map(|curr_cursor| {
            self.cursor = self.cursed.prev(curr_cursor);
            let elem = self.cursed.get(curr_cursor);
            (elem, curr_cursor)
        })
    }
}

///////////////////////////////////////////////////////////////////////////////
// WrappingIter

/// A cyclic [`Bounded`] element `Iterator` that returns a reference to the
/// element and a [`Cursor`] that points to it.
///
/// [`Cursor`]: ../struct.Cursor.html
/// [`Bounded`]: ../trait.Bounded.html
#[derive(Debug)]
pub struct WrappingIter<'a, C, T> {
    cursor: Cursor<T>,
    cursed: &'a C,
}

impl<'a, C, T> WrappingIter<'a, C, T> {
    /// Construct a new `WrappingIter`.
    pub fn new(cursed: &'a C, cursor: Cursor<T>) -> Self {
        Self { cursed, cursor }
    }
}

impl<'a, C, T: 'a> Iterator for WrappingIter<'a, C, T>
where
    C: Bounded<T>,
{
    type Item = (&'a T, Cursor<T>);

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, None)
    }

    fn next(&mut self) -> Option<Self::Item> {
        let curr_cursor = self.cursor;
        self.cursor = self.cursed.wrapping_next(curr_cursor);
        let elem = self.cursed.get(curr_cursor);
        Some((elem, curr_cursor))
    }
}

impl<'a, C, T: 'a> DoubleEndedIterator for WrappingIter<'a, C, T>
where
    C: Bounded<T>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        let curr_cursor = self.cursor;
        self.cursor = self.cursed.wrapping_prev(curr_cursor);
        let elem = self.cursed.get(curr_cursor);
        Some((elem, curr_cursor))
    }
}