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]
fn new() -> DelayQueue<T>
[src]
Creates an empty DelayQueue<T>
.
Examples
Basic usage:
use delay_queue::{Delay, DelayQueue}; let mut queue : DelayQueue<Delay<i32>> = DelayQueue::new();
fn with_capacity(capacity: usize) -> DelayQueue<T>
[src]
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);
fn push(&mut self, item: T)
[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)));
fn pop(&mut self) -> T
[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);
fn try_pop_for(&mut self, timeout: Duration) -> Option<T>
[src]
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"
fn try_pop_until(&mut self, try_until: Instant) -> Option<T>
[src]
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"
fn is_empty(&self) -> bool
[src]
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
impl<T: Debug + Delayed> Debug for DelayQueue<T>
[src]
impl<T: Delayed> Default for DelayQueue<T>
[src]
fn default() -> DelayQueue<T>
[src]
Creates an empty DelayQueue<T>
.
impl<T: Delayed> Clone for DelayQueue<T>
[src]
fn clone(&self) -> DelayQueue<T>
[src]
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();
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more