mapped_futures 0.2.1

A collection of futures based on FuturesUnordered that supports insertion, removal and mutation of futures by key
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! An unbounded map of streams

use super::task::Task;
use super::{FutMut, MappedFutures};

use core::fmt::{self, Debug};
use core::hash::Hash;
use core::pin::Pin;
use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;
use std::ops::{Deref, DerefMut};

use futures_core::ready;
use futures_core::stream::{FusedStream, Stream};
use futures_core::task::{Context, Poll};

use futures_util::stream::{StreamExt, StreamFuture};

/// An unbounded bimultimap of streams
///
/// This "combinator" provides the ability to maintain a map of streams
/// and drive them all to completion.
///
/// Streams are inserted into this map with keys and their realized values are
/// yielded as they become ready. Streams will only be polled when they
/// generate notifications. This allows to coordinate a large number of streams.
///
/// You can start with an empty map with the `BiMultiMappedStreams::new` constructor or
/// you can collect from an iterator whose items are (leftkey, rightkey , stream) triples.
#[must_use = "streams do nothing unless polled"]
pub struct MappedStreams<K: Hash + Eq, St, S = RandomState>
where
    S: BuildHasher,
{
    pub(super) inner: MappedFutures<K, StreamFuture<St>, S>,
}

impl<K: Hash + Eq + Clone, St: Debug> Debug for MappedStreams<K, St> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "MappedStreams {{ ... }}")
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin> MappedStreams<K, St, RandomState> {
    /// Constructs a new, empty `MappedStreams`
    ///
    /// The returned `MappedStreams` does not contain any streams and, in this
    /// state, `MappedStreams::poll` will return `Poll::Ready(None)`.
    pub fn new() -> Self {
        Self {
            inner: MappedFutures::new(),
        }
    }
}

impl<K: Hash + Eq + Clone, St: Stream, S: BuildHasher> MappedStreams<K, St, S> {
    /// Constructs a new, empty `MappedStreams` that maps using the specified hasher.
    ///
    /// The returned `MappedStreams` does not contain any streams and, in this
    /// state, `MappedStreams::poll` will return `Poll::Ready(None)`.
    pub fn with_hasher(hasher: S) -> Self {
        Self {
            inner: MappedFutures::with_hasher(hasher),
        }
    }

    // Gets the BuildHasher associated with these MappedStreams.
    pub fn hasher(&self) -> &S {
        self.inner.hasher()
    }

    /// Returns the number of streams contained in the map.
    ///
    /// This represents the total number of in-flight streams.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns `true` if the map contains no streams
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Remove a stream from the set, dropping it.
    ///
    /// Returns true if a stream was cancelled.
    pub fn cancel(&mut self, key: &K) -> bool {
        self.inner.cancel(key)
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin, S: BuildHasher> MappedStreams<K, St, S> {
    /// Returns an iterator that allows inspecting each stream in the map.
    pub fn iter(&self) -> Iter<'_, K, St, S> {
        Iter(self.inner.iter())
    }

    /// Returns an iterator that allows modifying each stream in the map.
    pub fn iter_mut(&mut self) -> IterMut<'_, K, St, S> {
        IterMut(self.inner.iter_mut())
    }

    /// Clears the map, removing all streams.
    pub fn clear(&mut self) {
        self.inner.clear()
    }

    /// Remove a stream from the map and return it.
    pub fn remove(&mut self, key: &K) -> Option<St> {
        if let Some(st_fut) = self.inner.remove(key) {
            return st_fut.into_inner();
        }
        None
    }

    /// Get a mutable reference to the mapped stream.
    pub fn get_mut<'a>(&'a mut self, key: &K) -> Option<StMut<'a, K, St>> {
        if let Some(st_fut) = self.inner.get_mut(key) {
            return Some(StMut { inner: st_fut });
        }
        None
    }

    /// Get a shared reference to the mapped stream.
    pub fn get<'a>(&mut self, key: &K) -> Option<&'a St> {
        if let Some(st_fut) = self.inner.get(key) {
            return st_fut.get_ref();
        }
        None
    }

    /// Insert a stream into the map.
    ///
    /// This function submits the given stream to the map for managing. This
    /// function will not call `poll` on the submitted stream. The caller must
    /// ensure that `MappedStreams::poll` is called in order to receive task
    /// notifications.
    pub fn insert(&mut self, key: K, stream: St) -> bool {
        self.inner.insert(key, stream.into_future())
    }

    /// Insert a future into the set and return the displaced future, if there was one.
    pub fn replace(&mut self, key: K, stream: St) -> Option<St> {
        let replacing = self.remove(&key);
        self.insert(key, stream);
        replacing
    }
}

