1 | //! Iterators. |
2 | |
3 | pub mod contiguous; |
4 | pub mod pixel; |
5 | pub mod raw; |
6 | |
7 | use crate::{ |
8 | draw_target::DrawTarget, geometry::Point, pixelcolor::PixelColor, primitives::Rectangle, Pixel, |
9 | }; |
10 | |
11 | /// Extension trait for contiguous iterators. |
12 | pub trait ContiguousIteratorExt |
13 | where |
14 | Self: Iterator + Sized, |
15 | <Self as Iterator>::Item: PixelColor, |
16 | { |
17 | /// Converts a contiguous iterator into a pixel iterator. |
18 | fn into_pixels(self, bounding_box: &Rectangle) -> contiguous::IntoPixels<Self>; |
19 | } |
20 | |
21 | impl<I> ContiguousIteratorExt for I |
22 | where |
23 | I: Iterator, |
24 | I::Item: PixelColor, |
25 | { |
26 | fn into_pixels(self, bounding_box: &Rectangle) -> contiguous::IntoPixels<Self> { |
27 | contiguous::IntoPixels::new(self, *bounding_box) |
28 | } |
29 | } |
30 | |
31 | /// Extension trait for pixel iterators. |
32 | pub trait PixelIteratorExt<C> |
33 | where |
34 | Self: Sized, |
35 | C: PixelColor, |
36 | { |
37 | /// Draws the pixel iterator to a draw target. |
38 | fn draw<D>(self, target: &mut D) -> Result<(), D::Error> |
39 | where |
40 | D: DrawTarget<Color = C>; |
41 | |
42 | /// Returns a translated version of the iterator. |
43 | fn translated(self, offset: Point) -> pixel::Translated<Self>; |
44 | } |
45 | |
46 | impl<I, C> PixelIteratorExt<C> for I |
47 | where |
48 | C: PixelColor, |
49 | I: Iterator<Item = Pixel<C>>, |
50 | { |
51 | fn draw<D>(self, target: &mut D) -> Result<(), D::Error> |
52 | where |
53 | D: DrawTarget<Color = C>, |
54 | { |
55 | target.draw_iter(self) |
56 | } |
57 | |
58 | fn translated(self, offset: Point) -> pixel::Translated<Self> { |
59 | pixel::Translated::new(self, offset) |
60 | } |
61 | } |
62 | |
63 | #[cfg (test)] |
64 | mod tests { |
65 | use crate::{ |
66 | geometry::Point, iterator::PixelIteratorExt, mock_display::MockDisplay, |
67 | pixelcolor::BinaryColor, Pixel, |
68 | }; |
69 | |
70 | #[test ] |
71 | fn draw_pixel_iterator() { |
72 | let pixels = [ |
73 | Pixel(Point::new(0, 0), BinaryColor::On), |
74 | Pixel(Point::new(1, 0), BinaryColor::Off), |
75 | Pixel(Point::new(2, 0), BinaryColor::On), |
76 | Pixel(Point::new(2, 1), BinaryColor::Off), |
77 | ]; |
78 | |
79 | let mut display = MockDisplay::new(); |
80 | pixels.iter().copied().draw(&mut display).unwrap(); |
81 | |
82 | display.assert_pattern(&[ |
83 | "#.#" , // |
84 | " ." , // |
85 | ]); |
86 | } |
87 | } |
88 | |