pub struct StopwatchImpl<I: Instant> { /* private fields */ }Expand description
A stopwatch measures and accumulates elapsed time between starts and stops.
Stopwatches work with any type that implements Instant.
Implementations§
Source§impl<I: Instant> StopwatchImpl<I>
 
impl<I: Instant> StopwatchImpl<I>
Sourcepub const fn new() -> Self
 
pub const fn new() -> Self
Returns a stopped stopwatch with zero elapsed time.
§Examples
let sw = Sw::new();
assert!(sw.is_stopped());
assert_eq!(sw.elapsed(), Duration::ZERO);Sourcepub fn new_started() -> Self
 
pub fn new_started() -> Self
Returns a running stopwatch initialized with zero elapsed time.
§Examples
let sw = Sw::new_started();
assert!(sw.is_running());Sourcepub const fn new_started_at(start: I) -> Self
 
pub const fn new_started_at(start: I) -> Self
Returns a stopwatch initialized with zero elapsed time, started at the given instant.
§Examples
let now = Instant::now();
let sw_1 = Sw::new_started_at(now);
let sw_2 = Sw::new_started_at(now);
// they've both started at the same time
assert_eq!(sw_1, sw_2);
// (and had zero elapsed time when they started)
assert_eq!(sw_1.elapsed_at(now), Duration::ZERO);Sourcepub const fn with_elapsed(elapsed: Duration) -> Self
 
pub const fn with_elapsed(elapsed: Duration) -> Self
Returns a stopped stopwatch with the given elapsed time.
§Examples
let sw = Sw::with_elapsed(Duration::from_secs(1));
assert!(sw.is_stopped());
assert_eq!(sw.elapsed(), Duration::from_secs(1));Sourcepub fn with_elapsed_started(elapsed: Duration) -> Self
 
pub fn with_elapsed_started(elapsed: Duration) -> Self
Returns a running stopwatch initialized with the given elapsed time.
§Examples
let sw = Sw::with_elapsed_started(Duration::from_secs(1));
assert!(sw.is_running());
assert!(sw.elapsed() >= Duration::from_secs(1));Sourcepub const fn from_raw(elapsed: Duration, start: Option<I>) -> Self
 
pub const fn from_raw(elapsed: Duration, start: Option<I>) -> Self
Returns a stopwatch from its raw parts.
Internally, a StopwatchImpl combines a saved elapsed time and an
instant which records the latest start time.
While the start time is Some, the stopwatch is running. When it
stops, the time which has elapsed since the start time is added to the
elapsed time, and the start time is set to None.
§Examples
let sw = Sw::from_raw(Duration::from_secs(1), None);
assert!(sw.is_stopped());
assert_eq!(sw.elapsed(), Duration::from_secs(1));§Notes
It is possible to craft two stopwatches whose internal components
differ, but are equal according to PartialEq, Eq, and Hash.
let mut elapsed = Duration::from_secs(10);
let mut start = Instant::now();
let sw_1 = Sw::from_raw(elapsed, Some(start));
let sw_2 = Sw::from_raw(
    elapsed - Duration::from_secs(1),     // now `elapsed()` is 1s less
    Some(start - Duration::from_secs(1)), // now with start pushed back, `elapsed()` is equal
);
// different components, but they are equal!
assert_eq!(sw_1, sw_2);Sourcepub const fn from_core(core_sw: CoreSw<I>) -> Self
 
pub const fn from_core(core_sw: CoreSw<I>) -> Self
Constructs a StopwatchImpl from a libsw_core::Stopwatch.
Sourcepub const fn to_core(self) -> CoreSw<I>
 
pub const fn to_core(self) -> CoreSw<I>
Returns a libsw_core::Stopwatch with the same elapsed time and start.
Sourcepub const fn is_running(&self) -> bool
 
pub const fn is_running(&self) -> bool
Returns true if the stopwatch is running.
§Examples
let sw = Sw::new_started();
assert!(sw.is_running());Sourcepub const fn is_stopped(&self) -> bool
 
pub const fn is_stopped(&self) -> bool
Sourcepub fn elapsed(&self) -> Duration
 
