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

Infer StableInterpolate on tuples #15931

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

mweatherley
Copy link
Contributor

@mweatherley mweatherley commented Oct 15, 2024

Objective

Make StableInterpolate "just work" on tuples whose parts are each StableInterpolate types. These types arise notably through Curve::zip (or just through explicit mapping of a similar form). It would otherwise be kind of frustrating to stumble upon such a thing and then realize that, e.g., automatic resampling just doesn't work, even though there is a very "obvious" way to do it.

Solution

Infer StableInterpolate on tuples of up to size 11. I can make that number bigger, if desired. Unfortunately, I don't think that our standard "fake variadics" tools actually work for this; the anonymous field accessors of tuples are :tt for purposes of macro expansion, which means that you can't simplify away the identifiers by doing something clever like using recursion (which would work if they were :expr). Maybe someone who knows some incredibly dark magic could chime in with a better solution.

The expanded impls look like this:

impl<
        T0: StableInterpolate,
        T1: StableInterpolate,
        T2: StableInterpolate,
        T3: StableInterpolate,
        T4: StableInterpolate,
    > StableInterpolate for (T0, T1, T2, T3, T4)
{
    fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
        (
            <T0 as StableInterpolate>::interpolate_stable(&self.0, &other.0, t),
            <T1 as StableInterpolate>::interpolate_stable(&self.1, &other.1, t),
            <T2 as StableInterpolate>::interpolate_stable(&self.2, &other.2, t),
            <T3 as StableInterpolate>::interpolate_stable(&self.3, &other.3, t),
            <T4 as StableInterpolate>::interpolate_stable(&self.4, &other.4, t),
        )
    }
}

Testing

Expanded macros; it compiles.

Future

Make a version of the fake variadics workflow that supports this kind of thing.

@mweatherley mweatherley added 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 D-Macros Code that generates Rust code C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Oct 15, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Can you add the fake_variadics doc attribute? We've added this elsewhere and it really improves the rustdoc output.

I would suggest using the all_tuples macro here, but it's good to not pull in that dependency.

Copy link
Contributor

@NiseVoid NiseVoid left a comment

Choose a reason for hiding this comment

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

Kind of cursed, but it does look like this is the only way without introducing new deps.

@mockersf
Copy link
Member

@mweatherley
Copy link
Contributor Author

Can you add the fake_variadics doc attribute? We've added this elsewhere and it really improves the rustdoc output.

Do you know how to do that outside of the context of the all_tuples macro? Everything I try basically winds up with the compiler complaining to me about how I'm not allowed to use #[doc(fake_variadics)], even when I add a module-level allow.

I would suggest using the all_tuples macro here, but it's good to not pull in that dependency.

It doesn't work anyway :( I spent quite a while trying to do it that way and reached the conclusion that it's basically impossible.

@alice-i-cecile alice-i-cecile self-requested a review October 15, 2024 19:48
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I'm fine with this for now :)

@alice-i-cecile
Copy link
Member

@BenjaminBrienen @bash y'all have any advice? You were very helpful for this in #15387

@mweatherley
Copy link
Contributor Author

mweatherley commented Oct 15, 2024

I'm fine with this for now :)

For what it's worth, I got it working with a new version of all_tuples in my branch now. At least long-term, I'd also prefer to do it that way, since I have other traits I want to derive in a similar way. I'd be happy to shove the more complicated thing into a follow-up, too, though.

@BenjaminBrienen
Copy link
Contributor

I had the same errors he's describing. It only worked for me within all_tuples!.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 15, 2024
@alice-i-cecile
Copy link
Member

Follow-up it is then ;)

@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 15, 2024
Merged via the queue into bevyengine:main with commit a09104b Oct 15, 2024
35 checks passed
@mweatherley mweatherley deleted the interpolate-tuples branch October 15, 2024 20:15
@mweatherley mweatherley restored the interpolate-tuples branch October 15, 2024 20:15
@bash
Copy link
Contributor

bash commented Oct 15, 2024

@BenjaminBrienen @bash y'all have any advice? You were very helpful for this in #15387

Yeah #[doc(fake_variadic)] can be a bit of a hassle :))
I created a follow-up PR that adds it: #15933

github-merge-queue bot pushed a commit that referenced this pull request Oct 16, 2024
This is a follow-up to #15931 that adds `#[doc(fake_variadic)]` for
improved docs output :)
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-Macros Code that generates Rust code 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.

6 participants