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

source

pub fn new(duration: Duration) -> Self

Create a new timer with the given duration

source

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.

source

pub fn just_finished(&self) -> bool

Whether the timer hit zero since the last call to tick.

source

pub fn finished(&self) -> bool

Whether the timer is at zero.

source

pub fn reset(&mut self)

Reset the timer to the starting duration. Resets finished and just_finished as well.

source

pub fn percent(&self) -> f32

Returns percent of timer elapsed (from 0.0 to 1.0)

source

pub fn percent_left(&self) -> f32

Returns percent of timer remaining (from 1.0 to 0.0)

source

pub fn duration(&self) -> Duration

Returns the full duration of the timer when newly started. See also remaining and elapsed

source

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.

source

pub fn remaining(&self) -> Duration

Returns the time currently remaining on the timer.

source

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.

source

pub fn elapsed(&self) -> Duration

Returns the time that has elapsed on the timer.

source

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.

Trait Implementations§

source§

impl Clone for Timer

source§

fn clone(&self) -> Timer

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Timer

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Timer

source§

fn default() -> Timer

Returns the “default value” for a type. Read more
source§

impl PartialEq for Timer

source§

fn eq(&self, other: &Timer) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Timer

source§

impl StructuralEq for Timer

source§

impl StructuralPartialEq for Timer

Auto Trait Implementations§

§

impl RefUnwindSafe for Timer

§

impl Send for Timer

§

impl Sync for Timer

§

impl Unpin for Timer

§

impl UnwindSafe for Timer

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.