Struct countrs::Counter

source ·
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: Direction

Implementations§

source§

impl<T, D> Counter<T>where T: Copy + Default + Display + Time<Duration = D> + FromStr + Sub<T, Output = D>, D: TimeUnits,

source

pub fn down(start: Option<T>, end: Option<T>) -> Counter<T>

If given None, the default value for T will be assigned.

source

pub fn up(start: Option<T>, end: Option<T>) -> Counter<T>

If given None, the default value for T will be assigned.

source

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.

source

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.

source

pub fn flip(&mut self)

Changes the direction of the Counter between Up/Down.

source

pub fn try_move_start( &mut self, seconds: impl Into<D> ) -> Result<(), TimeOverflow>

source

pub fn try_move_end( &mut self, seconds: impl Into<D> ) -> Result<(), TimeOverflow>

Trait Implementations§

source§

impl<T: Debug> Debug for Counter<T>

source§

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

Formats the value using the given formatter. Read more
source§

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§

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

Formats the value using the given formatter. Read more
source§

impl<T: Ord> Ord for Counter<T>

source§

fn cmp(&self, other: &Counter<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq> PartialEq<Counter<T>> for Counter<T>

source§

fn eq(&self, other: &Counter<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd<Counter<T>> for Counter<T>

source§

fn partial_cmp(&self, other: &Counter<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Eq> Eq for Counter<T>

source§

impl<T> StructuralEq for Counter<T>

source§

impl<T> StructuralPartialEq for Counter<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Counter<T>where T: RefUnwindSafe,

§

impl<T> Send for Counter<T>where T: Send,

§

impl<T> Sync for Counter<T>where T: Sync,

§

impl<T> Unpin for Counter<T>where T: Unpin,

§

impl<T> UnwindSafe for Counter<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.