pub fn elapsed(&self) -> Duration
Returns the total time elapsed. If overflow occurs, the elapsed time is
saturated to Duration::MAX.
§Examples
let sw = Sw::new_started();
thread::sleep(Duration::from_millis(100));
assert!(sw.elapsed() >= Duration::from_millis(100));Sourcepub fn elapsed_at(&self, anchor: I) -> Duration
 
pub fn elapsed_at(&self, anchor: I) -> Duration
Returns the total time elapsed, measured as if the current time were
anchor. If overflow occurs, the elapsed time is saturated to
Duration::MAX.
§Notes
anchor saturates to the last instant the stopwatch was started.
§Examples
let sw_1 = Sw::new_started();
let sw_2 = sw_1.clone();
let anchor = Instant::now();
assert!(sw_1.elapsed_at(anchor) == sw_2.elapsed_at(anchor));Sourcepub fn checked_elapsed(&self) -> Option<Duration>
 
pub fn checked_elapsed(&self) -> Option<Duration>
Sourcepub fn checked_elapsed_at(&self, anchor: I) -> Option<Duration>
 
pub fn checked_elapsed_at(&self, anchor: I) -> Option<Duration>
Computes the total time elapsed, measured as if the current time were
anchor. If overflow occurred, returns None.
§Notes
anchor saturates to the last instant the stopwatch was started.
§Examples
See the documentation for checked_elapsed for
a related example.
Sourcepub fn stop(&mut self) -> Result<()>
 
pub fn stop(&mut self) -> Result<()>
Stops measuring the time elapsed since the last start.
§Errors
Returns SwStop if the stopwatch is already stopped.
§Notes
Overflows of the new elapsed time are saturated to Duration::MAX.
Use StopwatchImpl::checked_stop to explicitly check for overflow.
§Examples
let mut sw = Sw::new_started();
assert!(sw.stop().is_ok());
assert!(sw.stop().is_err());
let then = sw.elapsed();
thread::sleep(Duration::from_millis(100));
let now = sw.elapsed();
assert!(then == now);Sourcepub fn start_at(&mut self, anchor: I) -> Result<()>
 
pub fn start_at(&mut self, anchor: I) -> Result<()>
Starts measuring the time elapsed as if the current time were anchor.
§Errors
Returns SwStart if the stopwatch is running.
§Notes
If anchor is in the future, elapsed will return
Duration::ZERO until the current time catches up to it.
§Examples
let mut sw_1 = Sw::new();
let mut sw_2 = Sw::new();
let start = Instant::now();
// off to the races! at the same time!
sw_1.start_at(start)?;
sw_2.start_at(start)?;
thread::sleep(Duration::from_millis(100));
let anchor = Instant::now();
assert_eq!(sw_1.elapsed_at(anchor), sw_2.elapsed_at(anchor)); // 'twas a tie
assert!(sw_1.elapsed_at(anchor) >= Duration::from_millis(100));Sourcepub fn stop_at(&mut self, anchor: I) -> Result<()>
 
pub fn stop_at(&mut self, anchor: I) -> Result<()>
Stops measuring the time elapsed since the last start as if the current
time were anchor.
§Errors
Returns SwStop if the stopwatch is already stopped.
§Notes
- 
If
anchoris earlier than the last start, there is no effect on the elapsed time. - 
Overflows of the new elapsed time are saturated to
Duration::MAX. UseStopwatchImpl::checked_stop_atto explicitly check for overflow. 
§Examples
let mut sw_1 = Sw::new_started();
let mut sw_2 = sw_1.clone();
let stop = Instant::now();
sw_1.stop_at(stop)?;
sw_2.stop_at(stop)?;
assert_eq!(sw_1, sw_2);Sourcepub fn checked_stop(&mut self) -> Result<Option<()>>
 
pub fn checked_stop(&mut self) -> Result<Option<()>>
Tries to stop the stopwatch. If the new elapsed time overflows, returns
None without mutating the stopwatch.
§Errors
Returns SwStop if the stopwatch is already stopped.
§Examples
let mut sw = Sw::new_started();
assert!(sw.checked_stop()?.is_some());
sw.set(Duration::MAX);
sw.start()?;
assert!(sw.checked_stop()?.is_none());Sourcepub fn checked_stop_at(&mut self, anchor: I) -> Result<Option<()>>
 
