Changing a part of an array with minimal boundscheck overhead

I am trying to change the values of an [[bool; N]; N] with minimal runtime boundscheck:

impl Plane {
    fn new() -> Self {
        Self {
            blocks: [[false; DIM]; DIM],
        }
    }

    fn apply(&mut self, start: Coordinate, end: Coordinate, op: BoolOps) {
        if let Some(slice) = self.blocks.get_mut(start.x..=end.x) {
            for row in slice.iter_mut() {
                if let Some(b) = row.get_mut(start.y..=end.y) {
                    for b in b.iter_mut() {
                        match op {
                            BoolOps::MakeFalse => *b = false,
                            BoolOps::MakeTrue => *b = true,
                            BoolOps::Toggle => *b = !*b,
                        }
                        dbg!(&op);
                    }
                } else {
                    dbg!("filter 2");
                }
            }
        } else {
            dbg!("filter 1");
        }
    }

I keep getting "filter 1". I have ensured that none of the values of Coordinate exceed DIM. (In action). Why is this happening?

Most of your coordinates in d6.dat are out of bounds for the 5x5 grid. Maybe you need to make it bigger?

It was meant to be 1000. Sleep deprivation.

Thank you

1 Like