pixelflut_rs/
grid.rs

1use crate::pixel::{Coordinate, Pixel};
2
3/// The size of a Grid, defined by x and y.
4///
5/// The size of the Grid is defined so that the following can actually be drawn on the grid:
6/// ```compile_fail
7/// # use pixelflut_rs::grid::{Grid, Size};
8/// # use pixelflut_rs::pixel::Pixel;
9/// # let grid: Grid;
10/// assert_eq!(grid.size(), Size::new(1024, 768));
11/// let pixel: Pixel = "PX 1023 767 ff0f00".parse()?;
12/// grid.draw(pixel);
13/// ```
14/// The following is not working because it is out of bounds:
15/// ```compile_fail
16/// # use pixelflut_rs::grid::{Grid, Size};
17/// # use pixelflut_rs::pixel::Pixel;
18/// # let grid: Grid;
19/// assert_eq!(grid.size(), Size::new(1024, 768));
20/// let pixel: Pixel = "PX 1024 768 ff0f00".parse()?;
21/// grid.draw(pixel);
22/// ```
23#[derive(Copy, Clone, PartialEq, Hash, Debug)]
24pub struct Size {
25    x: usize,
26    y: usize,
27}
28
29impl Size {
30    /// Creates a new Size for the given x and y values.
31    pub fn new(x: usize, y: usize) -> Size {
32        Size { x, y }
33    }
34
35    /// Returns the x value.
36    pub fn x(&self) -> usize {
37        self.x
38    }
39
40    /// Returns the y value.
41    pub fn y(&self) -> usize {
42        self.y
43    }
44}
45
46/// The Grid which can be implemented by your Project to attach the Pixelflut interface to it.
47pub trait Grid {
48    /// Returns the Size of this Grid.
49    ///
50    /// The size of the Grid is defined so that the following can actually be drawn on the grid:
51    /// ```compile_fail
52    /// # use pixelflut_rs::grid::{Grid, Size};
53    /// # use pixelflut_rs::pixel::Pixel;
54    /// # let grid: Grid;
55    /// assert_eq!(grid.size(), Size::new(1024, 768));
56    /// let pixel: Pixel = "PX 1023 767 ff0f00".parse()?;
57    /// grid.draw(pixel);
58    /// ```
59    /// The following is not working because it is out of bounds:
60    /// ```compile_fail
61    /// # use pixelflut_rs::grid::{Grid, Size};
62    /// # use pixelflut_rs::pixel::Pixel;
63    /// # let grid: Grid;
64    /// assert_eq!(grid.size(), Size::new(1024, 768));
65    /// let pixel: Pixel = "PX 1024 768 ff0f00".parse()?;
66    /// grid.draw(pixel);
67    /// ```
68    fn size(&self) -> Size;
69
70    /// Draw the given Pixel on the Grid.
71    fn draw(&mut self, px: &Pixel);
72
73    /// Fetch the current status of the Pixel for the given Coordinates. Returns None if no such
74    /// Pixel exists.
75    fn fetch(&self, p: Coordinate) -> Option<Pixel>;
76}