Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Replace the bool argument of Timer with TimerMode #6247

Closed
wants to merge 6 commits into from
Closed

[Merged by Bors] - Replace the bool argument of Timer with TimerMode #6247

wants to merge 6 commits into from

Conversation

lovelymono
Copy link
Contributor

@lovelymono lovelymono commented Oct 13, 2022

As mentioned in #2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as Stopwatch, and possibly future ones, such as DiscreteStopwatch and DiscreteTimer from #2683, into one struct.

Signed-off-by: Lena Milizé me@lvmn.org

Objective

Fixes #2926.

Solution

Introduce TimerMode which replaces the bool argument of Timer constructors. A Default value for TimerMode is Once.


Changelog

Added

  • TimerMode enum, along with variants TimerMode::Once and TimerMode::Repeating

Changed

  • Replace bool argument of Timer::new and Timer::from_seconds with TimerMode
  • Change repeating: bool field of Timer with mode: TimerMode

Migration Guide

  • Replace Timer::new(duration, false) with Timer::new(duration, TimerMode::Once).
  • Replace Timer::new(duration, true) with Timer::new(duration, TimerMode::Repeating).
  • Replace Timer::from_seconds(seconds, false) with Timer::from_seconds(seconds, TimerMode::Once).
  • Replace Timer::from_seconds(seconds, true) with Timer::from_seconds(seconds, TimerMode::Repeating).
  • Change timer.repeating() to timer.mode() == TimerMode::Repeating.

As mentioned in #2926, it's better to have an explicit type that clearly
communicates the intent of the timer mode rather than an opaque boolean,
which can be only understood when knowing the signature or reading the
documentation.

Signed-off-by: Lena Milizé <me@lvmn.org>
crates/bevy_time/src/timer.rs Outdated Show resolved Hide resolved
crates/bevy_time/src/timer.rs Outdated Show resolved Hide resolved
crates/bevy_time/src/timer.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@tguichaoua tguichaoua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, I think all usage of bool for repeating should be completly replaced by TimerMode to avoid mixing and confusion.

crates/bevy_time/src/timer.rs Outdated Show resolved Hide resolved
crates/bevy_time/src/timer.rs Outdated Show resolved Hide resolved
Replaces Timer::set_repeating and Timer::repeating with Timer::set_mode
and Timer::mode, respectively.

Adds PartialEq, Eq, Hash to TimerMode. Replaces impl Default with derive
macro.

Signed-off-by: Lena Milizé <me@lvmn.org>
Signed-off-by: Lena Milizé <me@lvmn.org>
@lovelymono lovelymono requested a review from tguichaoua October 13, 2022 13:51
@tguichaoua
Copy link
Contributor

@lovelymono don't forgot to click on "Mark as resolved" on conversations. It's help other reviewers to know what is already done.

@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Time Involves time keeping and reporting M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Oct 13, 2022
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much clearer! This is a great first PR.

Signed-off-by: Lena Milizé <me@lvmn.org>
tguichaoua:
> The problem with utility methods is that if you want to be consistant
> you have to add one for each variant for each enum. That is complex to
> maintain.
> For fieldless enums using equality is more straightforward and seems
> to be the most used way in the rust community.

Signed-off-by: Lena Milizé <me@lvmn.org>
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Oct 13, 2022
We want to check that the timer is what we set it to, and before the
condition was backwards.

Signed-off-by: Lena Milizé <me@lvmn.org>
@alice-i-cecile
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Oct 17, 2022
As mentioned in #2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from #2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes #2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
@bors
Copy link
Contributor

bors bot commented Oct 17, 2022

@bors bors bot changed the title Replace the bool argument of Timer with TimerMode [Merged by Bors] - Replace the bool argument of Timer with TimerMode Oct 17, 2022
@bors bors bot closed this Oct 17, 2022
@lovelymono lovelymono deleted the timer-mode branch October 17, 2022 14:06
james7132 pushed a commit to james7132/bevy that referenced this pull request Oct 19, 2022
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
james7132 pushed a commit to james7132/bevy that referenced this pull request Oct 28, 2022
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
jackbackes added a commit to jackbackes/bevy-website that referenced this pull request Nov 12, 2022
Updated Getting Started docs to match the change at bevyengine/bevy#6247 and replace from_seconds(time, bool) with from_seconds(time, TimerMode)
bors bot pushed a commit to bevyengine/bevy-website that referenced this pull request Nov 12, 2022
Updated Getting Started: Resources docs to match the change at bevyengine/bevy#6247 and replace from_seconds(time, bool) with from_seconds(time, TimerMode).

Co-authored-by: thejohnbackes <jwbackes@gmail.com>
Pietrek14 pushed a commit to Pietrek14/bevy that referenced this pull request Dec 17, 2022
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
)

As mentioned in bevyengine#2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.

This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from bevyengine#2683, into one struct.

Signed-off-by: Lena Milizé <me@lvmn.org>

# Objective

Fixes bevyengine#2926.

## Solution

Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.

---

## Changelog

### Added

- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`

### Changed

- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`

## Migration Guide

- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Time Involves time keeping and reporting C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor away the bool argument of Timer
4 participants