From 40f878602326a43732e57989029d72cfb748f79a Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Thu, 14 Jul 2022 22:54:23 +0200 Subject: [PATCH] Make Matrix::place less crashy if HexoSynth is doing something wrong --- src/matrix.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/matrix.rs b/src/matrix.rs index 1279179..b89dad0 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -656,6 +656,9 @@ impl Matrix { /// does not have cycles, otherwise an upload to the DSP thread via /// [Matrix::sync] will fail. /// + /// If you try to place a cell outside the grid, it will not be placed + /// and just silently ignored. + /// /// You can safely check the DSP topology of changes using /// the convenience function [Matrix::change_matrix] /// or alternatively: [Matrix::save_matrix], [Matrix::restore_matrix] @@ -665,6 +668,11 @@ impl Matrix { pub fn place(&mut self, x: usize, y: usize, mut cell: Cell) { cell.x = x as u8; cell.y = y as u8; + + if x >= self.w || y >= self.h { + return; + } + self.matrix[x * self.h + y] = cell; }