Struct bones_framework::time::Stopwatch
source · pub struct Stopwatch { /* private fields */ }
Expand description
A Stopwatch is a struct that track elapsed time when started.
Examples
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
assert_eq!(stopwatch.elapsed_secs(), 0.0);
stopwatch.tick(Duration::from_secs_f32(1.0)); // tick one second
assert_eq!(stopwatch.elapsed_secs(), 1.0);
stopwatch.pause();
stopwatch.tick(Duration::from_secs_f32(1.0)); // paused stopwatches don't tick
assert_eq!(stopwatch.elapsed_secs(), 1.0);
stopwatch.reset(); // reset the stopwatch
assert!(stopwatch.paused());
assert_eq!(stopwatch.elapsed_secs(), 0.0);
Implementations§
source§impl Stopwatch
impl Stopwatch
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new unpaused Stopwatch
with no elapsed time.
Examples
let stopwatch = Stopwatch::new();
assert_eq!(stopwatch.elapsed_secs(), 0.0);
assert_eq!(stopwatch.paused(), false);
sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the elapsed time since the last reset
of the stopwatch.
Examples
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
stopwatch.tick(Duration::from_secs(1));
assert_eq!(stopwatch.elapsed(), Duration::from_secs(1));
See Also
elapsed_secs
- if an f32
value is desirable instead.
elapsed_secs_f64
- if an f64
is desirable instead.
sourcepub fn elapsed_secs(&self) -> f32
pub fn elapsed_secs(&self) -> f32
Returns the elapsed time since the last reset
of the stopwatch, in seconds.
Examples
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
stopwatch.tick(Duration::from_secs(1));
assert_eq!(stopwatch.elapsed_secs(), 1.0);
See Also
elapsed
- if a Duration
is desirable instead.
elapsed_secs_f64
- if an f64
is desirable instead.
sourcepub fn elapsed_secs_f64(&self) -> f64
pub fn elapsed_secs_f64(&self) -> f64
Returns the elapsed time since the last reset
of the stopwatch, in seconds, as f64.
See Also
elapsed
- if a Duration
is desirable instead.
elapsed_secs
- if an f32
is desirable instead.
sourcepub fn set_elapsed(&mut self, time: Duration)
pub fn set_elapsed(&mut self, time: Duration)
Sets the elapsed time of the stopwatch.
Examples
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
stopwatch.set_elapsed(Duration::from_secs_f32(1.0));
assert_eq!(stopwatch.elapsed_secs(), 1.0);
sourcepub fn tick(&mut self, delta: Duration) -> &Self
pub fn tick(&mut self, delta: Duration) -> &Self
Advance the stopwatch by delta
seconds.
If the stopwatch is paused, ticking will not have any effect
on elapsed time.
Examples
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
stopwatch.tick(Duration::from_secs_f32(1.5));
assert_eq!(stopwatch.elapsed_secs(), 1.5);
sourcepub fn pause(&mut self)
pub fn pause(&mut self)
Pauses the stopwatch. Any call to tick
while
paused will not have any effect on the elapsed time.
Examples
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
stopwatch.pause();
stopwatch.tick(Duration::from_secs_f32(1.5));
assert!(stopwatch.paused());
assert_eq!(stopwatch.elapsed_secs(), 0.0);
sourcepub fn unpause(&mut self)
pub fn unpause(&mut self)
Unpauses the stopwatch. Resume the effect of ticking on elapsed time.
Examples
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
stopwatch.pause();
stopwatch.tick(Duration::from_secs_f32(1.0));
stopwatch.unpause();
stopwatch.tick(Duration::from_secs_f32(1.0));
assert!(!stopwatch.paused());
assert_eq!(stopwatch.elapsed_secs(), 1.0);
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Stopwatch
impl Send for Stopwatch
impl Sync for Stopwatch
impl Unpin for Stopwatch
impl UnwindSafe for Stopwatch
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere T: Default,
source§fn from_world(_world: &World) -> T
fn from_world(_world: &World) -> T
Self
using data from the given World
.