Skip to content

Commit ee9bea1

Browse files
authored
Use variadics_please to implement StableInterpolate on tuples. (#16931)
# Objective Now that `variadics_please` has a 1.1 release, we can re-implement the original solution. ## Solution Copy-paste the code from the [original PR](#15931) branch :)
1 parent bacc693 commit ee9bea1

File tree

2 files changed

+10
-86
lines changed

2 files changed

+10
-86
lines changed

crates/bevy_math/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ smallvec = { version = "1.11" }
2828
bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", features = [
2929
"glam",
3030
], optional = true }
31+
variadics_please = "1.1"
3132

3233
[dev-dependencies]
3334
approx = "0.5"

crates/bevy_math/src/common_traits.rs

+9-86
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use core::{
55
fmt::Debug,
66
ops::{Add, Div, Mul, Neg, Sub},
77
};
8+
use variadics_please::all_tuples_enumerated;
89

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

396-
// If you're confused about how #[doc(fake_variadic)] works,
397-
// then the `all_tuples` macro is nicely documented (it can be found in the `bevy_utils` crate).
398-
// tl;dr: `#[doc(fake_variadic)]` goes on the impl of tuple length one.
399-
// the others have to be hidden using `#[doc(hidden)]`.
400397
macro_rules! impl_stable_interpolate_tuple {
401-
(($T:ident, $n:tt)) => {
402-
impl_stable_interpolate_tuple! {
403-
@impl
404-
#[cfg_attr(any(docsrs, docsrs_dep), doc(fake_variadic))]
405-
#[cfg_attr(
406-
any(docsrs, docsrs_dep),
407-
doc = "This trait is implemented for tuples up to 11 items long."
408-
)]
409-
($T, $n)
410-
}
411-
};
412-
($(($T:ident, $n:tt)),*) => {
413-
impl_stable_interpolate_tuple! {
414-
@impl
415-
#[cfg_attr(any(docsrs, docsrs_dep), doc(hidden))]
416-
$(($T, $n)),*
417-
}
418-
};
419-
(@impl $(#[$($meta:meta)*])* $(($T:ident, $n:tt)),*) => {
420-
$(#[$($meta)*])*
398+
($(#[$meta:meta])* $(($n:tt, $T:ident)),*) => {
399+
$(#[$meta])*
421400
impl<$($T: StableInterpolate),*> StableInterpolate for ($($T,)*) {
422401
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
423402
(
@@ -430,68 +409,12 @@ macro_rules! impl_stable_interpolate_tuple {
430409
};
431410
}
432411

433-
// (See `macro_metavar_expr`, which might make this better.)
434-
// This currently implements `StableInterpolate` for tuples of up to 11 elements.
435-
impl_stable_interpolate_tuple!((T, 0));
436-
impl_stable_interpolate_tuple!((T0, 0), (T1, 1));
437-
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2));
438-
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2), (T3, 3));
439-
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4));
440-
impl_stable_interpolate_tuple!((T0, 0), (T1, 1), (T2, 2), (T3, 3), (T4, 4), (T5, 5));
441-
impl_stable_interpolate_tuple!(
442-
(T0, 0),
443-
(T1, 1),
444-
(T2, 2),
445-
(T3, 3),
446-
(T4, 4),
447-
(T5, 5),
448-
(T6, 6)
449-
);
450-
impl_stable_interpolate_tuple!(
451-
(T0, 0),
452-
(T1, 1),
453-
(T2, 2),
454-
(T3, 3),
455-
(T4, 4),
456-
(T5, 5),
457-
(T6, 6),
458-
(T7, 7)
459-
);
460-
impl_stable_interpolate_tuple!(
461-
(T0, 0),
462-
(T1, 1),
463-
(T2, 2),
464-
(T3, 3),
465-
(T4, 4),
466-
(T5, 5),
467-
(T6, 6),
468-
(T7, 7),
469-
(T8, 8)
470-
);
471-
impl_stable_interpolate_tuple!(
472-
(T0, 0),
473-
(T1, 1),
474-
(T2, 2),
475-
(T3, 3),
476-
(T4, 4),
477-
(T5, 5),
478-
(T6, 6),
479-
(T7, 7),
480-
(T8, 8),
481-
(T9, 9)
482-
);
483-
impl_stable_interpolate_tuple!(
484-
(T0, 0),
485-
(T1, 1),
486-
(T2, 2),
487-
(T3, 3),
488-
(T4, 4),
489-
(T5, 5),
490-
(T6, 6),
491-
(T7, 7),
492-
(T8, 8),
493-
(T9, 9),
494-
(T10, 10)
412+
all_tuples_enumerated!(
413+
#[doc(fake_variadic)]
414+
impl_stable_interpolate_tuple,
415+
1,
416+
11,
417+
T
495418
);
496419

497420
/// A type that has tangents.

0 commit comments

Comments
 (0)