pub fn checked_stop_at(&mut self, anchor: I) -> Result<Option<()>>
Tries to stop the stopwatch, as if the current time were anchor. If
the new elapsed time overflows, returns None without mutating the
stopwatch.
§Errors
Returns SwStop if the stopwatch is already stopped.
§Notes
If anchor is earlier than the last start, there is no effect on the
elapsed time.
§Examples
See StopwatchImpl::checked_stop for comparable example usage.
Sourcepub fn toggle_at(&mut self, anchor: I)
 
pub fn toggle_at(&mut self, anchor: I)
Toggles whether the stopwatch is running or stopped, as if the current
time were anchor.
§Notes
See start_at and stop_at for
notes about the chronology of anchor, as well as what happens if
overflow occurs.
§Examples
let mut left = Sw::new();
let mut right = Sw::new_started();
// perfect swap of left and right running
let now = Instant::now();
left.toggle_at(now);
right.toggle_at(now);
assert!(left.is_running());
assert!(right.is_stopped());Sourcepub fn checked_toggle(&mut self) -> Option<()>
 
pub fn checked_toggle(&mut self) -> Option<()>
Tries to toggle whether the stopwatch is running or stopped. If the new
elapsed time overflows, returns None without mutating the stopwatch.
§Examples
let mut sw = Sw::with_elapsed_started(Duration::MAX);
thread::sleep(Duration::from_millis(100));
// whoops, new elapsed time can't be Duration::MAX + 100ms
assert!(sw.checked_toggle().is_none());Sourcepub fn checked_toggle_at(&mut self, anchor: I) -> Option<()>
 
pub fn checked_toggle_at(&mut self, anchor: I) -> Option<()>
Tries to toggle whether the stopwatch is running or stopped, as if the
current time were anchor. If the new elapsed time overflows, returns
None without mutating the stopwatch.
§Examples
See the documentation for checked_toggle for a
related example.
Sourcepub fn guard(&mut self) -> Result<Guard<'_, I>>
 
pub fn guard(&mut self) -> Result<Guard<'_, I>>
Starts the stopwatch, returning a Guard which when dropped, will
stop the stopwatch.
§Errors
Returns SwGuard if the stopwatch is running.
§Examples
For examples on how to use Guards, see the struct
documentation.
Sourcepub fn reset(&mut self)
 
pub fn reset(&mut self)
Stops and resets the elapsed time to zero.
§Examples
let mut sw = Sw::with_elapsed_started(Duration::from_secs(1));
sw.reset();
assert_eq!(sw, Sw::new());Sourcepub fn reset_in_place(&mut self)
 
pub fn reset_in_place(&mut self)
Resets the elapsed time to zero without affecting whether the stopwatch is running.
§Examples
let mut sw = Sw::with_elapsed_started(Duration::from_secs(1));
sw.reset_in_place();
assert!(sw.is_running());
// new elapsed time is close to zero
assert!(sw.elapsed() < Duration::from_millis(1));
sw.stop()?;
sw.reset_in_place();
assert_eq!(sw, Sw::new());Sourcepub fn reset_in_place_at(&mut self, start: I)
 
pub fn reset_in_place_at(&mut self, start: I)
Resets the elapsed time to zero without affecting whether the stopwatch is running.
§Notes
See start_at for notes about the chronology of
anchor.
§Examples
See the documentation for reset_in_place for a
related example.
Sourcepub fn set(&mut self, new: Duration)
 
pub fn set(&mut self, new: Duration)
Stops and sets the total elapsed time to new.
§Examples
let mut sw = Sw::new();
sw.set(Duration::from_secs(1));
assert_eq!(sw.elapsed(), Duration::from_secs(1));Sourcepub fn set_in_place(&mut self, new: Duration)
 
pub fn set_in_place(&mut self, new: Duration)
Sets the total elapsed time to new without affecting whether the
stopwatch is running.
§Examples
let mut sw = Sw::new();
sw.set_in_place(Duration::from_secs(1));
assert_eq!(sw.elapsed(), Duration::from_secs(1));
assert!(sw.is_stopped());
sw.start()?;
sw.set_in_place(Duration::from_secs(2));
assert!(sw.elapsed() >= Duration::from_secs(2));
assert!(sw.is_running());Sourcepub fn set_in_place_at(&mut self, new: Duration, anchor: I)
 
