From 4f191d8689c05d9fe1a1c9bce3e6632d5a7dd403 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Mon, 18 Jul 2022 11:00:17 +0200 Subject: [PATCH] safety considerations --- src/main.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index e7fbe80..e83f928 100644 --- a/src/main.rs +++ b/src/main.rs @@ -204,7 +204,7 @@ impl SField { #[inline(always)] fn print(&self) { - for i in 0..self.size * self.size { + for i in 0..self.num_fields { if i != 0 && i % self.size == 0 { println!(); } @@ -273,10 +273,12 @@ impl SField { fn put_valid_nr(&mut self) -> bool { let current_nr = self.get_field_at_pos(self.pos); + // safety: + // self.possible_vals is initialized with self.size * self.size + // so self.pos can safely be used to index here let possible_vals = unsafe { self.possible_values.get_unchecked(self.pos) }; - for idx in 0..possible_vals.len() { - let nr = unsafe { possible_vals.get_unchecked(idx) }; + for nr in possible_vals.iter() { if *nr <= current_nr { continue; } @@ -308,11 +310,18 @@ impl SField { #[inline(always)] fn is_fixed(&self) -> bool { + // safety: self.pos can be used to index the field unchecked + // since the only methods modifying self.pos are + // `next()` and `prev()` and they do bounds checking unsafe { *self.fixed.get_unchecked(self.pos) == 1 } } #[inline(always)] fn get_field_at_pos(&self, pos: usize) -> usize { + // safety: + // TODO + // would need to mathematically explain that the calculations in + // get_row(), get_col() and get_block() can never exceed 0..self.num_fields unsafe { *self.field.get_unchecked(pos) } }