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

Use variadics_please to implement StableInterpolate on tuples. #16931

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ smallvec = { version = "1.11" }
bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", features = [
"glam",
], optional = true }
variadics_please = "1.1"

[dev-dependencies]
approx = "0.5"
Expand Down
95 changes: 9 additions & 86 deletions crates/bevy_math/src/common_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::{
fmt::Debug,
ops::{Add, Div, Mul, Neg, Sub},
};
use variadics_please::all_tuples_enumerated;

/// A type that supports the mathematical operations of a real vector space, irrespective of dimension.
/// In particular, this means that the implementing type supports:
Expand Down Expand Up @@ -393,31 +394,9 @@ impl StableInterpolate for Dir3A {
}
}

// If you're confused about how #[doc(fake_variadic)] works,
// then the `all_tuples` macro is nicely documented (it can be found in the `bevy_utils` crate).
// tl;dr: `#[doc(fake_variadic)]` goes on the impl of tuple length one.
// the others have to be hidden using `#[doc(hidden)]`.
macro_rules! impl_stable_interpolate_tuple {
(($T:ident, $n:tt)) => {
impl_stable_interpolate_tuple! {
@impl
#[cfg_attr(any(docsrs, docsrs_dep), doc(fake_variadic))]
#[cfg_attr(
any(docsrs, docsrs_dep),
doc = "This trait is implemented for tuples up to 11 items long."
)]
($T, $n)
}
};
($(($T:ident, $n:tt)),*) => {
impl_stable_interpolate_tuple! {
@impl
#[cfg_attr(any(docsrs, docsrs_dep), doc(hidden))]
$(($T, $n)),*
}
};
(@impl $(#[$($meta:meta)*])* $(($T:ident, $n:tt)),*) => {
$(#[$($meta)*])*
($(#[$meta:meta])* $(($n:tt, $T:ident)),*) => {
$(#[$meta])*
impl<$($T: StableInterpolate),*> StableInterpolate for ($($T,)*) {
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
(
Expand All @@ -430,68 +409,12 @@ macro_rules! impl_stable_interpolate_tuple {
};
}

// (See `macro_metavar_expr`, which might make this better.)
// This currently implements `StableInterpolate` for tuples of up to 11 elements.
impl_stable_interpolate_tuple!((T, 0));
impl_stable_interpolate_tuple!((T0, 0), (T1, 1));
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2));
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2), (T3, 3));
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4));
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5));
impl_stable_interpolate_tuple!(
(T0, 0),
(T1, 1),
(T2, 2),
(T3, 3),
(T4, 4),
(T5, 5),
(T6, 6)
);
impl_stable_interpolate_tuple!(
(T0, 0),
(T1, 1),
(T2, 2),
(T3, 3),
(T4, 4),
(T5, 5),
(T6, 6),
(T7, 7)
);
impl_stable_interpolate_tuple!(
(T0, 0),
(T1, 1),
(T2, 2),
(T3, 3),
(T4, 4),
(T5, 5),
(T6, 6),
(T7, 7),
(T8, 8)
);
impl_stable_interpolate_tuple!(
(T0, 0),
(T1, 1),
(T2, 2),
(T3, 3),
(T4, 4),
(T5, 5),
(T6, 6),
(T7, 7),
(T8, 8),
(T9, 9)
);
impl_stable_interpolate_tuple!(
(T0, 0),
(T1, 1),
(T2, 2),
(T3, 3),
(T4, 4),
(T5, 5),
(T6, 6),
(T7, 7),
(T8, 8),
(T9, 9),
(T10, 10)
all_tuples_enumerated!(
#[doc(fake_variadic)]
impl_stable_interpolate_tuple,
1,
11,
T
);

/// A type that has tangents.
Expand Down
Loading