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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//! Asynchronous iteration.
//!
//! This module is an async version of [`std::iter`].
//!
//! [`std::iter`]: https://doc.rust-lang.org/std/iter/index.html
//!
//! # Examples
//!
//! ```
//! # fn main() { async_std::task::block_on(async {
//! #
//! use async_std::prelude::*;
//! use async_std::stream;
//!
//! let mut s = stream::repeat(9).take(3);
//!
//! while let Some(v) = s.next().await {
//!     assert_eq!(v, 9);
//! }
//! #
//! # }) }
//! ```

mod all;
mod any;
mod enumerate;
mod filter_map;
mod find;
mod find_map;
mod fold;
mod fuse;
mod min_by;
mod next;
mod nth;
mod scan;
mod take;
mod zip;

pub use fuse::Fuse;
pub use scan::Scan;
pub use take::Take;
pub use zip::Zip;

use all::AllFuture;
use any::AnyFuture;
use enumerate::Enumerate;
use filter_map::FilterMap;
use find::FindFuture;
use find_map::FindMapFuture;
use fold::FoldFuture;
use min_by::MinByFuture;
use next::NextFuture;
use nth::NthFuture;

use std::cmp::Ordering;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

use cfg_if::cfg_if;

cfg_if! {
    if #[cfg(any(feature = "unstable", feature = "docs"))] {
        use crate::stream::FromStream;
    }
}

