Skip to content

Create enumerating version of all_tuples #15936

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

Closed
wants to merge 3 commits into from

Conversation

mweatherley
Copy link
Contributor

@mweatherley mweatherley commented Oct 15, 2024

Objective

When writing macros that blanket-implement traits over tuples, it is often necessary to index into anonymous tuple struct fields. This turns out to be somewhat annoying, since it requires the indices to be passed sequentially in the macro invocation, which prevents the macro from being used with all_tuples.

The goal here is to provide support for this pattern by introducing an enumerating version of all_tuples that can be used in these circumstances.

Solution

Create all_tuples_enumerated, a procedural macro which behaves much like all_tuples but enumerates its output. Here is an example from the docs:

trait Squawk {
    fn squawk(&self);
}

// If every type in a tuple is `Squawk`, the tuple can squawk by having its
// constituents squawk sequentially:
macro_rules! impl_squawk {
    ($(($n:tt, $T:ident)),*) => {
        impl<$($T: Squawk),*> Squawk for ($($T,)*) {
            fn squawk(&self) {
                $(
                    self.$n.squawk();
                )*
            }
        }
    };
}

all_tuples_enumerated!(impl_squawk, 1, 15, T);
// impl_squawk!((0, T0));
// impl_squawk!((0, T0), (1, T1));
// ..
// impl_squawk!((0, T0) .. (14, T14));

The implementation for StableInterpolate over tuples has been changed to use this pattern instead of doing manual enumeration. It also actually uses fake_variadic now.

Testing

Tested via macro expansion and compiling.

@mweatherley mweatherley added C-Code-Quality A section of code that is hard to understand or change A-Utils Utility functions and types S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Macros Code that generates Rust code labels Oct 15, 2024
@mweatherley mweatherley added the C-Docs An addition or correction to our documentation label Oct 15, 2024
Copy link
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.

Pretty sure this would also be pretty useful for bevy_reflect too (this is pretty much what we need to make impl_reflect_tuple! compatible with all_tuples!)

@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Oct 15, 2024
@alice-i-cecile
Copy link
Member

FYI I'm merging the simple fix in #15933 now, but I do think we should move fully to this. It does make me think we should pull all_tuples out into its own crate though: I'm not a fan of bevy_math relying on bevy_utils.

@BenjaminBrienen
Copy link
Contributor

I can try my hand at that again using this once this is merged and I'm feeling better.

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.

Lovely :)

@pablo-lua pablo-lua 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 19, 2024
@ghost
Copy link

ghost commented Nov 8, 2024

https://github.com/bevyengine/variadics_please has been created, please open a PR in that repo for the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Utils Utility functions and types C-Code-Quality A section of code that is hard to understand or change C-Docs An addition or correction to our documentation 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants