min_timer
This is a small library that provides f64 based duration and timer. Standard
library's implementation uses integers. Thus, for a clock that gives time as
f64, this library should have higher performance. Additionally, there are less
checks. Although, there is strong type safety for SI units (seconds), which is
hopefully optimized away by the compiler.
Example
use ;
Usage
// Type safety.
// Seconds can be only added and subtracted with other seconds.
// Can be scaled with scalars tho.
// 4s * 5s = 20 s^2 -> Result has a different dimension: T^2.
// 4s + 5 = ? -> Meaningless, adding numbers of different dimensions: T + 1.
let duration = new;
// Or...
let duration = 10.0 * MILLI;
// To and from standard library's duration.
let std_dur = from;
// Provided implementation (using standard library).
// As written previously this has conversions to and from, doubles and integers.
// You should prefer directly using the standard library `timer` (std::time::Instant).
let now = new;
// Get the time.
// You won't be using this manually.
let start = now.now;
let elapsed = now.now - start;
// Creating timer, holds a ref to `now`.
let timer = new;
// Holds the start and gives you elapsed time.
let elapsed = timer.elapsed;
// Usefullness of timer:
// Moving forwards...
let timer = timer + new;
// Or...
let mut timer = new;
timer += new;
// Checking elapsed time...
// Simply, `timer` is taken as `elapsed` for these calcualtions.
let passed = timer > duration;
let remaining = duration - timer;
let same = timer == duration;
// Do not thrust `same` that is above!
// These are floating point numbers!
// Use the one on the next line.
let tolerance = new; // 1 us
Motivation
Why write this when there is the standard library? First, education: I got to
practice Rust, espacially newtype pattern with Sec. Second, I didn't now
much about std::time before writing this. Third, I will use this with GLFW
timer, which returns the time as a double in seconds. This way I will
implement Now with GLFW and there will be no conversions compared to:
let start = time;
let elapsed = time - start;
let seconds = elapsed.as_sec_f64; // conversion!
Check out my other crate, min_gl, for seeing the Now implementation for
the GLFW timer.
Copyright (C) 2022 Cem Geçgel gecgelcem@outlook.com