pub fn set_in_place_at(&mut self, new: Duration, anchor: I)
Sets the total elapsed time to new as if the current time were
anchor, and without affecting whether the stopwatch is running.
§Notes
See start_at for notes about the chronology of
anchor.
§Examples
See the documentation for set_in_place for
a related example.
Sourcepub fn replace(&mut self, new: Duration) -> Duration
 
pub fn replace(&mut self, new: Duration) -> Duration
Stops and sets the total elapsed time to new, returning the previous
elapsed time.
§Examples
let mut sw = Sw::with_elapsed(Duration::from_secs(3));
let previous = sw.replace(Duration::from_secs(1));
assert_eq!(previous, Duration::from_secs(3));
assert_eq!(sw.elapsed(), Duration::from_secs(1));Sourcepub fn replace_at(&mut self, new: Duration, anchor: I) -> Duration
 
pub fn replace_at(&mut self, new: Duration, anchor: I) -> Duration
Stops and sets the total elapsed time to new, returning the previous
elapsed time as if the current time were anchor.
§Notes
See elapsed_at for notes about the chronology of
anchor.
§Examples
See the documentation for replace for a related
example.
Sourcepub const fn saturating_add(self, dur: Duration) -> Self
 
pub const fn saturating_add(self, dur: Duration) -> Self
Adds dur to the total elapsed time. If overflow occurred, the total
elapsed time is set to Duration::MAX.
let mut sw = Sw::with_elapsed(Duration::from_secs(1));
sw = sw.saturating_add(Duration::from_secs(1));
assert_eq!(sw.elapsed(), Duration::from_secs(2));
sw = sw.saturating_add(Duration::MAX);
assert_eq!(sw.elapsed(), Duration::MAX);Sourcepub fn saturating_sub(self, dur: Duration) -> Self
 
pub fn saturating_sub(self, dur: Duration) -> Self
Subtracts dur from the total elapsed time. If underflow occurred, the
total elapsed time is set to Duration::ZERO.
§Notes
See the documentation for saturating_sub_at
for notes about positive overflow.
§Examples
let mut sw = Sw::with_elapsed(Duration::from_secs(1));
sw = sw.saturating_sub(Duration::from_secs(1));
assert_eq!(sw.elapsed(), Duration::ZERO);
sw = sw.saturating_sub(Duration::from_secs(1));
assert_eq!(sw.elapsed(), Duration::ZERO);Sourcepub fn saturating_sub_at(self, dur: Duration, anchor: I) -> Self
 
pub fn saturating_sub_at(self, dur: Duration, anchor: I) -> Self
Subtracts dur from the total elapsed time, as if the current time were
anchor. If underflow occurred, the total elapsed time is set to
Duration::ZERO.
§Notes
- 
If the elapsed time is overflowing (as in, would exceed
Duration::MAX), the elapsed time is clamped toDuration::MAXandduris subtracted from that. - 
anchorsaturates to the last instant the stopwatch was started. 
§Examples
let mut sw = Sw::new_started();
thread::sleep(Duration::from_millis(100));
let mut now = Instant::now();
sw = sw.saturating_sub_at(Duration::from_secs(1), now);
assert_eq!(sw.elapsed_at(now), Duration::ZERO);Sourcepub const fn checked_add(self, dur: Duration) -> Option<Self>
 
pub const fn checked_add(self, dur: Duration) -> Option<Self>
Sourcepub fn checked_sub(self, dur: Duration) -> Option<Self>
 
pub fn checked_sub(self, dur: Duration) -> Option<Self>
Subtracts dur from the total elapsed time. If overflow occurred,
returns None.
§Notes
See the documentation for checked_sub_at for
notes about positive overflow.
§Examples
let mut sw = Sw::new();
assert_eq!(sw.checked_sub(Duration::from_secs(1)), None);
sw += Duration::from_secs(1);
assert_eq!(
    sw.checked_sub(Duration::from_secs(1)),
    Some(Sw::with_elapsed(Duration::ZERO)),
);Sourcepub fn checked_sub_at(self, dur: Duration, anchor: I) -> Option<Self>
 
