Impl TryFrom vector for directions and add InvalidDirectionError#10884
Impl TryFrom vector for directions and add InvalidDirectionError#10884alice-i-cecile merged 4 commits intobevyengine:mainfrom
TryFrom vector for directions and add InvalidDirectionError#10884Conversation
|
|
||
| /// An error indicating that a direction is invalid because it is zero or not finite. | ||
| #[derive(Debug)] | ||
| pub struct InvalidDirectionError; |
There was a problem hiding this comment.
I think this is probably more useful as an enum: the different failure modes are useful to bubble up.
There was a problem hiding this comment.
I think we need to do a custom try_normalize for this, but it should be easy since it's just an .is_finite() and a comparison against zero
There was a problem hiding this comment.
Or alternatively, do the checks again, but only in error cases. I think this could be better since we can also check for NaN separately.
There was a problem hiding this comment.
I changed it to an enum with Zero, Infinite and NaN variants and added tests to verify that it gives the correct errors.
|
Is there a reason why the |
tim-blackbird
left a comment
There was a problem hiding this comment.
Looks good to me if the new constructor is changed to use this error as well
|
I changed the |
| NaN, | ||
| } | ||
|
|
||
| impl std::fmt::Display for InvalidDirectionError { |
There was a problem hiding this comment.
Is there any reason why thiserror is not used here?
There was a problem hiding this comment.
bevy_math doesn't have thiserror as a dependency currently. Could add it though, especially if we add more error types
Objective
Implement
TryFrom<Vec2>/TryFrom<Vec3>for direction primitives as considered in #10857.Solution
Implement
TryFromfor the direction primitives.These are all equivalent:
For error cases, an
Err(InvalidDirectionError)is returned. It contains the type of failure: