kpreid / all-is-cubes

Yet another block/voxel game; in this one the blocks are made out of blocks. Runs in browsers on WebGL+WebAssembly.
https://kpreid.dreamwidth.org/tag/all+is+cubes
Apache License 2.0
147 stars 8 forks source link

Possible Incompleteness #486

Closed YichiZhang0613 closed 2 months ago

YichiZhang0613 commented 2 months ago

In all-is-cubes/all-is-cubes-content/src/alg.rs, I don't think the crash caused by gradient.len() == 0 as comment read is the behavior we expect. So I think use assert! to check whether gradient.len() is equal to 0 before use it is a better way.

/// Panics if `gradient.len() == 0`.
pub(crate) fn gradient_lookup(gradient: &[Block], value: f32) -> &Block {
    &gradient[((value * gradient.len() as f32) as usize).clamp(0, gradient.len() - 1)]
}
kpreid commented 2 months ago

The behavior of assert! is to panic if the condition is not met. The function already panics when gradient.len() == 0. So, adding an assert! would not change the behavior of the function (other than a better error message).

So, I am not sure what change you are proposing. There is no correct thing for it to return if gradient is empty, so it must panic (or do worse things such as abort, but panicking is clearly better).