Skip to content
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

godot-core: builtin: reimplement Rect2i to reduce need of inner usage #218

Merged
merged 3 commits into from
Apr 12, 2023

Conversation

yannick-was-taken
Copy link
Contributor

@yannick-was-taken yannick-was-taken commented Apr 7, 2023

Addresses part of #209 .

Only doing Rect2i for now, so that suggestions only have to be applied once, instead of four times ;)

The implementations are tested (regular case + edge case where applicable) including an itest that checks equivalence between the inner type's fns and the implemented fns.

Documentation is mostly taken from Godot's docs for Rect2i, with little changes here and there.

There are a bunch of FIXMEs that need to be resolved before this is mergable: Godot's rect2i.h prints errors in some cases when the rect size is negative. In accordance with what I believe to be a design principle of gdext, the equivalent fns panic in that case (compare to cast/try_cast: default is to assume and panic?).

There could be _unchecked fns that likely should be marked unsafe, as though they would not introduce memory unsafety, they would result in "Garbage In / Garbage Out", which in Rust usually is considered unsafe, as the safe bet would be to prevent the Garbage In situation via typing, or at least make it return Option<Garbage Out>. The Option approach could be used here as well, but would change the type's API as opposed to what it is in GdScript.

So please provide input on those FIXMEs as to what you think should be done here.

@@ -58,7 +62,7 @@ impl Rect2i {
pub fn from_corners(position: Vector2i, end: Vector2i) -> Self {
Self {
position,
size: position + end,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this line here was a bug, so should be cherry-picked regardless of this PR's status:

End = Position + Size
-> Size = End - Position

@Bromeon Bromeon added quality-of-life No new functionality, but improves ergonomics/internals c: core Core components labels Apr 7, 2023
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! 🙂

Generally, I think quite some code could be simplified by thinking more in terms of vectors than individual coordinates. For example, a function Vector2i::cwise_max/cwise_min() (component-wise max/min) would make a lot of operations easier. We should also try to reuse Self::from_corners() wherever it is simpler than the other constructors.

Regarding duplicating this 4 times (int/float, 2D/3D) -- do you think it makes sense if we find an abstraction? Can also just be a macro, there is some prior art for the vectors already. But I fully agree we should iron out the Rect2i case first.

Also, a lot of these APIs have been previously built in gdnative. While we should not copy-paste code (license), let's at least use the prior art to see how APIs are designed and how special cases are handled. We can gladly deviate from it when we have a reason, but it would avoid starting from scratch and repeating the many discussions we had in previous issues (e.g. godot-rust/gdnative#867 or godot-rust/gdnative#948).

The tests are a great addition, thanks for writing them 👍

godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
godot-core/src/builtin/rect2i.rs Outdated Show resolved Hide resolved
itest/rust/src/rect2i_test.rs Outdated Show resolved Hide resolved
@yannick-was-taken
Copy link
Contributor Author

Regarding duplicating this 4 times (int/float, 2D/3D) -- do you think it makes sense if we find an abstraction? Can also just be a macro, there is some prior art for the vectors already. But I fully agree we should iron out the Rect2i case first.

I agree, especially for Rect2/Rect2i this makes sense. The vector implementation should serve as a good starting point, but I guess this will be in a separate commit, once Rect2 is added to the mix.

@yannick-was-taken yannick-was-taken force-pushed the feature/reimplement-rect2i branch 3 times, most recently from 09fcd03 to c90a08b Compare April 7, 2023 22:36
@yannick-was-taken
Copy link
Contributor Author

After implementing the requested changes, quite a few fns are no longer const, this is mostly blocked by const_trait_impl, which is currently experimental, but seems to be needed for using overloaded operators in const contexts. However, this shouldn't pose too much of a problem.

@Bromeon
Copy link
Member

Bromeon commented Apr 9, 2023

Regarding our coord_min/max discussion, it looks like the float Vector2/3 implementations already have min/max. These should probably be renamed -- if not for clarity, then for (future) confusion an implicitly provided Ord::min/max.

@yannick-was-taken
Copy link
Contributor Author

Regarding our coord_min/max discussion, it looks like the float Vector2/3 implementations already have min/max. These should probably be renamed -- if not for clarity, then for (future) confusion an implicitly provided Ord::min/max.

As min/max on the float vectors did effectively the same as coord_min/coord_max just using glam, I removed the fns in favor of those two.

I agree that we should not imply that there is an (implicit) ordering on vectors, and I think the coord_ variants achieve that.

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, and splitting into separate commits! 🙂

godot-core/src/builtin/vector_macros.rs Outdated Show resolved Hide resolved
itest/rust/src/rect2i_test.rs Outdated Show resolved Hide resolved
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

bors r+

@bors
Copy link
Contributor

bors bot commented Apr 12, 2023

Build succeeded:

  • full-ci

@bors bors bot merged commit 21401a1 into godot-rust:master Apr 12, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components quality-of-life No new functionality, but improves ergonomics/internals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants