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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::{
core::{GridError, HasSize, Layout, Pos, Rect},
ops::{GridWrite, unchecked::TrustedSizeGrid},
};
/// Write elements to a 2-dimensional grid position without bounds checking.
pub trait GridWriteUnchecked {
/// 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 without bounds checking.
///
/// ## Safety
///
/// Calling this method with an out-of-bounds position is _[undefined behavior][]_.
///
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
unsafe fn set_unchecked(&mut self, pos: Pos, value: Self::Element);
/// Sets elements within a rectangular region of the grid without bounds checking.
///
/// 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.
///
/// ## Safety
///
/// The caller must ensure that all positions in the rectangle are valid positions in the grid.
///
/// ## Performance
///
/// The default implementation uses [`Layout::iter_pos`] to iterate over the rectangle,
/// involving a call to [`GridWriteUnchecked::set_unchecked`] for each element. Other
/// implementations may optimize this, for example by using a more efficient iteration strategy
/// (for linear writes, etc.).
unsafe fn fill_rect_unchecked(&mut self, dst: Rect, mut f: impl FnMut(Pos) -> Self::Element) {
Self::Layout::iter_pos(dst).for_each(|pos| unsafe {
self.set_unchecked(pos, f(pos));
});
}
/// Sets elements within a rectangular region of the grid without bounds checking.
///
/// 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. If the iterator has more elements than the rectangle, the behavior is undefined.
///
/// ## Safety
///
/// The caller must ensure that all positions in the rectangle are valid positions in the grid
/// and that the iterator does not yield more elements than the rectangle can hold.
///
/// ## Performance
///
/// The default implementation uses [`Layout::iter_pos`] to iterate over the rectangle,
/// involving a call to [`GridWriteUnchecked::set_unchecked`] for each element. Other
/// implementations may optimize this, for example by using a more efficient iteration strategy
/// (for linear writes, etc.).
unsafe fn fill_rect_iter_unchecked(
&mut self,
dst: Rect,
iter: impl IntoIterator<Item = Self::Element>,
) {
Self::Layout::iter_pos(dst)
.zip(iter)
.for_each(|(pos, value)| unsafe {
self.set_unchecked(pos, value);
});
}
/// Sets elements within a rectangular region of the grid without bounds checking.
///
/// 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.
///
/// ## Safety
///
/// The caller must ensure that all positions in the rectangle are valid positions in the grid.
///
/// ## 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.).
unsafe fn fill_rect_solid_unchecked(&mut self, bounds: Rect, value: Self::Element)
where
Self::Element: Copy,
{
unsafe { self.fill_rect_unchecked(bounds, |_| value) };
}
}
/// Automatically implement `GridWrite` when `GridWriteUnchecked` + `TrustedSizeGrid` are implemented.
impl<T: GridWriteUnchecked + TrustedSizeGrid> GridWrite for T {
type Element = T::Element;
type Layout = T::Layout;
fn set(&mut self, pos: Pos, value: Self::Element) -> Result<(), GridError> {
if self.contains(pos) {
unsafe {
self.set_unchecked(pos, value);
}
Ok(())
} else {
Err(GridError)
}
}
fn fill_rect(&mut self, bounds: Rect, f: impl FnMut(Pos) -> Self::Element) {
let size = self.size().to_rect();
let rect = bounds.intersect(size);
unsafe { self.fill_rect_unchecked(rect, f) }
}
fn fill_rect_iter(&mut self, dst: Rect, iter: impl IntoIterator<Item = Self::Element>) {
let size = self.size().to_rect();
let rect = dst.intersect(size);
unsafe { self.fill_rect_iter_unchecked(rect, iter) }
}
fn fill_rect_solid(&mut self, dst: Rect, value: Self::Element)
where
Self::Element: Copy,
{
let size = self.size().to_rect();
let rect = dst.intersect(size);
unsafe { self.fill_rect_solid_unchecked(rect, value) }
}
}
#[cfg(test)]
mod tests {
extern crate alloc;
use super::*;
use crate::core::RowMajor;
use alloc::vec;
struct UncheckedTestGrid {
grid: [[u8; 3]; 3],
}
unsafe impl TrustedSizeGrid for UncheckedTestGrid {
fn width(&self) -> usize {
3
}
fn height(&self) -> usize {
3
}
}
impl GridWriteUnchecked for UncheckedTestGrid {
type Element = u8;
type Layout = RowMajor;
unsafe fn set_unchecked(&mut self, pos: Pos, value: Self::Element) {
self.grid[pos.y][pos.x] = value;
}
}
#[test]
fn impl_unsafe_set_ok() {
let mut grid = UncheckedTestGrid { 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_unsafe_set_out_of_bounds_x() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let pos = Pos { x: 3, y: 1 };
assert!(grid.set(pos, 42).is_err());
assert_eq!(grid.grid, [[0, 0, 0], [0, 0, 0], [0, 0, 0]]);
}
#[test]
fn impl_unsafe_set_out_of_bounds_y() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let pos = Pos { x: 1, y: 3 };
assert!(grid.set(pos, 42).is_err());
assert_eq!(grid.grid, [[0, 0, 0], [0, 0, 0], [0, 0, 0]]);
}
#[test]
fn impl_unsafe_set_unchecked_in_bounds() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let pos = Pos { x: 2, y: 2 };
unsafe {
grid.set_unchecked(pos, 99);
}
assert_eq!(grid.grid[2][2], 99);
}
#[test]
fn impl_unsafe_fill_rect_complete() {
let mut grid = UncheckedTestGrid { 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_unsafe_fill_rect_partial_in_bounds() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(0, 0, 2, 2).unwrap();
grid.fill_rect(bounds, |pos| if pos.x == 1 && pos.y == 1 { 99 } else { 42 });
assert_eq!(grid.grid, [[42, 42, 0], [42, 99, 0], [0, 0, 0]]);
}
#[test]
fn impl_unsafe_fill_rect_partial_out_of_bounds() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(1, 1, 4, 4).unwrap(); // Out of bounds on the right and bottom
grid.fill_rect(bounds, |_| 42);
assert_eq!(grid.grid, [[0, 0, 0], [0, 42, 42], [0, 42, 42]]);
}
#[test]
fn impl_unsafe_fill_rect_iter_complete() {
let mut grid = UncheckedTestGrid { 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_unsafe_fill_rect_iter_partial_in_bounds() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(0, 0, 2, 2).unwrap();
grid.fill_rect_iter(bounds, vec![42, 99]);
#[rustfmt::skip]
assert_eq!(grid.grid, [
[42, 99, 0],
[0, 0, 0],
[0, 0, 0]
]);
}
#[test]
fn impl_unsafe_fill_rect_iter_partial_in_bounds_with_extra() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(0, 0, 2, 1).unwrap();
grid.fill_rect_iter(bounds, vec![42, 99, 100]);
#[rustfmt::skip]
assert_eq!(grid.grid, [
[42, 99, 0],
[0, 0, 0],
[0, 0, 0]
]);
}
#[test]
fn impl_unsafe_fill_rect_iter_partial_out_of_bounds() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(1, 1, 4, 4).unwrap(); // Out of bounds on the right and bottom
grid.fill_rect_iter(bounds, vec![42, 99, 100]);
#[rustfmt::skip]
assert_eq!(grid.grid, [
[0, 0, 0],
[0, 42, 99],
[0, 100, 0]
]);
}
#[test]
fn impl_unsafe_fill_rect_iter_out_of_bounds() {
let mut grid = UncheckedTestGrid { grid: [[0; 3]; 3] };
let bounds = Rect::from_ltrb(3, 3, 4, 4).unwrap(); // Out of bounds on the right and bottom
grid.fill_rect_iter(bounds, vec![42, 99, 100]);
#[rustfmt::skip]
assert_eq!(grid.grid, [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
}
#[test]
fn impl_unsafe_fill_rect_solid() {
let mut grid = UncheckedTestGrid { 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]);
}
}