Struct reactor::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 timeout manager.

Takes a threshold below which two timeouts cannot overlap.

source

pub fn len(&self) -> usize

Return the number of timeouts being tracked.

source

pub fn is_empty(&self) -> bool

Check whether there are timeouts being tracked.

source

pub fn set_timer(&mut self, span: Duration, after: Timestamp)

Register a new timeout with an associated key and wake-up time from a UNIX time epoch.

source

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

Get the minimum time duration we should wait for at least one timeout to be reached. Returns None if there are no timeouts.

use reactor::{Timer, Timestamp};

let mut tm = Timer::new();

let now = Timestamp::now();
tm.set_timer(Duration::from_secs(16), now);
tm.set_timer(Duration::from_secs(8), now);
tm.set_timer(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(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(now).unwrap() <= Duration::from_secs(7));
source

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

Returns vector of timers which has fired before certain time.

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