libsw

Struct Guard

Source
pub struct Guard<'sw, I: Instant> { /* private fields */ }
Expand description

A running, guarded, stopwatch. When dropped, the stopwatch will automatically stop.

Guards are returned by the StopwatchImpl methods guard and guard_at.

§Examples

let mut sw = Sw::new();
{
    let _guard = sw.guard()?;
    // stopwatch is now running and guarded!
    thread::sleep(Duration::from_millis(100));
    // guard dropped, stopwatch stopped
}
assert!(sw.is_stopped());
assert!(sw.elapsed() >= Duration::from_millis(100));

Implementations§

Source§

impl<'sw, I: Instant> Guard<'sw, I>

Source

pub fn new(sw: &'sw mut StopwatchImpl<I>) -> Result<Self>

Returns a Guard to a running stopwatch.

§Errors

If the stopwatch is stopped, returns GuardNew.

§Examples
use std::time::Instant;
let mut sw = Sw::new();
assert_eq!(Guard::new(&mut sw), Err(Error::GuardNew));

sw.start()?;
assert!(Guard::new(&mut sw).is_ok());
assert!(sw.is_stopped());
Source

pub const fn inner(&self) -> &StopwatchImpl<I>

Returns a reference to the inner StopwatchImpl.

§Examples
let mut sw = Sw::new();
let guard = sw.guard()?;
thread::sleep(Duration::from_millis(100));
assert!(guard.inner().elapsed() >= Duration::from_millis(100));

Trait Implementations§

Source§

impl<'sw, I: Debug + Instant> Debug for Guard<'sw, I>

Source§

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

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

impl<I: Instant> Drop for Guard<'_, I>

Source§

fn drop(&mut self)

Releases the guard, calling stop on the guarded stopwatch.

Source§

impl<'sw, I: Hash + Instant> Hash for Guard<'sw, I>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<I: Instant> PartialEq for Guard<'_, I>

Source§

fn eq(&self, rhs: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<I: Instant> Eq for Guard<'_, I>

Auto Trait Implementations§

§

impl<'sw, I> Freeze for Guard<'sw, I>

§

impl<'sw, I> RefUnwindSafe for Guard<'sw, I>
where I: RefUnwindSafe,

§

impl<'sw, I> Send for Guard<'sw, I>
where I: Send,

§

impl<'sw, I> Sync for Guard<'sw, I>
where I: Sync,

§

impl<'sw, I> Unpin for Guard<'sw, I>

§

impl<'sw, I> !UnwindSafe for Guard<'sw, I>

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, 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.