Struct TimeWheel

Source
pub struct TimeWheel<T> { /* private fields */ }
Expand description

A binary-heap based timing wheel implementation.

Implementations§

Source§

impl<T> TimeWheel<T>

Source

pub fn new(tick_interval: Duration) -> Self

Create a time-wheel with minimum time interval resolution tick_interval.

Source

pub fn len(&self) -> usize

Returns the number of alive timers.

Source

pub fn deadline(&mut self, deadline: Instant, value: T) -> Option<u64>

Create a new timer using provided deadline.

Return None if the deadline is already reach.

§Examples
use std::time::{ Duration, Instant };
use timing_wheel::TimeWheel;
use std::thread::sleep;

let mut time_wheel = TimeWheel::new(Duration::from_millis(1));

time_wheel.deadline(Instant::now() + Duration::from_millis(1), ());

sleep(Duration::from_millis(2));

let mut wakers = vec![];

time_wheel.spin(&mut wakers);

assert_eq!(wakers.into_iter().map(|v| v.1).collect::<Vec<_>>(), vec![()]);
Source

pub fn after(&mut self, duration: Duration, value: T) -> Option<u64>

Create a new deadline with a value equal to Instant::now() + duration.

§Examples
use std::time::Duration;
use timing_wheel::TimeWheel;
use std::thread::sleep;

let mut time_wheel = TimeWheel::new(Duration::from_millis(1));

time_wheel.after(Duration::from_millis(1), ());

sleep(Duration::from_millis(2));

let mut wakers = vec![];

time_wheel.spin(&mut wakers);

assert_eq!(wakers.into_iter().map(|v| v.1).collect::<Vec<_>>(), vec![()]);
Source

pub fn spin(&mut self, wakers: &mut Vec<(u64, T)>)

Spin the wheel according to the current time and detect(returns) the expiry timers.

Auto Trait Implementations§

§

impl<T> Freeze for TimeWheel<T>

§

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

§

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

§

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

§

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

§

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

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.