From 465d1139e7fc658aaf283c2f99d3b5c8c6982af0 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 21 Oct 2024 03:17:59 +0100 Subject: [PATCH] Replace TwoIterators with Either in bevy_animation (#16036) # Objective - Less code - Better iterator (implements `size_hint` for example) ## Solution - Use `either` - This change is free because `bevy_animation` depends on `bevy_asset`, which already depends on `either` ## Testing CI --- crates/bevy_animation/Cargo.toml | 1 + crates/bevy_animation/src/gltf_curves.rs | 33 +++++------------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/crates/bevy_animation/Cargo.toml b/crates/bevy_animation/Cargo.toml index 1614d98f54980..6fb9fb9f5992b 100644 --- a/crates/bevy_animation/Cargo.toml +++ b/crates/bevy_animation/Cargo.toml @@ -39,6 +39,7 @@ derive_more = { version = "1", default-features = false, features = [ "from", "display", ] } +either = "1.13" thread_local = "1" uuid = { version = "1.7", features = ["v4"] } smallvec = "1" diff --git a/crates/bevy_animation/src/gltf_curves.rs b/crates/bevy_animation/src/gltf_curves.rs index 74d86fd03baf8..8d3b6cc248442 100644 --- a/crates/bevy_animation/src/gltf_curves.rs +++ b/crates/bevy_animation/src/gltf_curves.rs @@ -6,6 +6,7 @@ use bevy_math::{ }; use bevy_reflect::Reflect; use derive_more::derive::{Display, Error, From}; +use either::Either; /// A keyframe-defined curve that "interpolates" by stepping at `t = 1.0` to the next keyframe. #[derive(Debug, Clone, Reflect)] @@ -189,11 +190,11 @@ where match self.core.sample_interp(t) { InterpolationDatum::Exact(v) | InterpolationDatum::LeftTail(v) - | InterpolationDatum::RightTail(v) => TwoIterators::Left(v.iter().copied()), + | InterpolationDatum::RightTail(v) => Either::Left(v.iter().copied()), InterpolationDatum::Between(u, v, s) => { let interpolated = u.iter().zip(v.iter()).map(move |(x, y)| x.lerp(*y, s)); - TwoIterators::Right(interpolated) + Either::Right(interpolated) } } } @@ -243,14 +244,14 @@ where match self.core.sample_interp(t) { InterpolationDatum::Exact(v) | InterpolationDatum::LeftTail(v) - | InterpolationDatum::RightTail(v) => TwoIterators::Left(v.iter().cloned()), + | InterpolationDatum::RightTail(v) => Either::Left(v.iter().cloned()), InterpolationDatum::Between(u, v, s) => { let interpolated = u.iter() .zip(v.iter()) .map(move |(x, y)| if s >= 1.0 { y.clone() } else { x.clone() }); - TwoIterators::Right(interpolated) + Either::Right(interpolated) } } } @@ -302,10 +303,10 @@ where // Pick out the part of this that actually represents the position (instead of tangents), // which is the middle third. let width = self.core.width(); - TwoIterators::Left(v[width..(width * 2)].iter().copied()) + Either::Left(v[width..(width * 2)].iter().copied()) } - InterpolationDatum::Between((t0, u), (t1, v), s) => TwoIterators::Right( + InterpolationDatum::Between((t0, u), (t1, v), s) => Either::Right( cubic_spline_interpolate_slices(self.core.width() / 3, u, v, s, t1 - t0), ), } @@ -392,26 +393,6 @@ pub enum WeightsCurve { // HELPERS // //---------// -enum TwoIterators { - Left(A), - Right(B), -} - -impl Iterator for TwoIterators -where - A: Iterator, - B: Iterator, -{ - type Item = T; - - fn next(&mut self) -> Option { - match self { - TwoIterators::Left(a) => a.next(), - TwoIterators::Right(b) => b.next(), - } - } -} - /// Helper function for cubic spline interpolation. fn cubic_spline_interpolation( value_start: T,