cfg_if! {
    if #[cfg(feature = "docs")] {
        #[doc(hidden)]
        pub struct ImplFuture<'a, T>(std::marker::PhantomData<&'a T>);

        macro_rules! ret {
            ($a:lifetime, $f:tt, $o:ty) => (ImplFuture<$a, $o>);
            ($a:lifetime, $f:tt, $o:ty, $t1:ty) => (ImplFuture<$a, $o>);
            ($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => (ImplFuture<$a, $o>);
            ($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => (ImplFuture<$a, $o>);

        }
    } else {
        macro_rules! ret {
            ($a:lifetime, $f:tt, $o:ty) => ($f<$a, Self>);
            ($a:lifetime, $f:tt, $o:ty, $t1:ty) => ($f<$a, Self, $t1>);
            ($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => ($f<$a, Self, $t1, $t2>);
            ($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => ($f<$a, Self, $t1, $t2, $t3>);
        }
    }
}

cfg_if! {
    if #[cfg(feature = "docs")] {
        #[doc(hidden)]
        pub struct DynFuture<'a, T>(std::marker::PhantomData<&'a T>);

        macro_rules! dyn_ret {
            ($a:lifetime, $o:ty) => (DynFuture<$a, $o>);
        }
    } else {
        #[allow(unused_macros)]
        macro_rules! dyn_ret {
            ($a:lifetime, $o:ty) => (Pin<Box<dyn core::future::Future<Output = $o> + Send + 'a>>)
        }
    }
}

/// An asynchronous stream of values.
///
/// This trait is an async version of [`std::iter::Iterator`].
///
/// While it is currently not possible to implement this trait directly, it gets implemented
/// automatically for all types that implement [`futures::stream::Stream`].
///
/// [`std::iter::Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
/// [`futures::stream::Stream`]:
/// https://docs.rs/futures-preview/0.3.0-alpha.17/futures/stream/trait.Stream.html
pub trait Stream {
    /// The type of items yielded by this stream.
    type Item;

    /// Attempts to receive the next item from the stream.
    ///
    /// There are several possible return values:
    ///
    /// * `Poll::Pending` means this stream's next value is not ready yet.
    /// * `Poll::Ready(None)` means this stream has been exhausted.
    /// * `Poll::Ready(Some(item))` means `item` was received out of the stream.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::pin::Pin;
    ///
    /// use async_std::prelude::*;
    /// use async_std::stream;
    /// use async_std::task::{Context, Poll};
    ///
    /// fn increment(s: impl Stream<Item = i32> + Unpin) -> impl Stream<Item = i32> + Unpin {
    ///     struct Increment<S>(S);
    ///
    ///     impl<S: Stream<Item = i32> + Unpin> Stream for Increment<S> {
    ///         type Item = S::Item;
    ///
    ///         fn poll_next(
    ///             mut self: Pin<&mut Self>,
    ///             cx: &mut Context<'_>,
    ///         ) -> Poll<Option<Self::Item>> {
    ///             match Pin::new(&mut self.0).poll_next(cx) {
    ///                 Poll::Pending => Poll::Pending,
    ///                 Poll::Ready(None) => Poll::Ready(None),
    ///                 Poll::Ready(Some(item)) => Poll::Ready(Some(item + 1)),
    ///             }
    ///         }
    ///     }
    ///
    ///     Increment(s)
    /// }
    ///
    /// let mut s = increment(stream::once(7));
    ///
    /// assert_eq!(s.next().await, Some(8));
    /// assert_eq!(s.next().await, None);
    /// #
    /// # }) }
    /// ```
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;

    /// Advances the stream and returns the next value.
    ///
    /// Returns [`None`] when iteration is finished. Individual stream implementations may
    /// choose to resume iteration, and so calling `next()` again may or may not eventually
    /// start returning more values.
    ///
    /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let mut s = stream::once(7);
    ///
    /// assert_eq!(s.next().await, Some(7));
    /// assert_eq!(s.next().await, None);
    /// #
    /// # }) }
    /// ```
    fn next(&mut self) -> ret!('_, NextFuture, Option<Self::Item>)
    where
        Self: Unpin,
    {
        NextFuture { stream: self }
    }

    /// Creates a stream that yields its first `n` elements.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let mut s = stream::repeat(9).take(3);
    ///
    /// while let Some(v) = s.next().await {
    ///     assert_eq!(v, 9);
    /// }
    /// #
    /// # }) }
    /// ```
    fn take(self, n: usize) -> Take<Self>
    where
        Self: Sized,
    {
        Take {
            stream: self,
            remaining: n,
        }
    }

    /// Creates a stream that gives the current element's count as well as the next value.
    ///
    /// # Overflow behaviour.
    ///
    /// This combinator does no guarding against overflows.
    ///
    /// # Examples
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use std::collections::VecDeque;
    ///
    /// let s: VecDeque<_> = vec!['a', 'b', 'c'].into_iter().collect();
    /// let mut s = s.enumerate();
    ///
    /// assert_eq!(s.next().await, Some((0, 'a')));
    /// assert_eq!(s.next().await, Some((1, 'b')));
    /// assert_eq!(s.next().await, Some((2, 'c')));
    /// assert_eq!(s.next().await, None);
    ///
    /// #
    /// # }) }
    fn enumerate(self) -> Enumerate<Self>
    where
        Self: Sized,
    {
        Enumerate::new(self)
    }

    /// Transforms this `Stream` into a "fused" `Stream` such that after the first time `poll`
    /// returns `Poll::Ready(None)`, all future calls to `poll` will also return
    /// `Poll::Ready(None)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let mut s = stream::once(1).fuse();
    /// assert_eq!(s.next().await, Some(1));
    /// assert_eq!(s.next().await, None);
    /// assert_eq!(s.next().await, None);
    /// #
    /// # }) }
    /// ```
    fn fuse(self) -> Fuse<Self>
    where
        Self: Sized,
    {
        Fuse {
            stream: self,
            done: false,
        }
    }

    /// Both filters and maps a stream.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::collections::VecDeque;
    /// use async_std::stream::Stream;
    ///
    /// let s: VecDeque<&str> = vec!["1", "lol", "3", "NaN", "5"].into_iter().collect();
    ///
    /// let mut parsed = s.filter_map(|a| a.parse::<u32>().ok());
    ///
    /// let one = parsed.next().await;
    /// assert_eq!(one, Some(1));
    ///
    /// let three = parsed.next().await;
    /// assert_eq!(three, Some(3));
    ///
    /// let five = parsed.next().await;
    /// assert_eq!(five, Some(5));
    ///
    /// let end = parsed.next().await;
    /// assert_eq!(end, None);
    /// #
    /// # }) }
    fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F, Self::Item, B>
    where
        Self: Sized,
        F: FnMut(Self::Item) -> Option<B>,
    {
        FilterMap::new(self, f)
    }

    /// Returns the element that gives the minimum value with respect to the
    /// specified comparison function. If several elements are equally minimum,
    /// the first element is returned. If the stream is empty, `None` is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::collections::VecDeque;
    /// use async_std::stream::Stream;
    ///
    /// let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
    ///
    /// let min = Stream::min_by(s.clone(), |x, y| x.cmp(y)).await;
    /// assert_eq!(min, Some(1));
    ///
    /// let min = Stream::min_by(s, |x, y| y.cmp(x)).await;
    /// assert_eq!(min, Some(3));
    ///
    /// let min = Stream::min_by(VecDeque::<usize>::new(), |x, y| x.cmp(y)).await;
    /// assert_eq!(min, None);
    /// #
    /// # }) }
    /// ```
    fn min_by<F>(self, compare: F) -> MinByFuture<Self, F, Self::Item>
    where
        Self: Sized,
        F: FnMut(&Self::Item, &Self::Item) -> Ordering,
    {
        MinByFuture::new(self, compare)
    }

    /// Returns the nth element of the stream.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::collections::VecDeque;
    /// use async_std::stream::Stream;
    ///
    /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
    ///
    /// let second = s.nth(1).await;
    /// assert_eq!(second, Some(2));
    /// #
    /// # }) }
    /// ```
    /// Calling `nth()` multiple times:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::collections::VecDeque;
    /// use async_std::stream::Stream;
    ///
    /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
    ///
    /// let second = s.nth(0).await;
    /// assert_eq!(second, Some(1));
    ///
    /// let second = s.nth(0).await;
    /// assert_eq!(second, Some(2));
    /// #
    /// # }) }
    /// ```
    /// Returning `None` if the stream finished before returning `n` elements:
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::collections::VecDeque;
    /// use async_std::stream::Stream;
    ///
    /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
    ///
    /// let fourth = s.nth(4).await;
    /// assert_eq!(fourth, None);
    /// #
    /// # }) }
    /// ```
    fn nth(&mut self, n: usize) -> ret!('_, NthFuture, Option<Self::Item>)
    where
        Self: Sized,
    {
        NthFuture::new(self, n)
    }

    /// Tests if every element of the stream matches a predicate.
    ///
    /// `all()` takes a closure that returns `true` or `false`. It applies
    /// this closure to each element of the stream, and if they all return
    /// `true`, then so does `all()`. If any of them return `false`, it
    /// returns `false`.
    ///
    /// `all()` is short-circuiting; in other words, it will stop processing
    /// as soon as it finds a `false`, given that no matter what else happens,
    /// the result will also be `false`.
    ///
    /// An empty stream returns `true`.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let mut s = stream::repeat::<u32>(42).take(3);
    /// assert!(s.all(|x| x ==  42).await);
    ///
    /// #
    /// # }) }
    /// ```
    ///
    /// Empty stream:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let mut s = stream::empty::<u32>();
    /// assert!(s.all(|_| false).await);
    /// #
    /// # }) }
    /// ```
    #[inline]
    fn all<F>(&mut self, f: F) -> ret!('_, AllFuture, bool, F, Self::Item)
    where
        Self: Unpin + Sized,
        F: FnMut(Self::Item) -> bool,
    {
        AllFuture {
            stream: self,
            result: true, // the default if the empty stream
            _marker: PhantomData,
            f,
        }
    }

    /// Searches for an element in a stream that satisfies a predicate.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use std::collections::VecDeque;
    ///
    /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
    /// let res = s.find(|x| *x == 2).await;
    /// assert_eq!(res, Some(2));
    /// #
    /// # }) }
    /// ```
    ///
    /// Resuming after a first find:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use std::collections::VecDeque;
    ///
    /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
    /// let res = s.find(|x| *x == 2).await;
    /// assert_eq!(res, Some(2));
    ///
    /// let next = s.next().await;
    /// assert_eq!(next, Some(3));
    /// #
    /// # }) }
    /// ```
    fn find<P>(&mut self, p: P) -> ret!('_, FindFuture, Option<Self::Item>, P, Self::Item)
    where
        Self: Sized,
        P: FnMut(&Self::Item) -> bool,
    {
        FindFuture::new(self, p)
    }

    /// Applies function to the elements of stream and returns the first non-none result.
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use std::collections::VecDeque;
    ///
    /// let mut s: VecDeque<&str> = vec!["lol", "NaN", "2", "5"].into_iter().collect();
    /// let first_number = s.find_map(|s| s.parse().ok()).await;
    ///
    /// assert_eq!(first_number, Some(2));
    /// #
    /// # }) }
    /// ```
    fn find_map<F, B>(&mut self, f: F) -> ret!('_, FindMapFuture, Option<B>, F, Self::Item, B)
    where
        Self: Sized,
        F: FnMut(Self::Item) -> Option<B>,
    {
        FindMapFuture::new(self, f)
    }

    /// A combinator that applies a function to every element in a stream
    /// producing a single, final value.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use std::collections::VecDeque;
    ///
    /// let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
    /// let sum = s.fold(0, |acc, x| acc + x).await;
    ///
    /// assert_eq!(sum, 6);
    /// #
    /// # }) }
    /// ```
    fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, F, Self::Item, B>
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        FoldFuture::new(self, init, f)
    }

    /// Tests if any element of the stream matches a predicate.
    ///
    /// `any()` takes a closure that returns `true` or `false`. It applies
    /// this closure to each element of the stream, and if any of them return
    /// `true`, then so does `any()`. If they all return `false`, it
    /// returns `false`.
    ///
    /// `any()` is short-circuiting; in other words, it will stop processing
    /// as soon as it finds a `true`, given that no matter what else happens,
    /// the result will also be `true`.
    ///
    /// An empty stream returns `false`.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let mut s = stream::repeat::<u32>(42).take(3);
    /// assert!(s.any(|x| x ==  42).await);
    /// #
    /// # }) }
    /// ```
    ///
    /// Empty stream:
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let mut s = stream::empty::<u32>();
    /// assert!(!s.any(|_| false).await);
    /// #
    /// # }) }
    /// ```
    #[inline]
    fn any<F>(&mut self, f: F) -> ret!('_, AnyFuture, bool, F, Self::Item)
    where
        Self: Unpin + Sized,
        F: FnMut(Self::Item) -> bool,
    {
        AnyFuture {
            stream: self,
            result: false, // the default if the empty stream
            _marker: PhantomData,
            f,
        }
    }

    /// A stream adaptor similar to [`fold`] that holds internal state and produces a new stream.
    ///
    /// [`fold`]: #method.fold
    ///
    /// `scan()` takes two arguments: an initial value which seeds the internal state, and a
    /// closure with two arguments, the first being a mutable reference to the internal state and
    /// the second a stream element. The closure can assign to the internal state to share state
    /// between iterations.
    ///
    /// On iteration, the closure will be applied to each element of the stream and the return
    /// value from the closure, an `Option`, is yielded by the stream.
    ///
    /// ## Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::collections::VecDeque;
    /// use async_std::stream::Stream;
    ///
    /// let s: VecDeque<isize> = vec![1, 2, 3].into_iter().collect();
    /// let mut s = s.scan(1, |state, x| {
    ///     *state = *state * x;
    ///     Some(-*state)
    /// });
    ///
    /// assert_eq!(s.next().await, Some(-1));
    /// assert_eq!(s.next().await, Some(-2));
    /// assert_eq!(s.next().await, Some(-6));
    /// assert_eq!(s.next().await, None);
    /// #
    /// # }) }
    /// ```
    #[inline]
    fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
    where
        Self: Sized,
        F: FnMut(&mut St, Self::Item) -> Option<B>,
    {
        Scan::new(self, initial_state, f)
    }

    /// 'Zips up' two streams into a single stream of pairs.
    ///
    /// `zip()` returns a new stream that will iterate over two other streams, returning a tuple
    /// where the first element comes from the first stream, and the second element comes from the
    /// second stream.
    ///
    /// In other words, it zips two streams together, into a single one.
    ///
    /// If either stream returns [`None`], [`poll_next`] from the zipped stream will return
    /// [`None`]. If the first stream returns [`None`], `zip` will short-circuit and `poll_next`
    /// will not be called on the second stream.
    ///
    /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
    /// [`poll_next`]: #tymethod.poll_next
    ///
    /// ## Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use std::collections::VecDeque;
    /// use async_std::stream::Stream;
    ///
    /// let l: VecDeque<isize> = vec![1, 2, 3].into_iter().collect();
    /// let r: VecDeque<isize> = vec![4, 5, 6, 7].into_iter().collect();
    /// let mut s = l.zip(r);
    ///
    /// assert_eq!(s.next().await, Some((1, 4)));
    /// assert_eq!(s.next().await, Some((2, 5)));
    /// assert_eq!(s.next().await, Some((3, 6)));
    /// assert_eq!(s.next().await, None);
    /// #
    /// # }) }
    /// ```
    #[inline]
    fn zip<U>(self, other: U) -> Zip<Self, U>
    where
        Self: Sized,
        U: Stream,
    {
        Zip::new(self, other)
    }

    /// Transforms a stream into a collection.
    ///
    /// `collect()` can take anything streamable, and turn it into a relevant
    /// collection. This is one of the more powerful methods in the async
    /// standard library, used in a variety of contexts.
    ///
    /// The most basic pattern in which `collect()` is used is to turn one
    /// collection into another. You take a collection, call [`stream`] on it,
    /// do a bunch of transformations, and then `collect()` at the end.
    ///
    /// Because `collect()` is so general, it can cause problems with type
    /// inference. As such, `collect()` is one of the few times you'll see
    /// the syntax affectionately known as the 'turbofish': `::<>`. This
    /// helps the inference algorithm understand specifically which collection
    /// you're trying to collect into.
    ///
    /// # Examples
    ///
    /// ```
    /// # fn main() { async_std::task::block_on(async {
    /// #
    /// use async_std::prelude::*;
    /// use async_std::stream;
    ///
    /// let s = stream::repeat(9u8).take(3);
    /// let buf: Vec<u8> = s.collect().await;
    ///
    /// assert_eq!(buf, vec![9; 3]);
    ///
    /// // You can also collect streams of Result values
    /// // into any collection that implements FromStream
    /// let s = stream::repeat(Ok(9)).take(3);
    /// // We are using Vec here, but other collections
    /// // are supported as well
    /// let buf: Result<Vec<u8>, ()> = s.collect().await;
    ///
    /// assert_eq!(buf, Ok(vec![9; 3]));
    ///
    /// // The stream will stop on the first Err and
    /// // return that instead
    /// let s = stream::repeat(Err(5)).take(3);
    /// let buf: Result<Vec<u8>, u8> = s.collect().await;
    ///
    /// assert_eq!(buf, Err(5));
    /// #
    /// # }) }
    /// ```
    ///
    /// [`stream`]: trait.Stream.html#tymethod.next
    #[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead (TODO)"]
    #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
    #[cfg(any(feature = "unstable", feature = "docs"))]
    fn collect<'a, B>(self) -> dyn_ret!('a, B)
    where
        Self: futures_core::stream::Stream + Sized + Send + 'a,
        <Self as futures_core::stream::Stream>::Item: Send,
        B: FromStream<<Self as futures_core::stream::Stream>::Item>,
    {
        FromStream::from_stream(self)
    }
}

impl<T: futures_core::stream::Stream + ?Sized> Stream for T {
    type Item = <Self as futures_core::stream::Stream>::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        futures_core::stream::Stream::poll_next(self, cx)
    }
}