Struct StopwatchStruct

Source
pub struct StopwatchStruct<T>
where T: Fn(u32),
{ 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),

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 your_crate_name::stopwatch::{StopwatchStruct, StopwatchStatus}; // Replace your_crate_name

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 loop continues indefinitely until the status field of the StopwatchStruct is manually set to StopwatchStatus::Stopped from outside this function (e.g., from another thread or an external signal).

§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};

let mut stopwatch = StopwatchStruct::new(|time| {
    println!("\nStopwatch finished at {} seconds!", time);
});

let mut writer = stdout();

// To stop the stopwatch, you would typically change its status from another thread
// or based on some external event. For demonstration, we'll do it after a delay.
let mut stopwatch_clone = stopwatch.clone(); // Clone to move into another thread
thread::spawn(move || {
    thread::sleep(Duration::from_secs(5));
    stopwatch_clone.status = StopwatchStatus::Stopped; // Stop after 5 seconds
});

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

Trait Implementations§

Source§

impl<T> Clone for StopwatchStruct<T>
where T: Fn(u32) + 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) + 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>
where T: Send,

§

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.