Skip to content

Commit 465d113

Browse files
authored
Replace TwoIterators with Either in bevy_animation (bevyengine#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
1 parent 30d8451 commit 465d113

2 files changed

Lines changed: 8 additions & 26 deletions

File tree

‎crates/bevy_animation/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ derive_more = { version = "1", default-features = false, features = [
3939
"from",
4040
"display",
4141
] }
42+
either = "1.13"
4243
thread_local = "1"
4344
uuid = { version = "1.7", features = ["v4"] }
4445
smallvec = "1"

‎crates/bevy_animation/src/gltf_curves.rs‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use bevy_math::{
66
};
77
use bevy_reflect::Reflect;
88
use derive_more::derive::{Display, Error, From};
9+
use either::Either;
910

1011
/// A keyframe-defined curve that "interpolates" by stepping at `t = 1.0` to the next keyframe.
1112
#[derive(Debug, Clone, Reflect)]
@@ -189,11 +190,11 @@ where
189190
match self.core.sample_interp(t) {
190191
InterpolationDatum::Exact(v)
191192
| InterpolationDatum::LeftTail(v)
192-
| InterpolationDatum::RightTail(v) => TwoIterators::Left(v.iter().copied()),
193+
| InterpolationDatum::RightTail(v) => Either::Left(v.iter().copied()),
193194

194195
InterpolationDatum::Between(u, v, s) => {
195196
let interpolated = u.iter().zip(v.iter()).map(move |(x, y)| x.lerp(*y, s));
196-
TwoIterators::Right(interpolated)
197+
Either::Right(interpolated)
197198
}
198199
}
199200
}
@@ -243,14 +244,14 @@ where
243244
match self.core.sample_interp(t) {
244245
InterpolationDatum::Exact(v)
245246
| InterpolationDatum::LeftTail(v)
246-
| InterpolationDatum::RightTail(v) => TwoIterators::Left(v.iter().cloned()),
247+
| InterpolationDatum::RightTail(v) => Either::Left(v.iter().cloned()),
247248

248249
InterpolationDatum::Between(u, v, s) => {
249250
let interpolated =
250251
u.iter()
251252
.zip(v.iter())
252253
.map(move |(x, y)| if s >= 1.0 { y.clone() } else { x.clone() });
253-
TwoIterators::Right(interpolated)
254+
Either::Right(interpolated)
254255
}
255256
}
256257
}
@@ -302,10 +303,10 @@ where
302303
// Pick out the part of this that actually represents the position (instead of tangents),
303304
// which is the middle third.
304305
let width = self.core.width();
305-
TwoIterators::Left(v[width..(width * 2)].iter().copied())
306+
Either::Left(v[width..(width * 2)].iter().copied())
306307
}
307308

308-
InterpolationDatum::Between((t0, u), (t1, v), s) => TwoIterators::Right(
309+
InterpolationDatum::Between((t0, u), (t1, v), s) => Either::Right(
309310
cubic_spline_interpolate_slices(self.core.width() / 3, u, v, s, t1 - t0),
310311
),
311312
}
@@ -392,26 +393,6 @@ pub enum WeightsCurve {
392393
// HELPERS //
393394
//---------//
394395

395-
enum TwoIterators<A, B> {
396-
Left(A),
397-
Right(B),
398-
}
399-
400-
impl<A, B, T> Iterator for TwoIterators<A, B>
401-
where
402-
A: Iterator<Item = T>,
403-
B: Iterator<Item = T>,
404-
{
405-
type Item = T;
406-
407-
fn next(&mut self) -> Option<Self::Item> {
408-
match self {
409-
TwoIterators::Left(a) => a.next(),
410-
TwoIterators::Right(b) => b.next(),
411-
}
412-
}
413-
}
414-
415396
/// Helper function for cubic spline interpolation.
416397
fn cubic_spline_interpolation<T>(
417398
value_start: T,

0 commit comments

Comments
 (0)