Struct Timer

Source
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

Source

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");
Source

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
Source

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);
Source

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"));
Source

pub fn elapsed_str(&self) -> String

Source

pub fn took_str(&self) -> String

Source

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
Source

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)
Source

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§

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() -> Self

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

impl Display for Timer

Source§

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

Formats the value using the given formatter. Read more

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.