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>
impl<T> TimeoutQueue<T>
Sourcepub fn push(&mut self, item: T, timeout: Duration)
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));Sourcepub fn pop(&mut self) -> Option<T>
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 itemSourcepub fn filter_pop<F>(&mut self, filter: F) -> Option<T>
pub fn filter_pop<F>(&mut self, filter: F) -> Option<T>
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));Sourcepub fn len(&self) -> usize
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);Trait Implementations§
Source§impl<T: Debug> Debug for TimeoutQueue<T>
impl<T: Debug> Debug for TimeoutQueue<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more