From 380826905dc60b641f75b85ee3df3ce53b05c63d Mon Sep 17 00:00:00 2001 From: Jerome Humbert <djeedai@gmail.com> Date: Wed, 16 Nov 2022 21:44:05 +0000 Subject: [PATCH] Take `impl Into<RepeatCount>` for easier usage (#78) Change the signature of `Tween::with_repeat_count()` to take an `impl Into<RepeatCount>` instead of a `RepeatCount` value, to make it easier to configure the `Tween`. Implement for `RepeatCount`: - `From<u32>` for an actual count, yielding `RepeatCount::Finite(value)` - `From<Duration>` for a duration "count", yielding `RepeatCount::For(value)` --- CHANGELOG.md | 10 ++++++++++ src/lib.rs | 27 ++++++++++++++++++++------- src/tweenable.rs | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17ef9be..b43cc8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unrelease] + +### Added + +- Added `From<u32>` and `From<Duration>` for `RepeatCount`, respectively yielding `RepeatCount::Finite(value)` and `RepeatCount::For(value)`. + +### Changed + +- Changed the signature of `with_repeat_count()` to take an `impl Into<RepeatCount>` instead of a `RepeatCount` by value. + ## [0.6.0] - 2022-11-15 ### Added diff --git a/src/lib.rs b/src/lib.rs index e1e083c..6f464c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -187,7 +187,26 @@ pub enum RepeatCount { Infinite, } -/// What to do when a tween animation needs to be repeated. +impl Default for RepeatCount { + fn default() -> Self { + Self::Finite(1) + } +} + +impl From<u32> for RepeatCount { + fn from(value: u32) -> Self { + Self::Finite(value) + } +} + +impl From<Duration> for RepeatCount { + fn from(value: Duration) -> Self { + Self::For(value) + } +} + +/// What to do when a tween animation needs to be repeated. See also +/// [`RepeatCount`]. /// /// Only applicable when [`RepeatCount`] is greater than the animation duration. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -204,12 +223,6 @@ pub enum RepeatStrategy { MirroredRepeat, } -impl Default for RepeatCount { - fn default() -> Self { - Self::Finite(1) - } -} - impl Default for RepeatStrategy { fn default() -> Self { Self::Repeat diff --git a/src/tweenable.rs b/src/tweenable.rs index e8827c1..1e7f6c5 100644 --- a/src/tweenable.rs +++ b/src/tweenable.rs @@ -574,8 +574,8 @@ impl<T> Tween<T> { /// Set the number of times to repeat the animation. #[must_use] - pub fn with_repeat_count(mut self, count: RepeatCount) -> Self { - self.clock.total_duration = compute_total_duration(self.clock.duration, count); + pub fn with_repeat_count(mut self, count: impl Into<RepeatCount>) -> Self { + self.clock.total_duration = compute_total_duration(self.clock.duration, count.into()); self } @@ -1245,6 +1245,37 @@ mod tests { ); } + #[test] + fn into_repeat_count() { + let tween = Tween::new( + EaseMethod::Linear, + Duration::from_secs(1), + TransformPositionLens { + start: Vec3::ZERO, + end: Vec3::ONE, + }, + ) + .with_repeat_count(5); + assert_eq!( + tween.total_duration(), + TotalDuration::Finite(Duration::from_secs(5)) + ); + + let tween = Tween::new( + EaseMethod::Linear, + Duration::from_secs(1), + TransformPositionLens { + start: Vec3::ZERO, + end: Vec3::ONE, + }, + ) + .with_repeat_count(Duration::from_secs(3)); + assert_eq!( + tween.total_duration(), + TotalDuration::Finite(Duration::from_secs(3)) + ); + } + /// Test ticking of a single tween in isolation. #[test] fn tween_tick() { -- GitLab