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
//! Pixel iterator.

use crate::{geometry::Point, pixelcolor::PixelColor, Pixel};

/// Translated pixel iterator.
#[derive(Debug, PartialEq)]
pub struct Translated<I> {
    iter: I,
    offset: Point,
}

impl<I, C> Translated<I>
where
    I: Iterator<Item = Pixel<C>>,
    C: PixelColor,
{
    pub(super) fn new(iter: I, offset: Point) -> Self {
        Self { iter, offset }
    }
}

impl<I, C> Iterator for Translated<I>
where
    I: Iterator<Item = Pixel<C>>,
    C: PixelColor,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter
            .next()
            .map(|Pixel(p, c)| Pixel(p + self.offset, c))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{iterator::PixelIteratorExt, pixelcolor::BinaryColor};

    #[test]
    fn translate() {
        let pixels = [
            Pixel(Point::new(1, 2), BinaryColor::On),
            Pixel(Point::new(3, 4), BinaryColor::On),
            Pixel(Point::new(5, 6), BinaryColor::On),
        ];
        let pixels = pixels.iter().copied();

        let expected = [
            Pixel(Point::new(1 + 4, 2 + 5), BinaryColor::On),
            Pixel(Point::new(3 + 4, 4 + 5), BinaryColor::On),
            Pixel(Point::new(5 + 4, 6 + 5), BinaryColor::On),
        ];
        let expected = expected.iter().copied();

        assert!(pixels.translated(Point::new(4, 5)).eq(expected));
    }
}