safety considerations

This commit is contained in:
ddidderr 2022-07-18 11:00:17 +02:00
parent 9d157745d9
commit 4f191d8689
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -204,7 +204,7 @@ impl SField {
#[inline(always)] #[inline(always)]
fn print(&self) { 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 { if i != 0 && i % self.size == 0 {
println!(); println!();
} }
@ -273,10 +273,12 @@ impl SField {
fn put_valid_nr(&mut self) -> bool { fn put_valid_nr(&mut self) -> bool {
let current_nr = self.get_field_at_pos(self.pos); 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) }; let possible_vals = unsafe { self.possible_values.get_unchecked(self.pos) };
for idx in 0..possible_vals.len() { for nr in possible_vals.iter() {
let nr = unsafe { possible_vals.get_unchecked(idx) };
if *nr <= current_nr { if *nr <= current_nr {
continue; continue;
} }
@ -308,11 +310,18 @@ impl SField {
#[inline(always)] #[inline(always)]
fn is_fixed(&self) -> bool { 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 } unsafe { *self.fixed.get_unchecked(self.pos) == 1 }
} }
#[inline(always)] #[inline(always)]
fn get_field_at_pos(&self, pos: usize) -> usize { 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) } unsafe { *self.field.get_unchecked(pos) }
} }