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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use ixy::HasSize;
use crate::{
core::{GridError, Pos, Rect},
ops::{
ExactSizeGrid, GridBase,
layout::{self, Layout as _},
},
};
/// Write elements to a 2-dimensional grid position.
pub trait GridWrite: GridBase {
/// The type of elements in the grid.
type Element;
/// The type of layout used for the grid.
///
/// ## Implementation
///
/// It is not guaranteed that the internal storage of the grid matches this layout, but methods
/// that return iterators over the grid's elements or positions should return them in the
/// traversal order defined by this layout.
///
/// [`RowMajor`][layout::RowMajor] is a reasonable default implementation for most grids.
type Layout: 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>;
/// Clears the grid, setting all elements to their default value.
///
/// Elements are set in an order agreeable to the grid's internal layout.
fn clear(&mut self)
where
Self::Element: Default,
Self: ExactSizeGrid,
{
self.clear_rect(self.size().to_rect());
}
/// Sets elements within the grid.
///
/// Elements are set in an order agreeable to the grid's internal layout.
fn fill(&mut self, f: impl FnMut(Pos) -> Self::Element)
where
Self: ExactSizeGrid,
{
self.fill_rect(self.size().to_rect(), f);
}
/// Sets elements within the grid from an iterator.
///
/// Elements are set in an order agreeable to the grid's internal layout.
fn fill_iter(&mut self, iter: impl Iterator<Item = Self::Element>)
where
Self: ExactSizeGrid,
{
self.fill_rect_iter(self.size().to_rect(), iter);
}
/// Sets elements within the grid to a single value.
///
/// Elements are set in an order agreeable to the grid's internal layout.
fn fill_solid(&mut self, value: Self::Element)
where
Self::Element: Copy,
Self: ExactSizeGrid,
{
self.fill_rect_solid(self.size().to_rect(), value);
}
/// Clears a rectangular region of the grid, setting all elements to their default value.
///
/// 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.).
///
/// [`Layout::iter_pos`]: layout::Layout::iter_pos
fn clear_rect(&mut self, bounds: Rect)
where
Self::Element: Default,
{
self.fill_rect(bounds, |_| Default::default());
}
/// 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.).
///
/// [`Layout::iter_pos`]: layout::Layout::iter_pos
fn fill_rect(&mut self, bounds: Rect, mut f: impl FnMut(Pos) -> Self::Element) {
Self::Layout::iter_pos(self.trim_rect(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.).
///
/// [`Layout::iter_pos`]: layout::Layout::iter_pos
fn fill_rect_iter(&mut self, dst: Rect, iter: impl IntoIterator<Item = Self::Element>) {
Self::Layout::iter_pos(self.trim_rect(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.).
///
/// [`Layout::iter_pos`]: layout::Layout::iter_pos
fn fill_rect_solid(&mut self, dst: Rect, value: Self::Element)
where
Self::Element: Copy,
{
self.fill_rect(self.trim_rect(dst), |_| value);
}
}
#[cfg(test)]
mod tests {
extern crate alloc;
use crate::{core::Size, ops::layout::RowMajor};
use super::*;
use alloc::vec;
struct TestGrid {
grid: [[u8; 3]; 3],
}
impl GridBase for TestGrid {
fn size_hint(&self) -> (Size, Option<Size>) {
let size = Size::new(3, 3);
(size, Some(size))
}
}
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::OutOfBounds { pos })
}
}
}
#[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]);
}
}