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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! Provides a generic Map iterator that is similar but easier to create type signatures for than [std::iter::Map].

use std::iter::FusedIterator;

/// Function is a generic trait for a Fn(T)->O
pub trait Function<T> {
    type Output;
    fn call(&self, x: T) -> Self::Output;
}

impl<F, T, O> Function<T> for F
where
    F: Fn(T) -> O,
{
    type Output = O;

    #[inline]
    fn call(&self, x: T) -> Self::Output {
        self(x)
    }
}

#[derive(Clone, Copy)]
/// Works a lot like [Map](std::iter::Map)
/// but accepts a non Fn(T)->O type,
/// Instead, opting to use the trait [Function]
/// for the mapping function. This makes creating types
/// using map a lot easier
pub struct Map<I, F> {
    i: I,
    f: F,
}

impl<I, F> Map<I, F>
where
    I: Iterator,
{
    pub fn new(i: impl IntoIterator<Item = I::Item, IntoIter = I>, f: F) -> Self {
        Map {
            i: i.into_iter(),
            f,
        }
    }
}

impl<I, F> Iterator for Map<I, F>
where
    I: Iterator,
    F: Function<I::Item>,
{
    type Item = F::Output;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        Some(self.f.call(self.i.next()?))
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.i.size_hint()
    }

    #[inline]
    fn count(self) -> usize
    where
        Self: Sized,
    {
        self.i.count()
    }

    #[inline]
    fn last(self) -> Option<Self::Item>
    where
        Self: Sized,
    {
        Some(self.f.call(self.i.last()?))
    }
}

impl<I, F> DoubleEndedIterator for Map<I, F>
where
    I: DoubleEndedIterator,
    F: Function<I::Item>,
{
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        Some(self.f.call(self.i.next_back()?))
    }
}

impl<I, F> ExactSizeIterator for Map<I, F>
where
    I: ExactSizeIterator,
    F: Function<I::Item>,
{
    fn len(&self) -> usize {
        self.i.len()
    }
}

impl<I, F> FusedIterator for Map<I, F>
where
    I: FusedIterator,
    F: Function<I::Item>,
{
}

#[cfg(test)]
mod tests {
    use std::assert_eq;

    use super::*;

    #[test]
    fn test_iter() {
        let c: Vec<_> = Map::new(0..5, |x| 2 * x).collect();
        assert_eq!(c, vec![0, 2, 4, 6, 8]);
    }

    #[test]
    fn test_count() {
        let it = Map::new(0..5, |x| 2 * x);
        assert_eq!(it.count(), 5);
    }

    #[test]
    fn test_last() {
        let it = Map::new(0..5, |x| 2 * x);
        assert_eq!(it.last(), Some(8));
    }

    #[test]
    fn test_reverse() {
        let it = Map::new(0..5, |x| 2 * x);
        assert!(it.rev().eq(vec![8, 6, 4, 2, 0]));
    }

    #[test]
    fn test_len() {
        let it = Map::new(0..5, |x| 2 * x);
        assert_eq!(it.len(), 5);
    }
}