Struct DelayHandler

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

An abstration over DelayQueue that allows you to create a delay, with associated data.

Users can add data to the delay-map with insert(). The associated data is removed and returned when delay is timedout by .awaiting on next(). Users can also prematurely remove the delay from the delay-map with remove().

§Examples

  1. Insert 3 numbers into delay-map with 10s delays, print them as they timeout
let mut handler = DelayHandler::default();
// Adds 1, 2, 3 to the delay-map, each with 10s delay
handler.insert(1, Duration::from_secs(10));
handler.insert(2, Duration::from_secs(10));
handler.insert(3, Duration::from_secs(10));

// Expect a delay of ~10s, after which 1, 2, 3 should print to stdout, in quick succession.
while let Some(expired) = handler.next().await {
    println!("{}", expired);
}
  1. Insert 3 numbers into delay-map with different delays, print them as they timeout
let mut handler = DelayHandler::default();
// Adds 1, 2 to the delay-map, with different delays
handler.insert(1, Duration::from_secs(10));
handler.insert(2, Duration::from_secs(5));

// With a delay of ~5s between, the prints should come in the order of 2 and 1.
while let Some(expired) = handler.next().await {
    println!("{}", expired);
}
  1. Insert 3 numbers into delay-map with different delays, remove print as delays are timedout
let mut handler = DelayHandler::default();
// Adds 1, 2, 3 to the delay-map, each with different delays
handler.insert(1, Duration::from_secs(15));
handler.insert(2, Duration::from_secs(5));
handler.insert(3, Duration::from_secs(10));
 
// Remove 3 from the delay-map
handler.remove(&3);

// Prints should be in the order of first 2 and ~10s later 1.
while let Some(expired) = handler.next().await {
    println!("{}", expired);
}

Implementations§

Source§

impl<T> DelayHandler<T>
where T: Eq + Hash + Clone + Display,

Source

pub fn insert(&mut self, item: T, period: Duration) -> bool

Insert new timeout into the map and queue if it doesn’t already exist. If one already exists, don’t .

Source

pub fn remove(&mut self, item: &T) -> bool

Prematurely removes timeout from delay-map, if it didn’t already exist returns false.

Source

pub async fn next(&mut self) -> Option<T>

Remove a key from map if it has timedout and return the name.

Source

pub fn is_empty(&self) -> bool

Check if queue is empty. Could be used as precondition in an async select operation. NOTE: The following example assumes usage of tokio::select

select! {
    ...
    Some(expired) = handler.next(), if !handler.is_empty() => println!("{}", expired)
    ...
}

Trait Implementations§

Source§

impl<T> Default for DelayHandler<T>
where T: Eq + Hash + Clone + Display,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for DelayHandler<T>

§

impl<T> !RefUnwindSafe for DelayHandler<T>

§

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

§

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

§

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

§

impl<T> !UnwindSafe for DelayHandler<T>

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.