Struct TimeoutQueue

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

Timeout queue.

This contains items that to be dequeued when the associated timeouts have expired.

Implementations§

Source§

impl<T> TimeoutQueue<T>

Source

pub fn new() -> Self

Makes a new TimeoutQueue instance.

Source

pub fn push(&mut self, item: T, timeout: Duration)

Enqueues the given item to the queue.

The item will be dequeued by calling pop method after the specified timeout has expired.

§Examples
use fibers_timeout_queue::TimeoutQueue;
use std::time::Duration;
use std::thread;

let mut queue = TimeoutQueue::new();
queue.push(3, Duration::from_millis(5));

assert_eq!(queue.pop(), None);
thread::sleep(Duration::from_millis(10));
assert_eq!(queue.pop(), Some(3));
Source

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

Tries dequeuing an item of which timeout has expired.

If there are no such items, this will return None.

§Examples
use fibers_timeout_queue::TimeoutQueue;
use std::time::Duration;
use std::thread;

let mut queue = TimeoutQueue::new();
assert_eq!(queue.pop(), None); // `queue` is empty

queue.push(3, Duration::from_millis(5));
assert_eq!(queue.pop(), None); // No expired items

thread::sleep(Duration::from_millis(10));
assert_eq!(queue.pop(), Some(3)); // There is an expired item
Source

pub fn filter_pop<F>(&mut self, filter: F) -> Option<T>
where F: Fn(&T) -> bool,

A variant of pop method that filters items located in the queue’s prefix.

If the invocation of filter(item) returns false, the item will be discarded.

This method is very efficient when there are many items that become unconscious before the timeout expires.

§Examples
use fibers_timeout_queue::TimeoutQueue;
use std::time::Duration;
use std::thread;

let mut queue = TimeoutQueue::new();
for i in 0..10 {
    queue.push(i, Duration::from_millis(i));
}
assert_eq!(queue.filter_pop(|&n| n > 5), None);

thread::sleep(Duration::from_millis(10));
assert_eq!(queue.pop(), Some(6));
Source

pub fn len(&self) -> usize

Returns the number of items holded in the queue.

§Examples
use fibers_timeout_queue::TimeoutQueue;
use std::time::Duration;

let mut queue = TimeoutQueue::new();
queue.push(1, Duration::from_millis(100));
queue.push(2, Duration::from_millis(20));
assert_eq!(queue.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the queue has no items, otherwise false.

Trait Implementations§

Source§

impl<T: Debug> Debug for TimeoutQueue<T>

Source§

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

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

impl<T> Default for TimeoutQueue<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for TimeoutQueue<T>

§

impl<T> RefUnwindSafe for TimeoutQueue<T>
where T: RefUnwindSafe,

§

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

§

impl<T> !Sync for TimeoutQueue<T>

§

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

§

impl<T> UnwindSafe for TimeoutQueue<T>
where T: UnwindSafe,

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.