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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::{
core::{GridError, Layout, Pos, Rect},
ops::{GridRead, convert::Blended},
};
/// Write elements to a 2-dimensional grid position.
pub trait GridWrite {
/// The type of elements in the grid.
type Element;
/// The type of layout used for the grid.
type Layout: Layout;
/// Sets the element at a specified position.
///
/// ## Errors
///
/// Returns an error if the position is out of bounds.
fn set(&mut self, pos: Pos, value: Self::Element) -> Result<(), GridError>;
/// Sets elements within a rectangular region of the grid.
///
/// Elements are set in an order agreeable to the grid's internal layout. Out-of-bounds
/// elements are skipped, and the bounding rectangle is treated as _exclusive_ of the right and
/// bottom edges.
///
/// ## Performance
///
/// The default implementation uses [`Layout::iter_pos`] to iterate over the rectangle,
/// involving bounds checking for each element. Other implementations may optimize this, for
/// example by using a more efficient iteration strategy (for linear reads, reduced bounds
/// checking, etc.).
fn fill_rect(&mut self, bounds: Rect, mut f: impl FnMut(Pos) -> Self::Element) {
Self::Layout::iter_pos(bounds).for_each(|pos| {
let _ = self.set(pos, f(pos));
});
}
/// Sets elements within a rectangular region of the grid.
///
/// Elements are set in an order agreeable to the grid's internal layout. Out-of-bounds
/// elements are skipped, and the bounding rectangle is treated as _exclusive_ of the right and
/// bottom edges.
///
/// If the provided iterator has fewer elements than the rectangle, the remaining elements will
/// not be set.
///
/// ## Performance
///
/// The default implementation uses [`Layout::iter_pos`] to iterate over the rectangle,
/// involving bounds checking for each element. Other implementations may optimize this, for
/// example by using a more efficient iteration strategy (for linear reads, reduced bounds
/// checking, etc.).
fn fill_rect_iter(&mut self, dst: Rect, iter: impl IntoIterator<Item = Self::Element>) {
Self::Layout::iter_pos(dst)
.zip(iter)
.for_each(|(pos, value)| {
let _ = self.set(pos, value);
});
}
/// Sets elements within a rectangular region of the grid.
///
/// Elements are set in an order agreeable to the grid's internal layout. Out-of-bounds
/// elements are skipped, and the bounding rectangle is treated as _exclusive_ of the right and
/// bottom edges.
///
/// ## Performance
///
/// The default implementation uses [`Layout::iter_pos`] to iterate over the rectangle,
/// involving bounds checking for each element. Other implementations may optimize this, for
/// example by using a more efficient iteration strategy (for linear reads, reduced bounds
/// checking, etc.).
fn fill_rect_solid(&mut self, dst: Rect, value: Self::Element)
where
Self::Element: Copy,
{
self.fill_rect(dst, |_| value);
}
/// Creates a blended version of this grid, applying a blend function when setting elements.
///
/// This is useful for operations like blending colors or combining values.
///
/// ## Examples
///
/// ```rust
/// use grixy::{core::Pos, ops::{GridRead, GridWrite}, buf::GridBuf};
///
/// let mut grid = GridBuf::new_filled(3, 3, 1);
/// let blend_fn = |current: &i32, new: i32| current + new;
/// let mut blended = grid.blend(blend_fn);
///
/// blended.set(Pos::new(1, 1), 5).unwrap();
/// assert_eq!(blended.get(Pos::new(1, 1)), Some(&6));
/// ```
fn blend<F>(&mut self, blend_fn: F) -> Blended<'_, Self, F>
where
Self: Sized,
Self: GridRead,
F: Fn(
<Self as GridRead>::Element<'_>,
<Self as GridWrite>::Element,
) -> <Self as GridWrite>::Element,
{
Blended {
source: self,
blend_fn,
}
}
}
#[cfg(test)]
mod tests {
extern crate alloc;
use super::*;
use crate::core::RowMajor;
use alloc::vec;
struct TestGrid {
grid: [[u8; 3]; 3],
}
impl GridWrite for TestGrid {
type Element = u8;
type Layout = RowMajor;
fn set(&mut self, pos: Pos, value: Self::Element) -> Result<(), GridError> {
if pos.x < 3 && pos.y < 3 {
self.grid[pos.y][pos.x] = value;
Ok(())
} else {
Err(GridError)
}
}
}
#[test]
fn impl_checked_set_ok() {
let mut grid = TestGrid { grid: [[0; 3]; 3] };
let pos = Pos { x: 1, y: 1 };
grid.set(pos, 42).unwrap();
assert_eq!(grid.grid[1][1], 42);
}
#[test]
fn impl_checked_set_out_of_bounds_x() {
let mut grid = TestGrid { grid: [[0; 3]; 3] };
let pos = Pos { x: 3, y: 1 };
grid.set(pos, 42).unwrap_err();
assert_eq!(grid.grid[1][1], 0);
}
#[test]
fn impl_checked_set_out_of_bounds_y() {
let mut grid = TestGrid { grid: [[0; 3]; 3] };
let pos = Pos { x: 1, y: 3 };
grid.set(pos, 42).unwrap_err();
assert_eq!(grid.grid[1][1], 0);
}
#[test]
fn impl_checked_fill_rect() {
let mut grid = TestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(0, 0, 3, 3).unwrap();
grid.fill_rect(bounds, |_| 42);
assert_eq!(grid.grid, [[42; 3]; 3]);
}
#[test]
fn impl_checked_fill_rect_iter() {
let mut grid = TestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(0, 0, 3, 3).unwrap();
grid.fill_rect_iter(bounds, vec![42; 9]);
assert_eq!(grid.grid, [[42; 3]; 3]);
}
#[test]
fn impl_checked_fill_rect_solid() {
let mut grid = TestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(0, 0, 3, 3).unwrap();
grid.fill_rect_solid(bounds, 42);
assert_eq!(grid.grid, [[42; 3]; 3]);
}
}