pub struct Timer {
pub start_time: Instant,
pub task_name: String,
}
Expand description
A struct for measuring and reporting the duration of tasks.
§Examples
use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;
let timer = Timer::new("Some Task");
sleep(Duration::from_millis(100));
timer.stop(); // This will print the duration of the task
Fields§
§start_time: Instant
§task_name: String
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn new(task_name: &str) -> Self
pub fn new(task_name: &str) -> Self
Creates a new Timer
instance with the given task name.
§Examples
use tea_timer::Timer;
let timer = Timer::new("My Task");
assert_eq!(timer.task_name, "My Task");
Sourcepub fn restart(&mut self, task_name: &str)
pub fn restart(&mut self, task_name: &str)
Restarts the timer with a new task name.
§Examples
use tea_timer::Timer;
let mut timer = Timer::new("Task 1");
// Do some work...
timer.restart("Task 2");
assert_eq!(timer.task_name, "Task 2");
// Timer now measures a new task
Sourcepub fn duration(&self) -> Duration
pub fn duration(&self) -> Duration
Returns the duration elapsed since the timer started.
§Examples
use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;
let timer = Timer::new("Test Task");
sleep(Duration::from_millis(10));
assert!(timer.duration().as_millis() >= 10);
Sourcepub fn duration_str(&self) -> String
pub fn duration_str(&self) -> String
Returns a formatted string representation of the elapsed duration.
§Examples
use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;
let timer = Timer::new("Test Task");
sleep(Duration::from_millis(10));
assert!(timer.duration_str().contains("ms"));
pub fn elapsed_str(&self) -> String
pub fn took_str(&self) -> String
Sourcepub fn elapsed(&self)
pub fn elapsed(&self)
Prints the elapsed time for the task.
§Examples
use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;
let timer = Timer::new("Test Task");
sleep(Duration::from_millis(10));
timer.elapsed(); // This will print to stdout
Sourcepub fn stop(self)
pub fn stop(self)
Stops the timer and prints the duration of the task.
§Examples
use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;
let timer = Timer::new("Sleep Task");
sleep(Duration::from_millis(100));
timer.stop(); // This will print: "Sleep Task took 100.00ms" (approximately)
Sourcepub fn log(&self)
pub fn log(&self)
Logs the elapsed time using the log
crate.
This method is only available when the log
feature is enabled.
§Examples
use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;
let timer = Timer::new("Log Task");
sleep(Duration::from_millis(10));
timer.log(); // This will log using the log crate
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Timer
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> 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