Skip to content

Commit

Permalink
Add spin_sleep_util crate (#23)
Browse files Browse the repository at this point in the history
* Add spin_sleep_util crate

* Deprecate LoopHelper
  • Loading branch information
alexheretic authored Jan 4, 2024
1 parent 0bac1b5 commit dcd48ef
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- run: rustup update stable
- uses: actions/checkout@v4
- run: cargo test
- run: cargo test --workspace

rustfmt:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Unreleased (v1.2.0)
* Deprecate `LoopHelper`. Instead use _spin_sleep_util_ crate.
* Windows: Use a high resolution waitable timer when available (>= Windows 10, version 1803).
* Windows: Replace _winapi_ with _windows-sys_ dependency.
* Windows: Remove _once_cell_ dependency.
Expand Down
13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
[package]
name = "spin_sleep"
version = "1.1.1"
authors = ["Alex Butler <[email protected]>"]
edition = "2021"
authors = ["Alex Butler <[email protected]>"]
description = "Accurate sleeping. Only use native sleep as far as it can be trusted, then spin."
repository = "https://github.com/alexheretic/spin-sleep"
keywords = ["sleep"]
license = "Apache-2.0"
readme="README.md"
readme = "README.md"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52", features = [
"Win32_Foundation", "Win32_Security", "Win32_System", "Win32_System_Threading", "Win32_Media"
"Win32_Foundation",
"Win32_Security",
"Win32_System",
"Win32_System_Threading",
"Win32_Media",
] }

[dev-dependencies]
Expand All @@ -20,3 +24,6 @@ approx = "0.5"
[features]
# Controls certain tests that are not deterministic
nondeterministic_tests = []

[workspace]
members = ["util"]
36 changes: 6 additions & 30 deletions src/loop_helper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use super::*;
use std::time::{Duration, Instant};

Expand All @@ -8,36 +10,8 @@ use std::time::{Duration, Instant};
///
/// Can limit a loop rate to a desired target using
/// [`LoopHelper::loop_sleep`](struct.LoopHelper.html#method.loop_sleep).
///
/// # Issues
/// There are known limitations/flaws, see [#19](https://github.com/alexheretic/spin-sleep/issues/19).
/// `LoopHelper` will probably be deprecated for removal in the future.
///
/// # Example
///
/// ```no_run
/// use spin_sleep::LoopHelper;
///
/// let mut loop_helper = LoopHelper::builder()
/// .report_interval_s(0.5) // report every half a second
/// .build_with_target_rate(250.0); // limit to 250 FPS if possible
///
/// let mut current_fps = None;
///
/// loop {
/// let delta = loop_helper.loop_start(); // or .loop_start_s() for f64 seconds
///
/// // compute_something(delta);
///
/// if let Some(fps) = loop_helper.report_rate() {
/// current_fps = Some(fps.round());
/// }
///
/// // render_fps(current_fps);
///
/// loop_helper.loop_sleep(); // sleeps to achieve a 250 FPS rate
/// }
/// ```
#[doc(hidden)]
#[deprecated = "Use spin_sleep_util crate"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoopHelper {
target_delta: Duration,
Expand All @@ -51,6 +25,8 @@ pub struct LoopHelper {
}

/// Builds [`LoopHelper`](struct.LoopHelper.html).
#[doc(hidden)]
#[deprecated = "Use spin_sleep_util crate"]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LoopHelperBuilder {
report_interval: Option<Duration>,
Expand Down
2 changes: 2 additions & 0 deletions util/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# v0.1.0
* Add `Interval`, `RateReporter`.
13 changes: 13 additions & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "spin_sleep_util"
version = "0.1.0"
edition = "2021"
authors = ["Alex Butler <[email protected]>"]
description = "Utils using spin_sleep"
repository = "https://github.com/alexheretic/spin-sleep"
keywords = ["sleep", "loop", "interval"]
license = "Apache-2.0"
readme = "README.md"

[dependencies]
spin_sleep = { path = "..", version = "1" }
1 change: 1 addition & 0 deletions util/LICENSE
20 changes: 20 additions & 0 deletions util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
spin_sleep_util
[![crates.io](https://img.shields.io/crates/v/spin_sleep_util.svg)](https://crates.io/crates/spin_sleep_util)
[![Documentation](https://docs.rs/spin_sleep_util/badge.svg)](https://docs.rs/spin_sleep_util)
===============
Utils using spin_sleep.

## Example: Frame limiter
`Interval` may be used to limit a loop to a max fps by calling `Interval::tick` at the start or end of each loop.

```rust
// Create an interval to tick 144 times each second
let mut interval = spin_sleep_util::interval(Duration::from_secs(1) / 144);
loop {
compute_something(); // do loop work

// tick: sleep using a SpinSleeper until next tick.
// The default `Skip` missed ticke behaviour is appropriate for a frame limiter
interval.tick();
}
```
Loading

0 comments on commit dcd48ef

Please sign in to comment.