Struct DelayQueue

Source
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>

Source

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();
Source

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);
Source

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)));
Source

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);
Source

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"
Source

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"
Source

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>

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Delayed> Debug for DelayQueue<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Delayed> Default for DelayQueue<T>

Source§

fn default() -> DelayQueue<T>

Creates an empty DelayQueue<T>.

Auto Trait Implementations§

§

impl<T> Freeze for DelayQueue<T>

§

impl<T> RefUnwindSafe for DelayQueue<T>

§

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

§

impl<T> Sync for DelayQueue<T>
where T: Send,

§

impl<T> Unpin for DelayQueue<T>

§

impl<T> UnwindSafe for DelayQueue<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.