Struct delay_queue::DelayQueue [] [src]

pub struct DelayQueue<T: Delayed> { /* fields omitted */ }

A concurrent unbounded blocking queue where each item can only be removed when its delay expires.

The queue supports multiple producers and multiple consumers.

Items of the queue must implement the Delayed trait. In most situations you can just use the helper struct Delay to wrap the values to be used by the queue.

If you implement the Delayed trait for your types, keep in mind that the DelayQueue assumes that the Instant until which each item is delayed does not change while that item is in the queue.

Examples

Basic usage:

use delay_queue::{Delay, DelayQueue};
use std::time::{Duration, Instant};

let mut queue = DelayQueue::new();
queue.push(Delay::for_duration("2nd", Duration::from_secs(5)));
queue.push(Delay::until_instant("1st", Instant::now()));

println!("First pop: {}", queue.pop().value);
println!("Second pop: {}", queue.pop().value);
assert!(queue.is_empty());

Methods

impl<T: Delayed> DelayQueue<T>
[src]

[src]

Creates an empty DelayQueue.

[src]

Pushes an item onto the queue.

Examples

Basic usage:

use delay_queue::{Delay, DelayQueue};
use std::time::Duration;

let mut queue = DelayQueue::new();
queue.push(Delay::for_duration("2nd", Duration::from_secs(5)));

[src]

Pops the next item from the queue, blocking if necessary until an item is available and its delay has expired.

Examples

Basic usage:

use delay_queue::{Delay, DelayQueue};
use std::time::{Duration, Instant};

let mut queue = DelayQueue::new();

queue.push(Delay::until_instant("1st", Instant::now()));

// The pop will not block, since the delay has expired.
println!("First pop: {}", queue.pop().value);

queue.push(Delay::for_duration("2nd", Duration::from_secs(5)));

// The pop will block for approximately 5 seconds before returning the item.
println!("Second pop: {}", queue.pop().value);

[src]

Checks if the queue is empty.

Trait Implementations

impl<T: Delayed> Clone for DelayQueue<T>
[src]

[src]

Returns a new DelayQueue that points to the same underlying data.

This is needed to share a queue between different threads.

Examples

Basic usage:

use delay_queue::{Delay, DelayQueue};
use std::time::Duration;
use std::thread;

let mut queue = DelayQueue::new();

queue.push(Delay::for_duration("1st", Duration::from_secs(1)));

let mut cloned_queue = queue.clone();

let handle = thread::spawn(move || {
    println!("First pop: {}", cloned_queue.pop().value);
    println!("Second pop: {}", cloned_queue.pop().value);
});

queue.push(Delay::for_duration("2nd", Duration::from_secs(2)));

handle.join().unwrap();

1.0.0
[src]

Performs copy-assignment from source. Read more