Struct Timer

Source
pub struct Timer { /* private fields */ }
Expand description

Manages timers and triggers timeouts.

Implementations§

Source§

impl Timer

Source

pub fn new() -> Self

Create a new timer containing no timeouts.

Source

pub fn count(&self) -> usize

Return the number of timeouts being tracked.

Source

pub fn has_timeouts(&self) -> bool

Check whether there are timeouts being tracked.

Source

pub fn set_timeout(&mut self, timeout: Duration, after: Timestamp)

Register a new timeout relative to a certain point in time.

Source

pub fn next_expiring_from(&self, time: impl Into<Timestamp>) -> Option<Duration>

Get the first timeout expiring right at or after certain moment of time. Returns None if there are no timeouts.

use reactor::{Timer, Timestamp};

let mut tm = Timer::new();

let now = Timestamp::now();
tm.set_timeout(Duration::from_secs(16), now);
tm.set_timeout(Duration::from_secs(8), now);
tm.set_timeout(Duration::from_secs(64), now);

let mut now = Timestamp::now();
// We need to wait 8 secs to trigger the next timeout (1).
assert!(tm.next_expiring_from(now) <= Some(Duration::from_secs(8)));

// ... sleep for a sec ...
now += Duration::from_secs(1);

// Now we don't need to wait as long!
assert!(tm.next_expiring_from(now).unwrap() <= Duration::from_secs(7));
Source

pub fn remove_expired_by(&mut self, time: Timestamp) -> usize

Removes timeouts which expire by a certain moment of time (inclusive), returning total number of timeouts which were removed.

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

Returns the “default value” for a type. 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, 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.