Struct fibers_timeout_queue::TimeoutQueue [−][src]
pub struct TimeoutQueue<T> { /* fields omitted */ }
Timeout queue.
This contains items that to be dequeued when the associated timeouts have expired.
Methods
impl<T> TimeoutQueue<T>
[src]
impl<T> TimeoutQueue<T>
pub fn new() -> Self
[src]
pub fn new() -> Self
Makes a new TimeoutQueue
instance.
pub fn push(&mut self, item: T, timeout: Duration)
[src]
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));
pub fn pop(&mut self) -> Option<T>
[src]
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
pub fn filter_pop<F>(&mut self, filter: F) -> Option<T> where
F: Fn(&T) -> bool,
[src]
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));
pub fn len(&self) -> usize
[src]
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);
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
Returns true
if the queue has no items, otherwise false
.
Trait Implementations
impl<T: Debug> Debug for TimeoutQueue<T>
[src]
impl<T: Debug> Debug for TimeoutQueue<T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<T> Default for TimeoutQueue<T>
[src]
impl<T> Default for TimeoutQueue<T>
Auto Trait Implementations
impl<T> Send for TimeoutQueue<T> where
T: Send,
impl<T> Send for TimeoutQueue<T> where
T: Send,
impl<T> !Sync for TimeoutQueue<T>
impl<T> !Sync for TimeoutQueue<T>