-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Impl TryFrom
vector for directions and add InvalidDirectionError
#10884
Conversation
@@ -13,6 +13,19 @@ pub trait Primitive2d {} | |||
/// A marker trait for 3D primitives | |||
pub trait Primitive3d {} | |||
|
|||
/// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason why thiserror
is not used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
TryFrom
for the direction primitives.These are all equivalent:
For error cases, an
Err(InvalidDirectionError)
is returned. It contains the type of failure: