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
use std::iter::Peekable;

pub struct MarkLast<I>
where
    I: Iterator,
{
    iter: Peekable<I>,
}

impl<I> MarkLast<I>
where
    I: Iterator,
{
    fn new(iter: I) -> MarkLast<I> {
        MarkLast {
            iter: iter.peekable(),
        }
    }
}

impl<I> Iterator for MarkLast<I>
where
    I: Iterator,
{
    type Item = (bool, I::Item);

    fn next(&mut self) -> Option<(bool, I::Item)> {
        let val = self.iter.next()?;
        let last = self.iter.peek().is_none();
        Some((last, val))
    }
}

pub trait MarkLastIterator<I>
where
    I: Iterator,
{
    /// Creates an iterator which gives the next value as well as a boolean indicating if this is
    /// the last value of the iterator.
    ///
    /// The iterator returned yields pairs `(b, val)`, where `b` is true if this is the last value
    /// and `val` is the value returned by the iterator.
    ///
    /// # Examples
    ///
    /// ```
    /// # use crate::mark_last::MarkLastIterator;
    /// let in_data = vec![1, 2, 3, 5, 99];
    /// let out_data: Vec<_> = in_data.into_iter().mark_last().collect();
    /// assert_eq!(
    ///     out_data,
    ///     vec![(false, 1), (false, 2), (false, 3), (false, 5), (true, 99)]
    /// )
    /// ```
    fn mark_last(self) -> MarkLast<I>;
}

impl<I> MarkLastIterator<I> for I
where
    I: Iterator,
{
    fn mark_last(self) -> MarkLast<Self>
    where
        Self: Sized,
    {
        MarkLast::new(self)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn marks_last() {
        let in_data = vec![1, 2, 3, 5, 99];
        let out_data: Vec<_> = in_data.into_iter().mark_last().collect();
        assert_eq!(
            out_data,
            vec![(false, 1), (false, 2), (false, 3), (false, 5), (true, 99)]
        )
    }

    #[test]
    fn marks_last_empty() {
        let in_data: [i32; 0] = [];
        let out_data: Vec<_> = in_data.iter().mark_last().collect();
        assert_eq!(out_data, vec![])
    }

    #[test]
    fn marks_last_length_one() {
        let in_data = vec![3];
        let out_data: Vec<_> = in_data.into_iter().mark_last().collect();
        assert_eq!(out_data, vec![(true, 3)])
    }

    #[test]
    fn marks_nothing_infinite() {
        let in_data = 0..;
        assert!(in_data.mark_last().take(1_000_000).all(|(last, _)| !last))
    }
}