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
//! Grid operations and utilities for working with 2D grids.
//!
//! Implementing these traits allows for generic operations on grids.
//!
//! Some of these traits are automatically imported by the [`prelude`](crate::prelude).
//!
//! ## Examples
//!
//! Using [`GridRead`] to read from a grid:
//!
//! ```rust
//! use grixy::{core::Pos, buf::GridBuf, ops::GridRead};
//!
//! let grid = GridBuf::new_filled(10, 10, 42);
//! assert_eq!(grid.get(Pos::new(5, 5)), Some(&42));
//! ```
//!
//! Implementing [`GridWrite`] to write to a grid:
//!
//! ```rust
//! use grixy::prelude::*;
//!
//! struct MyGrid {
//! grid: Vec<u8>,
//! width: usize,
//! }
//!
//! impl GridBase for MyGrid {
//! fn size_hint(&self) -> (Size, Option<Size>) {
//! let size = Size::new(self.width, self.grid.len() / self.width);
//! (size, Some(size))
//! }
//! }
//!
//! impl GridWrite for MyGrid {
//! type Element = u8;
//! type Layout = RowMajor;
//!
//! fn set(&mut self, pos: Pos, value: Self::Element) -> Result<(), GridError> {
//! if pos.x >= self.width || pos.y >= self.grid.len() / self.width {
//! return Err(GridError::OutOfBounds { pos });
//! }
//! let index = pos.y * self.width + pos.x;
//! self.grid[index] = value;
//! Ok(())
//! }
//! }
//!
//! let mut my_grid = MyGrid {
//! grid: vec![0; 100],
//! width: 10,
//! };
//!
//! my_grid.set(Pos::new(5, 5), 42);
//! assert_eq!(my_grid.grid[55], 42);
//! ```
pub use ;
pub use copy_rect;
pub use ;
pub use GridWrite;