Skip to content

Commit

Permalink
feat: panic if animation frame duration is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Jun 5, 2022
1 parent 9f8034d commit 98cedb0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/animation.rs
Expand Up @@ -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<usize>, frame_duration: Duration) -> Self {
Self::from_iter(index_range, frame_duration)
Expand All @@ -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<Item = usize>, frame_duration: Duration) -> Self {
indices
.into_iter()
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/spec.rs
Expand Up @@ -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
Expand Down

0 comments on commit 98cedb0

Please sign in to comment.