becheran / grid

Two dimensional grid data structure
MIT License
82 stars 27 forks source link

Remove unsafe code from `.get()` and `.get_mut()` #22

Closed nicoburns closed 1 year ago

nicoburns commented 1 year ago

There's no real need to have unsafe code here, and it could cause issues if the values of the rows or cols fields get out of sync with the length of the data Vec.

becheran commented 1 year ago

Thx for the PR. A reason for using unsafe here is performance after doing the bound checks manually via the if condition. The bounds are checked before the multiplication which could potentially panic. The call get(usize::MAX, usize::MAX) would panic after the merge which would change the behavior of the function as documented:

Returns None if an element beyond the grid bounds is tried to be accessed.