From 98cedb030279d1188d751fc4595c02fbab5acab6 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Sun, 5 Jun 2022 09:27:12 +0000 Subject: [PATCH] feat: panic if animation frame duration is zero --- src/animation.rs | 29 +++++++++++++++++++++++++++++ tests/spec.rs | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/animation.rs b/src/animation.rs index c12c6ae..92fa01e 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -59,6 +59,10 @@ impl SpriteSheetAnimation { /// Create a new animation from index-range, using the same frame duration for each frame. /// /// For more granular configuration, see [`from_frames`](SpriteSheetAnimation::from_frames) + /// + /// # Panics + /// + /// Panics if the duration is zero #[must_use] pub fn from_range(index_range: RangeInclusive, frame_duration: Duration) -> Self { Self::from_iter(index_range, frame_duration) @@ -76,6 +80,10 @@ impl SpriteSheetAnimation { /// ``` /// /// For more granular configuration, see [`from_frames`](SpriteSheetAnimation::from_frames) + /// + /// # Panics + /// + /// Panics if the duration is zero pub fn from_iter(indices: impl IntoIterator, frame_duration: Duration) -> Self { indices .into_iter() @@ -147,9 +155,30 @@ impl Default for AnimationMode { impl Frame { /// Create a new animation frame + /// + /// The duration must be > 0 + /// + /// # Panics + /// + /// Panics if the duration is zero #[inline] #[must_use] pub fn new(index: usize, duration: Duration) -> Self { + assert!( + !duration.is_zero(), + "zero-duration is invalid for animation frame" + ); Self { index, duration } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic] + fn panics_for_zero_duration() { + let _ = Frame::new(0, Duration::ZERO); + } +} diff --git a/tests/spec.rs b/tests/spec.rs index 2e37cbd..aa3eeb5 100644 --- a/tests/spec.rs +++ b/tests/spec.rs @@ -20,7 +20,7 @@ fn plugin_does_not_crash() { .unwrap() .add(SpriteSheetAnimation::from_range( 0..=2, - Duration::from_nanos(0), + Duration::from_nanos(1), )); app.world