Struct rusty_time::Timer
source · pub struct Timer { /* private fields */ }
Expand description
A simple timer that is externally driven. .tick()
must be called with a delta
time for the timer to advance.
Use it in a loop like this:
use std::time::{Duration, Instant};
use rusty_time::Timer;
fn main() {
let mut timer = Timer::new(Duration::from_secs_f32(1.5));
let mut start_time = Instant::now();
loop {
timer.tick(start_time.elapsed());
start_time = Instant::now();
println!(
"Time on timer: {:.2}s ({:.1}%)",
timer.remaining().as_secs_f32(),
timer.percent_left() * 100.0
);
if timer.just_finished() {
break;
}
}
println!("Timer finished!");
}
Implementations§
source§impl Timer
impl Timer
sourcepub fn tick(&mut self, delta: Duration) -> &mut Self
pub fn tick(&mut self, delta: Duration) -> &mut Self
Advance the timer by a specific duration. This must be called for the timer to be useful.
sourcepub fn just_finished(&self) -> bool
pub fn just_finished(&self) -> bool
Whether the timer hit zero since the last call to tick
.
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the timer to the starting duration. Resets finished
and just_finished
as well.
sourcepub fn percent_left(&self) -> f32
pub fn percent_left(&self) -> f32
Returns percent of timer remaining (from 1.0 to 0.0)
sourcepub fn set_duration(&mut self, duration: Duration)
pub fn set_duration(&mut self, duration: Duration)
Sets the starting duration of the timer, but does not change the remaining time on the timer.
See reset
to reset the remaining time to the starting duration.
sourcepub fn set_remaining(&mut self, remaining: Duration)
pub fn set_remaining(&mut self, remaining: Duration)
Sets the time currently remaining on the timer. Sets/unsets that the timer has finished as appropriate.
sourcepub fn set_elapsed(&mut self, elapsed: Duration)
pub fn set_elapsed(&mut self, elapsed: Duration)
Sets the time currently elapsed on the timer. Sets/unsets that the timer has finished as appropriate.