pub struct Counter<T> {
pub start: T,
pub end: T,
pub direction: Direction,
}Expand description
A counter stores start and end times, and implements Display
to either show the time passed since start, or until end,
formatted as HH(+):MM:SS.
The timer will not go down past 00:00:00.
Examples
Basic functionality is very simple:
let now = TimeStamp::now();
let mut counter = Counter::down(
Some(now - 600),
Some(now + 600)
);
// A small amount of time will have passed since `now` was assigned
assert_eq!(counter.to_string(), "00:09:59");
counter.flip();
// It now counts up from `start`
assert_eq!(counter.to_string(), "00:10:00")Both start and end times are adjustable:
let mut counter = Counter::up(Some(TimeStamp::now()), None);
counter.try_move_start(-30).unwrap();
assert_eq!(counter.to_string(), "00:00:30")Fields§
§start: T§end: T§direction: DirectionImplementations§
source§impl<T, D> Counter<T>where
T: Copy + Default + Display + Time<Duration = D> + FromStr + Sub<T, Output = D>,
D: TimeUnits,
impl<T, D> Counter<T>where T: Copy + Default + Display + Time<Duration = D> + FromStr + Sub<T, Output = D>, D: TimeUnits,
sourcepub fn down(start: Option<T>, end: Option<T>) -> Counter<T>
pub fn down(start: Option<T>, end: Option<T>) -> Counter<T>
If given None, the default value for T will be assigned.
sourcepub fn up(start: Option<T>, end: Option<T>) -> Counter<T>
pub fn up(start: Option<T>, end: Option<T>) -> Counter<T>
If given None, the default value for T will be assigned.
sourcepub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
Calls to_string on start, end, and direction, and std::fs::writes each
to one line in a file, in that order.
sourcepub fn from_file<P: AsRef<Path>>(path: P) -> Result<Counter<T>>
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Counter<T>>
Tries converting the first three lines of a file (read by std::fs::read_to_string)
into a Counter by attempting to parse them into start, end, and direction
respectively, calling from_str.
pub fn try_move_start( &mut self, seconds: impl Into<D> ) -> Result<(), TimeOverflow>
pub fn try_move_end( &mut self, seconds: impl Into<D> ) -> Result<(), TimeOverflow>
Trait Implementations§
source§impl<T, D> Display for Counter<T>where
T: Copy + Default + Display + Time<Duration = D> + FromStr + Sub<T, Output = D>,
D: TimeUnits,
impl<T, D> Display for Counter<T>where T: Copy + Default + Display + Time<Duration = D> + FromStr + Sub<T, Output = D>, D: TimeUnits,
Displayed as “HH(+):MM:SS”, not below “00:00:00”
source§impl<T: Ord> Ord for Counter<T>
impl<T: Ord> Ord for Counter<T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl<T: PartialEq> PartialEq<Counter<T>> for Counter<T>
impl<T: PartialEq> PartialEq<Counter<T>> for Counter<T>
source§impl<T: PartialOrd> PartialOrd<Counter<T>> for Counter<T>
impl<T: PartialOrd> PartialOrd<Counter<T>> for Counter<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self and other) and is used by the <=
operator. Read more