chore(lambda-rs): Update math API to return errors instead of panicking #97
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Refactors the math library to return
Resulttypes with descriptive errors instead of panicking. This allows users to handle math errors gracefully and makes the API more idiomatic Rust. The change aligns with the repository guidelines that state: "avoid using panic unless absolutely necessary and allow for the user handle errors whenever possible."Related Issues
Changes
MathErrorenum incrates/lambda-rs/src/math/error.rswith descriptive error variants for all fallible operationsVector::cross()to returnResult<Self, MathError>instead of panicking on dimension mismatchesVector::normalize()to returnResult<Self, MathError>instead of panicking on zero-length vectorsMatrix::determinant()to returnResult<f32, MathError>instead of panicking on empty or non-square matricesrotate_matrix()to returnResult<MatrixLike, MathError>instead of panicking on invalid inputsrotate_matrix()axis parameter from genericInputVectorto concrete[f32; 3]arrayreflective_room.rs,textured_cube.rs) and internal usage (scene_math.rs) to handle the new Result typesreflective-room.md,textured-cube.md) to reflect the API changesType of Change
Affected Crates
lambda-rslambda-rs-platformlambda-rs-argslambda-rs-loggingChecklist
cargo +nightly fmt --all)cargo clippy --workspace --all-targets -- -D warnings)cargo test --workspace)Testing
Commands run:
cargo build --workspace cargo test --workspace cargo run --example reflective_room cargo run --example textured_cubeManual verification steps (if applicable):
reflective_roomexample and verify rotation behavior is unchangedtextured_cubeexample and verify cube renders correctly with rotationScreenshots/Recordings
N/A - No visual changes; API behavior remains the same for valid inputs.
Platform Testing
Additional Notes
Breaking Changes
The following public APIs now return
Resulttypes:Vector::cross()fn cross(&self, other: &Self) -> Selffn cross(&self, other: &Self) -> Result<Self, MathError>Vector::normalize()fn normalize(&self) -> Selffn normalize(&self) -> Result<Self, MathError>Matrix::determinant()fn determinant(&self) -> f32fn determinant(&self) -> Result<f32, MathError>rotate_matrix()fn rotate_matrix<...>(...) -> OutputMatrixfn rotate_matrix<...>(...) -> Result<MatrixLike, MathError>Migration Guide
Users calling these functions will need to handle the
Result:New Error Types
The
MathErrorenum provides detailed context for each failure case:CrossProductDimension { actual }- Cross product requires 3D vectorsMismatchedVectorDimensions { left, right }- Vectors must have matching dimensionsInvalidRotationAxis { axis }- Rotation axis must be a unit axis vectorInvalidRotationMatrixSize { rows, cols }- Rotation requires a 4x4 matrixNonSquareMatrix { rows, cols }- Operation requires square matrixEmptyMatrix- Operation requires a non-empty matrixZeroLengthVector- Cannot normalize a zero-length vector