pollable_map/futures/
timeout_set.rs1use crate::common::Timed;
2use crate::futures::set::FutureSet;
3use futures::{Stream, StreamExt};
4use std::future::Future;
5use std::ops::{Deref, DerefMut};
6use std::pin::Pin;
7use std::task::{Context, Poll};
8use std::time::Duration;
9
10pub struct TimeoutFutureSet<S> {
11 duration: Duration,
12 set: FutureSet<Timed<S>>,
13}
14
15impl<S> Deref for TimeoutFutureSet<S> {
16 type Target = FutureSet<Timed<S>>;
17 fn deref(&self) -> &Self::Target {
18 &self.set
19 }
20}
21
22impl<S> DerefMut for TimeoutFutureSet<S> {
23 fn deref_mut(&mut self) -> &mut Self::Target {
24 &mut self.set
25 }
26}
27
28impl<F> TimeoutFutureSet<F>
29where
30 F: Future + Send + Unpin + 'static,
31{
32 pub fn new(duration: Duration) -> Self {
34 Self {
35 duration,
36 set: FutureSet::new(),
37 }
38 }
39
40 pub fn insert(&mut self, future: F) -> bool {
42 self.set.insert(Timed::new(future, self.duration))
43 }
44}
45
46impl<F> Stream for TimeoutFutureSet<F>
47where
48 F: Future + Send + Unpin + 'static,
49{
50 type Item = std::io::Result<F::Output>;
51 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
52 self.set.poll_next_unpin(cx)
53 }
54
55 fn size_hint(&self) -> (usize, Option<usize>) {
56 self.set.size_hint()
57 }
58}