Skip to content

Improve PhantomData held by curve adaptors#15881

Merged
alice-i-cecile merged 2 commits intobevyengine:mainfrom
mweatherley:curve-variance
Oct 13, 2024
Merged

Improve PhantomData held by curve adaptors#15881
alice-i-cecile merged 2 commits intobevyengine:mainfrom
mweatherley:curve-variance

Conversation

@mweatherley
Copy link
Copy Markdown
Contributor

@mweatherley mweatherley commented Oct 13, 2024

Objective

The previous PhantomData instances were written somewhat lazily, so they were just things like PhantomData<T> for curves with an output type of T. This looks innocuous, but it unnecessarily constrains Send/Sync inference based on T. See here.

Solution

Switch to PhantomData of the form PhantomData<fn() -> T> for most of these adaptors. Since they only have a functional relationship to T (i.e. it shows up in the return type of trait methods), this is more accurate.

Testing

Tested by compiling Bevy.

@mweatherley mweatherley added D-Trivial Nice and easy! A great choice to get started with Bevy C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Math Fundamental domain-agnostic mathematical operations X-Uncontroversial This work is generally agreed upon S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 13, 2024
@mweatherley
Copy link
Copy Markdown
Contributor Author

mweatherley commented Oct 13, 2024

I'm actually still a little bit unsure about MapCurve specifically. My reasoning was that it should behave like it owns a fn() -> S since it actually owns a curve with that output type; the fn(S) -> T phantom type looks reasonable but is also just going to be redundant in all real cases, because F: Fn(S) -> T whenever the struct is a curve (so, for instance, it could literally be a function pointer fn(S) -> T).

@alice-i-cecile alice-i-cecile added D-Straightforward Simple bug fixes and API improvements, docs, test and examples and removed D-Trivial Nice and easy! A great choice to get started with Bevy labels Oct 13, 2024
Copy link
Copy Markdown
Member

@MrGVSV MrGVSV left a comment

Choose a reason for hiding this comment

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

My brain struggles so hard with trying to understand variance, but this looks right to me

@MrGVSV
Copy link
Copy Markdown
Member

MrGVSV commented Oct 13, 2024

I'm actually still a little bit unsure about MapCurve specifically. My reasoning was that it should behave like it owns a fn() -> S since it actually owns a curve with that output type; the fn(S) -> T phantom type looks reasonable but is also just going to be redundant in all real cases, because F: Fn(S) -> T whenever the struct is a curve (so, for instance, it could literally be a function pointer fn(S) -> T).

When would MapCurve not be a curve? Is there a reason F isn't defined with the Fn(S) -> T bound?

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 13, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 13, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 13, 2024
@mweatherley
Copy link
Copy Markdown
Contributor Author

When would MapCurve not be a curve? Is there a reason F isn't defined with the Fn(S) -> T bound?

It's often regarded as good practice not to include trait bounds on struct definitions unless they are required for the struct itself to make sense (e.g. by using associated types in fields). (If anything, this helps avoid needless duplication of trait bounds, but there are other reasons as well.)

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 13, 2024
Merged via the queue into bevyengine:main with commit 6521e75 Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Math Fundamental domain-agnostic mathematical operations C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants