Struct StopwatchStruct

Source
pub struct StopwatchStruct<T>
where T: Fn(u32) + Send + Copy + 'static,
{ pub current_time: u32, pub status: StopwatchStatus, pub operation_on_stop: T, }
Expand description

Represents a stopwatch that measures elapsed time.

It takes a generic type T which must be a closure that accepts a u32 (the final current_time when the stopwatch stops).

Fields§

§current_time: u32

The current elapsed time in seconds.

§status: StopwatchStatus

The current status of the stopwatch (Running or Stopped).

§operation_on_stop: T

A closure that will be executed when the stopwatch is stopped. It receives the final current_time as an argument.

Implementations§

Source§

impl<T> StopwatchStruct<T>
where T: Fn(u32) + Send + Copy + 'static,

Source

pub fn new(operation_on_stop: T) -> StopwatchStruct<T>

Creates a new StopwatchStruct instance.

§Arguments
  • operation_on_stop - A closure that will be called when the stopwatch status is set to Stopped. It receives the total elapsed time in seconds as its argument.
§Returns

A new StopwatchStruct initialized with current_time at 0 and status as Running.

§Examples
use clock-timer::stopwatch::{StopwatchStruct, StopwatchStatus}; // Replace clock-timer

let mut stopwatch = StopwatchStruct::new(|time| {
    println!("Stopwatch stopped at {} seconds.", time);
});
Source

pub fn start_timer<W: Write>(&mut self, writer: &mut W)

Starts the stopwatch.

The stopwatch will increment its current_time every second and print the elapsed time to the provided writer, overwriting the previous line.

The timer can be stopped in two ways:

  1. Pressing Ctrl+C. This will execute the operation_on_stop closure and exit the process.
  2. Programmatically by setting the status field to StopwatchStatus::Stopped. This will stop the loop and execute the operation_on_stop closure.
§Arguments
  • writer - A mutable reference to any type that implements the std::io::Write trait (e.g., &mut std::io::Stdout).
§Examples
use your_crate_name::stopwatch::{StopwatchStruct, StopwatchStatus}; // Replace your_crate_name
use std::{io::stdout, thread, time::Duration};

// This stopwatch will be stopped by another thread after 5 seconds.
let mut stopwatch = StopwatchStruct::new(|time| {
    println!("\nStopwatch finished at {} seconds!", time);
});

let mut stopwatch_clone = stopwatch.clone();
thread::spawn(move || {
    thread::sleep(Duration::from_secs(5));
    stopwatch_clone.status = StopwatchStatus::Stopped;
});

stopwatch.start_timer(&mut stdout());
println!("Stopwatch loop ended.");

Trait Implementations§

Source§

impl<T> Clone for StopwatchStruct<T>
where T: Fn(u32) + Send + Copy + 'static + Clone,

Source§

fn clone(&self) -> StopwatchStruct<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for StopwatchStruct<T>
where T: Fn(u32) + Send + Copy + 'static + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for StopwatchStruct<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StopwatchStruct<T>
where T: RefUnwindSafe,

§

impl<T> Send for StopwatchStruct<T>

§

impl<T> Sync for StopwatchStruct<T>
where T: Sync,

§

impl<T> Unpin for StopwatchStruct<T>
where T: Unpin,

§

impl<T> UnwindSafe for StopwatchStruct<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.