Wrangling casts when adding signed values to an unsigned integer

Usually I use either a function like this:


Or instead just use a type like this struct { x: isize, y: isize }, I think especially with vector math it is helpful when your vector type can represent both positions and directions.

That way your switch on direction can return a direction vector and you can do const new_position = position.add(direction);

If you later have some code that always needs to act on position values that never can be negative you also could have 2 types, something like Position and ChunkCoord where the latter would be used to index into some arrays, then you can have some function that converts your position to a chunk coord and that can assert at the same time that no part is negative.

1 Like