1pub mod optional;
2pub mod ordered;
3pub mod set;
4pub mod timeout_map;
5pub mod timeout_set;
6
7use crate::common::InnerMap;
8use futures::stream::{FusedStream, FuturesUnordered};
9use futures::{Stream, StreamExt};
10use std::future::Future;
11use std::pin::Pin;
12use std::task::{Context, Poll, Waker};
13
14pub struct FutureMap<K, S> {
15 list: FuturesUnordered<InnerMap<K, S>>,
16 empty: bool,
17 waker: Option<Waker>,
18}
19
20impl<K, T> Default for FutureMap<K, T> {
21 fn default() -> Self {
22 Self::new()
23 }
24}
25
26impl<K, T> FutureMap<K, T> {
27 pub fn new() -> Self {
29 Self {
30 list: FuturesUnordered::new(),
31 empty: true,
32 waker: None,
33 }
34 }
35}
36
37impl<K, T> FutureMap<K, T>
38where
39 K: Clone + PartialEq + Send + Unpin + 'static,
40 T: Future + Send + Unpin + 'static,
41{
42 pub fn insert(&mut self, key: K, fut: T) -> bool {
46 if self.contains_key(&key) {
47 return false;
48 }
49
50 let st = InnerMap::new(key, fut);
51 self.list.push(st);
52
53 if let Some(waker) = self.waker.take() {
54 waker.wake();
55 }
56
57 self.empty = false;
58 true
59 }
60
61 pub fn set_wake_on_success(&mut self, key: &K, wake_on_success: bool) -> bool {
65 self.list
66 .iter_mut()
67 .find(|st| st.key().eq(key))
68 .is_some_and(|st| st.set_wake_on_success(wake_on_success))
69 }
70
71 pub fn iter(&self) -> impl Iterator<Item = (&K, &T)> {
73 self.list.iter().filter_map(|st| st.key_value())
74 }
75
76 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut T)> {
78 self.list.iter_mut().filter_map(|st| st.key_value_mut())
79 }
80
81 pub fn iter_pin(&mut self) -> impl Iterator<Item = (&K, Pin<&mut T>)> {
83 self.list.iter_mut().filter_map(|st| st.key_value_pin())
84 }
85
86 pub fn keys(&self) -> impl Iterator<Item = &K> {
88 self.list.iter().map(|st| st.key())
89 }
90
91 pub fn values(&self) -> impl Iterator<Item = &T> {
93 self.list.iter().filter_map(|st| st.inner())
94 }
95
96 pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T> {
98 self.list.iter_mut().filter_map(|st| st.inner_mut())
99 }
100
101 pub fn contains_key(&self, key: &K) -> bool {
103 self.list.iter().any(|st| st.key().eq(key))
104 }
105
106 pub fn clear(&mut self) {
108 self.list.clear();
109 }
110
111 pub fn get(&self, key: &K) -> Option<&T> {
113 self.list
114 .iter()
115 .find(|st| st.key().eq(key))
116 .and_then(|st| st.inner())
117 }
118
119 pub fn get_mut(&mut self, key: &K) -> Option<&mut T> {
121 self.list
122 .iter_mut()
123 .find(|st| st.key().eq(key))
124 .and_then(|st| st.inner_mut())
125 }
126
127 pub fn get_pinned(&mut self, key: &K) -> Option<Pin<&mut T>> {
129 self.list
130 .iter_mut()
131 .find(|st| st.key().eq(key))
132 .and_then(|st| st.inner_pin())
133 }
134
135 pub fn remove(&mut self, key: &K) -> Option<T> {
137 self.list
138 .iter_mut()
139 .find(|st| st.key().eq(key))
140 .and_then(|st| st.take_inner())
141 }
142
143 pub fn len(&self) -> usize {
145 self.list.iter().filter(|st| st.inner().is_some()).count()
146 }
147
148 pub fn is_empty(&self) -> bool {
150 self.list.is_empty() || self.list.iter().all(|st| st.inner().is_none())
151 }
152}
153
154impl<K, T> FromIterator<(K, T)> for FutureMap<K, T>
155where
156 K: Clone + PartialEq + Send + Unpin + 'static,
157 T: Future + Send + Unpin + 'static,
158{
159 fn from_iter<I: IntoIterator<Item = (K, T)>>(iter: I) -> Self {
160 let mut maps = Self::new();
161 for (key, val) in iter {
162 maps.insert(key, val);
163 }
164 maps
165 }
166}
167
168impl<K, T> Stream for FutureMap<K, T>
169where
170 K: Clone + PartialEq + Send + Unpin + 'static,
171 T: Future + Unpin + Send + 'static,
172{
173 type Item = (K, T::Output);
174
175 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
176 let this = &mut *self;
177 loop {
178 match this.list.poll_next_unpin(cx) {
179 Poll::Ready(Some((key, Some(item)))) => return Poll::Ready(Some((key, item))),
180 Poll::Ready(Some((key, None))) => {
182 this.remove(&key);
183 }
184 Poll::Ready(None) => {
185 if self.empty {
192 self.waker = Some(cx.waker().clone());
193 return Poll::Pending;
194 }
195
196 self.empty = true;
197 return Poll::Ready(None);
198 }
199 Poll::Pending => {
200 self.waker = Some(cx.waker().clone());
202 return Poll::Pending;
203 }
204 }
205 }
206 }
207
208 fn size_hint(&self) -> (usize, Option<usize>) {
209 self.list.size_hint()
210 }
211}
212
213impl<K, T> FusedStream for FutureMap<K, T>
214where
215 K: Clone + PartialEq + Send + Unpin + 'static,
216 T: Future + Unpin + Send + 'static,
217{
218 fn is_terminated(&self) -> bool {
219 self.list.is_terminated()
220 }
221}