time: add DelayQueue::peek (#5569)

Signed-off-by: Jens Reidel <[email protected]>
This commit is contained in:
Jens Reidel
2023-04-16 09:43:03 +02:00
committed by GitHub
parent 3b16564ce0
commit effead29d1
5 changed files with 94 additions and 0 deletions
+39
View File
@@ -874,6 +874,41 @@ impl<T> DelayQueue<T> {
self.slab.compact();
}
/// Gets the [`Key`] that [`poll_expired`] will pull out of the queue next, without
/// pulling it out or waiting for the deadline to expire.
///
/// Entries that have already expired may be returned in any order, but it is
/// guaranteed that this method returns them in the same order as when items
/// are popped from the `DelayQueue`.
///
/// # Examples
///
/// Basic usage
///
/// ```rust
/// use tokio_util::time::DelayQueue;
/// use std::time::Duration;
///
/// # #[tokio::main]
/// # async fn main() {
/// let mut delay_queue = DelayQueue::new();
///
/// let key1 = delay_queue.insert("foo", Duration::from_secs(10));
/// let key2 = delay_queue.insert("bar", Duration::from_secs(5));
/// let key3 = delay_queue.insert("baz", Duration::from_secs(15));
///
/// assert_eq!(delay_queue.peek().unwrap(), key2);
/// # }
/// ```
///
/// [`Key`]: struct@Key
/// [`poll_expired`]: method@Self::poll_expired
pub fn peek(&self) -> Option<Key> {
use self::wheel::Stack;
self.expired.peek().or_else(|| self.wheel.peek())
}
/// Returns the next time to poll as determined by the wheel
fn next_deadline(&mut self) -> Option<Instant> {
self.wheel
@@ -1166,6 +1201,10 @@ impl<T> wheel::Stack for Stack<T> {
}
}
fn peek(&self) -> Option<Self::Owned> {
self.head
}
#[track_caller]
fn remove(&mut self, item: &Self::Borrowed, store: &mut Self::Store) {
let key = *item;