pub struct StopwatchStruct<T>{
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: u32The current elapsed time in seconds.
status: StopwatchStatusThe current status of the stopwatch (Running or Stopped).
operation_on_stop: TA 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>
impl<T> StopwatchStruct<T>
Sourcepub fn new(operation_on_stop: T) -> StopwatchStruct<T>
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 toStopped. 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);
});Sourcepub fn start_timer<W: Write>(&mut self, writer: &mut W)
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:
- Pressing
Ctrl+C. This will execute theoperation_on_stopclosure and exit the process. - Programmatically by setting the
statusfield toStopwatchStatus::Stopped. This will stop the loop and execute theoperation_on_stopclosure.
§Arguments
writer- A mutable reference to any type that implements thestd::io::Writetrait (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>
impl<T> Clone for StopwatchStruct<T>
Source§fn clone(&self) -> StopwatchStruct<T>
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)
const fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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> 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
Mutably borrows from an owned value. Read more