pub struct DelayQueue<T: Delayed> { /* private fields */ }Expand description
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());Implementations§
Source§impl<T: Delayed> DelayQueue<T>
impl<T: Delayed> DelayQueue<T>
Sourcepub fn new() -> DelayQueue<T>
pub fn new() -> DelayQueue<T>
Creates an empty DelayQueue<T>.
§Examples
Basic usage:
use delay_queue::{Delay, DelayQueue};
let mut queue : DelayQueue<Delay<i32>> = DelayQueue::new();Sourcepub fn with_capacity(capacity: usize) -> DelayQueue<T>
pub fn with_capacity(capacity: usize) -> DelayQueue<T>
Creates an empty DelayQueue<T> with a specific capacity.
This preallocates enough memory for capacity elements,
so that the DelayQueue does not have to be reallocated
until it contains at least that many values.
§Examples
Basic usage:
use delay_queue::{Delay, DelayQueue};
let mut queue : DelayQueue<Delay<&str>> = DelayQueue::with_capacity(10);Sourcepub fn push(&mut self, item: T)
pub fn push(&mut self, item: T)
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)));Sourcepub fn pop(&mut self) -> T
pub fn pop(&mut self) -> T
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);Sourcepub fn try_pop_for(&mut self, timeout: Duration) -> Option<T>
pub fn try_pop_for(&mut self, timeout: Duration) -> Option<T>
Pops the next item from the queue, blocking if necessary until an item is available and its delay has expired or until the given timeout expires.
Returns None if the given timeout expires and no item became available to be popped.
§Examples
Basic usage:
use delay_queue::{Delay, DelayQueue};
use std::time::Duration;
let mut queue = DelayQueue::new();
queue.push(Delay::for_duration("1st", Duration::from_secs(5)));
// The pop will block for approximately 2 seconds before returning None.
println!("First pop: {:?}",
queue.try_pop_for(Duration::from_secs(2))); // Prints "None"
// The pop will block for approximately 3 seconds before returning the item.
println!("Second pop: {}",
queue.try_pop_for(Duration::from_secs(5)).unwrap().value); // Prints "1st"Sourcepub fn try_pop_until(&mut self, try_until: Instant) -> Option<T>
pub fn try_pop_until(&mut self, try_until: Instant) -> Option<T>
Pops the next item from the queue, blocking if necessary until an item is available and its
delay has expired or until the given Instant is reached.
Returns None if the given Instant is reached and no item became available to be popped.
§Examples
Basic usage:
use delay_queue::{Delay, DelayQueue};
use std::time::{Duration, Instant};
let mut queue = DelayQueue::new();
queue.push(Delay::for_duration("1st", Duration::from_secs(5)));
// The pop will block for approximately 2 seconds before returning None.
println!("First pop: {:?}",
queue.try_pop_until(Instant::now() + Duration::from_secs(2))); // Prints "None"
// The pop will block for approximately 3 seconds before returning the item.
println!("Second pop: {}",
queue.try_pop_until(Instant::now() + Duration::from_secs(5))
.unwrap().value); // Prints "1st"Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the queue is empty.
§Examples
Basic usage:
use delay_queue::{Delay, DelayQueue};
use std::time::Instant;
let mut queue = DelayQueue::new();
queue.push(Delay::until_instant("val", Instant::now()));
assert!(!queue.is_empty());
println!("First pop: {}", queue.pop().value);
assert!(queue.is_empty());Trait Implementations§
Source§impl<T: Delayed> Clone for DelayQueue<T>
impl<T: Delayed> Clone for DelayQueue<T>
Source§fn clone(&self) -> DelayQueue<T>
fn clone(&self) -> DelayQueue<T>
Returns a new DelayQueue that points to the same underlying data.
This method can be used 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 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Delayed> Default for DelayQueue<T>
impl<T: Delayed> Default for DelayQueue<T>
Source§fn default() -> DelayQueue<T>
fn default() -> DelayQueue<T>
Creates an empty DelayQueue<T>.