pub struct StMut<'a, K: Hash + Eq, St: Stream + Unpin> {
    inner: FutMut<'a, K, StreamFuture<St>>,
}

impl<'a, K: Hash + Eq, St: Stream + Unpin> Deref for StMut<'a, K, St> {
    type Target = St;

    fn deref(&self) -> &Self::Target {
        self.inner.get_ref().unwrap()
    }
}
impl<'a, K: Hash + Eq, St: Stream + Unpin> DerefMut for StMut<'a, K, St> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.get_mut().unwrap()
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin> Default for MappedStreams<K, St> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a, K: Hash + Eq, St: Stream + Unpin> StMut<'a, K, St> {
    // fn new(task: &'a Arc<Task<K, StreamFuture<St>>>) -> Self {
    //     FutMut {
    //         inner: Arc::as_ptr(task),
    //         mutated: false,
    //         _marker: PhantomData,
    //     }
    // }
    pub(super) fn new_from_ptr(task: *const Task<K, StreamFuture<St>>) -> Self {
        StMut {
            inner: FutMut::new_from_ptr(task),
        }
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin> Stream for MappedStreams<K, St> {
    type Item = (K, Option<St::Item>);

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        loop {
            match ready!(self.inner.poll_next_unpin(cx)) {
                Some((key, (Some(item), remaining))) => {
                    self.insert(key.clone(), remaining);
                    return Poll::Ready(Some((key, Some(item))));
                }
                Some((key, (None, _))) => {
                    // `MappedFutures` thinks it isn't terminated
                    // because it yielded a Some.
                    // Return the key of the stream that terminated.
                    return Poll::Ready(Some((key, None)));
                }
                None => return Poll::Ready(None),
            }
        }
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin> FusedStream for MappedStreams<K, St> {
    fn is_terminated(&self) -> bool {
        self.inner.is_terminated()
    }
}

/// Convert a list of streams into a `Stream` of results from the streams.
///
/// This essentially takes a list of streams (e.g. a vector, an iterator, etc.)
/// and bundles them together into a single stream.
/// The stream will yield items as they become available on the underlying
/// streams internally, in the order they become available.
///
/// Note that the returned set can also be used to dynamically push more
/// streams into the set as they become available.
pub fn map_all<K, I, St>(streams: I) -> MappedStreams<K, St, RandomState>
where
    K: Hash + Eq + Clone,
    I: IntoIterator<Item = (K, St)>,
    St: Stream + Unpin,
{
    let mut map = MappedStreams::new();
    map.extend(streams);
    map
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin> FromIterator<(K, St)> for MappedStreams<K, St> {
    fn from_iter<T: IntoIterator<Item = (K, St)>>(iter: T) -> Self {
        let mut map = MappedStreams::new();

        for (key, stream) in iter {
            map.insert(key, stream);
        }
        map
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin> Extend<(K, St)> for MappedStreams<K, St> {
    fn extend<T: IntoIterator<Item = (K, St)>>(&mut self, iter: T) {
        for (key, st) in iter {
            self.insert(key, st);
        }
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin, S: BuildHasher> IntoIterator
    for MappedStreams<K, St, S>
{
    type Item = St;
    type IntoIter = IntoIter<K, St, S>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter(self.inner.into_iter())
    }
}

impl<'a, K: Clone + Hash + Eq, St: Stream + Unpin, S: BuildHasher> IntoIterator
    for &'a MappedStreams<K, St, S>
{
    type Item = &'a St;
    type IntoIter = Iter<'a, K, St, S>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, K: Clone + Hash + Eq, St: Stream + Unpin, S: BuildHasher> IntoIterator
    for &'a mut MappedStreams<K, St, S>
{
    type Item = &'a mut St;
    type IntoIter = IterMut<'a, K, St, S>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// Immutable iterator over all streams in the map.
#[derive(Debug)]
pub struct Iter<'a, K: Hash + Eq, St: Unpin, S: BuildHasher>(
    super::Iter<'a, K, StreamFuture<St>, S>,
);

/// Mutable iterator over all streams in the map.
#[derive(Debug)]
pub struct IterMut<'a, K: Hash + Eq, St: Unpin, S: BuildHasher>(
    super::IterMut<'a, K, StreamFuture<St>, S>,
);

/// Owned iterator over all streams in the unordered set.
#[derive(Debug)]
pub struct IntoIter<K: Hash + Eq, St: Unpin, S: BuildHasher>(
    super::IntoIter<K, StreamFuture<St>, S>,
);

impl<'a, K: Clone + Hash + Eq, St: Stream + Unpin, S: BuildHasher> Iterator for Iter<'a, K, St, S> {
    type Item = &'a St;

    fn next(&mut self) -> Option<Self::Item> {
        let st = self.0.next()?;
        let next = st.get_ref();
        // This should always be true because MappedFutures removes completed futures.
        debug_assert!(next.is_some());
        next
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin, S: BuildHasher> ExactSizeIterator
    for Iter<'_, K, St, S>
{
}

impl<'a, K: Clone + Hash + Eq, St: Stream + Unpin, S: BuildHasher> Iterator
    for IterMut<'a, K, St, S>
{
    type Item = &'a mut St;

    fn next(&mut self) -> Option<Self::Item> {
        let st = self.0.next()?;
        let next = st.get_mut();
        // This should always be true because MappedFutures removes completed futures.
        debug_assert!(next.is_some());
        next
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin, S: BuildHasher> ExactSizeIterator
    for IterMut<'_, K, St, S>
{
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin, S: BuildHasher> Iterator for IntoIter<K, St, S> {
    type Item = St;

    fn next(&mut self) -> Option<Self::Item> {
        let st = self.0.next()?;
        let next = st.into_inner();
        // This should always be true because MappedFutures removes completed futures.
        debug_assert!(next.is_some());
        next
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<K: Hash + Eq + Clone, St: Stream + Unpin, S: BuildHasher> ExactSizeIterator
    for IntoIter<K, St, S>
{
}

#[cfg(test)]
pub mod tests {
    use super::MappedStreams;
    use futures::executor::block_on;
    use futures_core::Stream;
    use futures_lite::FutureExt as FutureLiteExt;
    use futures_task::Poll;
    use futures_timer::Delay;
    use futures_util::StreamExt;
    use std::time::Duration;
    use std::{pin::Pin, task::ready};

    struct DelayStream {
        num: u8,
        interval: u64,
        fut: Option<Delay>,
    }

    impl DelayStream {
        fn new(num: u8, interval: u64) -> Self {
            DelayStream {
                num,
                interval,
                fut: None,
            }
        }
    }

    impl Stream for DelayStream {
        type Item = ();

        fn poll_next(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut futures_task::Context<'_>,
        ) -> Poll<Option<Self::Item>> {
            match &mut self.fut {
                Some(fut) => {
                    ready!(Pin::new(fut).poll(cx));
                    self.fut = None;
                    self.num = self.num - 1;
                    Poll::Ready(Some(()))
                }
                None => match self.num {
                    0 => Poll::Ready(None),
                    _ => {
                        println!("interval: {}", self.interval);
                        self.fut = Some(Delay::new(Duration::from_millis(self.interval)));
                        self.poll_next(cx)
                    }
                },
            }
        }
    }

    #[test]
    fn map_streams() {
        let mut streams = MappedStreams::new();
        let s1 = futures::stream::iter(vec![1]);
        let s2 = futures::stream::iter(vec![2]);
        streams.insert(1, s1);
        streams.insert(2, s2);
        let output: Vec<_> = block_on(streams.collect());
        assert_eq!(
            output,
            vec![(1, Some(1)), (2, Some(2)), (1, None), (2, None)]
        );
    }

    #[test]
    fn mutate_streams() {
        let mut streams = MappedStreams::new();
        streams.insert(1, DelayStream::new(1, 500));
        streams.insert(2, DelayStream::new(1, 600));
        streams.get_mut(&1).unwrap().interval = 700;
        assert_eq!(block_on(streams.next()), Some((2, Some(()))));
        assert_eq!(block_on(streams.next()), Some((2, None)));
        assert_eq!(block_on(streams.next()), Some((1, Some(()))));
        assert_eq!(block_on(streams.next()), Some((1, None)));
        assert!(streams.is_empty());
    }

    #[test]
    fn remove_streams() {
        let mut streams: MappedStreams<i32, DelayStream> = MappedStreams::new();
        streams.insert(1, DelayStream::new(1, 50));
        streams.insert(2, DelayStream::new(1, 60));
        streams.remove(&1);
        assert_eq!(block_on(streams.next()), Some((2, Some(()))));
    }
}