pub fn checked_sub_at(self, dur: Duration, anchor: I) -> Option<Self>
Subtracts dur from the total elapsed time, as if the current time were
anchor. If overflow occurred, returns None.
§Notes
- 
Overflow can also occur if the elapsed time is overflowing (as in, would exceed
Duration::MAX). - 
anchorsaturates to the last instant the stopwatch was started. 
§Examples
let mut sw = Sw::new_started();
thread::sleep(Duration::from_millis(100));
let now = Instant::now();
// underflow yields `None`
assert_eq!(sw.checked_sub_at(Duration::from_secs(1), now), None);
// positive overflow yields `None`
sw.set_in_place(Duration::MAX);
assert_eq!(sw.checked_sub(Duration::ZERO), None);Trait Implementations§
Source§impl<I: Instant> Add<Duration> for StopwatchImpl<I>
 
impl<I: Instant> Add<Duration> for StopwatchImpl<I>
Source§fn add(self, dur: Duration) -> Self::Output
 
fn add(self, dur: Duration) -> Self::Output
Add dur to self.
Currently this is an alias to StopwatchImpl::checked_add, but that
is not a stable guarentee. If you need a guarentee on the
implementation, use the checked or
saturating methods explicitly.
§Panics
Panics if overflow occurs.
Source§type Output = StopwatchImpl<I>
 
type Output = StopwatchImpl<I>
+ operator.Source§impl<I: Instant> AddAssign<Duration> for StopwatchImpl<I>
 
impl<I: Instant> AddAssign<Duration> for StopwatchImpl<I>
Source§fn add_assign(&mut self, dur: Duration)
 
fn add_assign(&mut self, dur: Duration)
+= operation. Read moreSource§impl<I: Clone + Instant> Clone for StopwatchImpl<I>
 
impl<I: Clone + Instant> Clone for StopwatchImpl<I>
Source§fn clone(&self) -> StopwatchImpl<I>
 
fn clone(&self) -> StopwatchImpl<I>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
 
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<I: Instant> Debug for StopwatchImpl<I>
 
impl<I: Instant> Debug for StopwatchImpl<I>
Source§impl<I: Instant> Default for StopwatchImpl<I>
 
impl<I: Instant> Default for StopwatchImpl<I>
Source§fn default() -> Self
 
fn default() -> Self
Returns the default stopwatch. Same as calling StopwatchImpl::new.
Source§impl<I: Instant> From<StopwatchImpl<I>> for Stopwatch<I>
 
impl<I: Instant> From<StopwatchImpl<I>> for Stopwatch<I>
Source§fn from(val: StopwatchImpl<I>) -> Self
 
fn from(val: StopwatchImpl<I>) -> Self
Source§impl<I: Instant + Hash> Hash for StopwatchImpl<I>
 
impl<I: Instant + Hash> Hash for StopwatchImpl<I>
Source§impl<I: Instant> PartialEq for StopwatchImpl<I>
 
impl<I: Instant> PartialEq for StopwatchImpl<I>
Source§impl<I: Instant> Sub<Duration> for StopwatchImpl<I>
 
impl<I: Instant> Sub<Duration> for StopwatchImpl<I>
Source§fn sub(self, dur: Duration) -> Self::Output
 
fn sub(self, dur: Duration) -> Self::Output
Subtract dur from self.
Currently this is an alias to StopwatchImpl::checked_sub, but that
is not a stable guarentee. If you need a guarentee on the
implementation, use the checked or
saturating methods explicitly.
§Panics
Panics if overflow occurs.
Source§type Output = StopwatchImpl<I>
 
type Output = StopwatchImpl<I>
- operator.Source§impl<I: Instant> SubAssign<Duration> for StopwatchImpl<I>
 
impl<I: Instant> SubAssign<Duration> for StopwatchImpl<I>
Source§fn sub_assign(&mut self, dur: Duration)
 
fn sub_assign(&mut self, dur: Duration)
-= operation. Read more