chore(lambda-rs): Update math API to return errors instead of panicking#97
Merged
chore(lambda-rs): Update math API to return errors instead of panicking#97
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the math library to return Result types instead of panicking, aligning with Rust best practices and the repository's guidelines to "avoid using panic unless absolutely necessary." The change converts four key math operations (Vector::cross, Vector::normalize, Matrix::determinant, and rotate_matrix) to return descriptive errors when given invalid inputs.
Changes:
- Introduced comprehensive
MathErrorenum with variants for all fallible math operations - Converted panic-based error handling to Result-based error handling for cross product, normalize, determinant, and matrix rotation operations
- Updated all examples, tests, and documentation to handle the new Result types appropriately
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/lambda-rs/src/math/error.rs | New error module with MathError enum and Display/Error trait implementations |
| crates/lambda-rs/src/math/mod.rs | Exports the new error module and MathError type |
| crates/lambda-rs/src/math/vector.rs | Updated cross() and normalize() to return Result types; updated tests |
| crates/lambda-rs/src/math/matrix.rs | Updated determinant() and rotate_matrix() to return Result types; updated tests |
| crates/lambda-rs/src/render/scene_math.rs | Updated compute_model_matrix to handle rotate_matrix Result |
| crates/lambda-rs/examples/textured_cube.rs | Updated to handle rotate_matrix Result with expect() |
| crates/lambda-rs/examples/reflective_room.rs | Updated to handle rotate_matrix Result with expect() |
| docs/tutorials/textured-cube.md | Updated tutorial code samples and metadata to reflect API changes |
| docs/tutorials/reflective-room.md | Updated tutorial code samples and metadata to reflect API changes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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