Direction: Rename from_normalized to new_unchecked#11425
Direction: Rename from_normalized to new_unchecked#11425alice-i-cecile merged 6 commits intobevyengine:mainfrom
from_normalized to new_unchecked#11425Conversation
|
|
||
| /// Create a [`Direction3d`] from a [`Vec3`] that is already normalized. | ||
| /// | ||
| /// ## Safety |
There was a problem hiding this comment.
| /// ## Safety | |
| /// # Safety |
| // SAFETY: Multipling a normalized vector (Vec3::Y) with a rotation returns a normalized vector. | ||
| direction: unsafe { Direction3d::new_unchecked(rotation * Vec3::Y) }, |
There was a problem hiding this comment.
Can Direction(3d,2d) implements rotation ? (probably in another PR)
to directly use rotation * Direction3d::Y without unsafe.
There was a problem hiding this comment.
Conventionally, unsafe in Rust is used strictly for memory safety issues (or other odd corners where you might hit genuine Undefined Behavior in the compiler). While I strongly think that there should be alternatives that work like unsafe, but for other forms of danger, we should stick to the conventions of the ecosystem.
That said, I love the rest of these changes and think we need to warn more aggressively here.
Yes, but it's also used in cases where there is a promise that a certain type must have some limitation (being normalized in this case). All the Having a direction vector that is not normalized may not cause UB in the traditional sense, but it could cause unforeseen bugs. |
|
No, but it almost certainly cause bugs in code that expect Marking it as |
|
From the Book:
|
Panics are arent much of a problem here (they can easily be tracked down), what is a problem is a silent bug (A bug that doesn't provide any debug information when it occurs). For example, if you have a player movement function that takes in a The
- The The "contract" here is that the inner vector is normalized. As I said earlier, making this |
|
This has been a longstanding discussion in the rust community. "What should I mark as unsafe?" For the standard library, the answer is "only methods with invariants, which, if not upheld, invoke undefined behaviors". There is even a chapter on the rust reference about "behaviors not considered unsafe": https://doc.rust-lang.org/stable/reference/behavior-not-considered-unsafe.html. "Logic Errors" is in there. This doesn't answer the question of whether we should mark the method as unsafe. The thing is, we already expose API that assumes the values passed-in represent a normal vector: The |
|
Well, I guess I'll make it safe. |
from_normalized to new_unchecked and declare unsafefrom_normalized to new_unchecked
Co-authored-by: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com>
Co-authored-by: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com>
Jondolf
left a comment
There was a problem hiding this comment.
Some comment nits, but otherwise looks good
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Quaternions do not need to be normalized. Only quaternions that are used for rotation do. I've used quaternions to represent angular velocity before, and those were intentionally not normalized. In nalgebra, there's UnitQuaternion specifically for rotations, but there's nothing similar in bevy/glam. |
Objective
Direction2d::from_normalized&Direction3d::from_normalizeddon't emphasize that importance of the vector being normalized enough.Solution
Rename
from_normalizedtonew_uncheckedand add more documentation.Direction2dandDirection3dwere added somewhat recently in #10466 (after 0.12), so I don't think documenting the changelog and migration guide is necessary (Since there is no major previous version to migrate from).But here it is anyway in case it's needed:
Changelog
Direction2d::from_normalizedandDirection3d::from_normalizedtonew_unchecked.Migration Guide
Direction2d::from_normalizedandDirection3d::from_normalizedtonew_unchecked.