Skip to content

Commit 1d0c9ca

Browse files
Updated systems to no longer crash when attempting to render out of
bounds
1 parent 99bdd0d commit 1d0c9ca

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/renderer/buffer.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ impl Buffer {
5252
pub fn set<C: Into<Cell>>(&mut self, loc: impl Into<Vec2>, cell: C) {
5353
let idx = self.index_of(loc);
5454

55+
// Ignore if cell is out of bounds
56+
let Some(idx) = idx else {
57+
return;
58+
};
59+
5560
self.cells[idx] = cell.into();
5661
}
5762

@@ -64,24 +69,26 @@ impl Buffer {
6469
}
6570

6671
/// Returns a reverence to the cell at the given location.
67-
pub fn get(&self, loc: impl Into<Vec2>) -> &Cell {
68-
let idx = self.index_of(loc);
69-
&self.cells[idx]
72+
pub fn get(&self, loc: impl Into<Vec2>) -> Option<&Cell> {
73+
let idx = self.index_of(loc)?;
74+
self.cells.get(idx)
7075
}
7176

7277
/// Returns a mutable reference to the cell at the given location.
73-
pub fn get_mut(&mut self, loc: impl Into<Vec2>) -> &mut Cell {
74-
let idx = self.index_of(loc);
75-
&mut self.cells[idx]
78+
pub fn get_mut(&mut self, loc: impl Into<Vec2>) -> Option<&mut Cell> {
79+
let idx = self.index_of(loc)?;
80+
self.cells.get_mut(idx)
7681
}
7782

78-
fn index_of(&self, loc: impl Into<Vec2>) -> usize {
83+
fn index_of(&self, loc: impl Into<Vec2>) -> Option<usize> {
7984
let loc = loc.into();
8085
let idx = loc.y as usize * self.size.x as usize + loc.x as usize;
8186

82-
debug_assert!((idx as u16) < self.size.x * self.size.y);
87+
if (idx as u16) >= self.size.x * self.size.y {
88+
return None;
89+
}
8390

84-
idx.min((self.size.x as usize * self.size.y as usize) - 1)
91+
Some(idx.min((self.size.x as usize * self.size.y as usize) - 1))
8592
}
8693

8794
/// Clears the buffer
@@ -98,7 +105,10 @@ impl Buffer {
98105
for x in 0..self.size.x {
99106
for y in 0..self.size.y {
100107
if self.get((x, y)) != other.get((x, y)) {
101-
res.push((vec2(x, y), other.get((x, y))))
108+
res.push((
109+
vec2(x, y),
110+
other.get((x, y)).expect("Cell should be in bounds"),
111+
))
102112
}
103113
}
104114
}
@@ -112,7 +122,11 @@ impl Buffer {
112122
let mut max_whitespace_y = 0;
113123
for x in (0..self.size.x).rev() {
114124
for y in (0..self.size.y).rev() {
115-
if !self.get((x, y)).is_empty() {
125+
if !self
126+
.get((x, y))
127+
.expect("Cell should be in bounds")
128+
.is_empty()
129+
{
116130
max_whitespace_x = x.max(max_whitespace_x);
117131
max_whitespace_y = y.max(max_whitespace_y);
118132
}
@@ -133,7 +147,7 @@ impl Buffer {
133147

134148
for y in 0..new_size.y {
135149
for x in 0..new_size.x {
136-
new_elements.push(self.get((x, y)).clone());
150+
new_elements.push(self.get((x, y)).expect("Cell should be in bounds").clone());
137151
}
138152
}
139153

@@ -165,7 +179,12 @@ impl Render for Buffer {
165179

166180
let dest = vec2(x + loc.x, y + loc.y);
167181

168-
buffer.set(dest, self.get(vec2(x, y)).clone());
182+
buffer.set(
183+
dest,
184+
self.get(vec2(x, y))
185+
.expect("Cell should be in bounds")
186+
.clone(),
187+
);
169188
}
170189
}
171190
vec2(loc.x + buffer.size().x, loc.y + buffer.size().y)

src/window.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ impl Window {
276276
let cell = self.buffers[self.active_buffer].size();
277277
for x in 0..cell.x {
278278
for y in 0..cell.y {
279-
let cell = self.buffers[self.active_buffer].get((x, y));
279+
let cell = self.buffers[self.active_buffer]
280+
.get((x, y))
281+
.expect("Cell should be in bounds");
280282
queue!(self.io, cursor::MoveTo(x, y), Print(cell))?;
281283
}
282284
}

0 commit comments

Comments
 (0)