@@ -29,6 +29,10 @@ mod collect;
29
29
#[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
30
30
pub use self :: collect:: Collect ;
31
31
32
+ mod unzip;
33
+ #[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
34
+ pub use self :: unzip:: Unzip ;
35
+
32
36
mod concat;
33
37
#[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
34
38
pub use self :: concat:: Concat ;
@@ -477,6 +481,45 @@ pub trait StreamExt: Stream {
477
481
assert_future :: < C , _ > ( Collect :: new ( self ) )
478
482
}
479
483
484
+ /// Converts a stream of pairs into a future, which
485
+ /// resolves to pair of containers.
486
+ ///
487
+ /// `unzip()` produces a future, which resolves to two
488
+ /// collections: one from the left elements of the pairs,
489
+ /// and one from the right elements.
490
+ ///
491
+ /// The returned future will be resolved when the stream terminates.
492
+ ///
493
+ /// # Examples
494
+ ///
495
+ /// ```
496
+ /// # futures::executor::block_on(async {
497
+ /// use futures::channel::mpsc;
498
+ /// use futures::stream::StreamExt;
499
+ /// use std::thread;
500
+ ///
501
+ /// let (tx, rx) = mpsc::unbounded();
502
+ ///
503
+ /// thread::spawn(move || {
504
+ /// tx.unbounded_send((1, 2)).unwrap();
505
+ /// tx.unbounded_send((3, 4)).unwrap();
506
+ /// tx.unbounded_send((5, 6)).unwrap();
507
+ /// });
508
+ ///
509
+ /// let (o1, o2): (Vec<_>, Vec<_>) = rx.unzip().await;
510
+ /// assert_eq!(o1, vec![1, 3, 5]);
511
+ /// assert_eq!(o2, vec![2, 4, 6]);
512
+ /// # });
513
+ /// ```
514
+ fn unzip < A , B , FromA , FromB > ( self ) -> Unzip < Self , FromA , FromB >
515
+ where
516
+ FromA : Default + Extend < A > ,
517
+ FromB : Default + Extend < B > ,
518
+ Self : Sized + Stream < Item = ( A , B ) > ,
519
+ {
520
+ assert_future :: < ( FromA , FromB ) , _ > ( Unzip :: new ( self ) )
521
+ }
522
+
480
523
/// Concatenate all items of a stream into a single extendable
481
524
/// destination, returning a future representing the end result.
482
525
///
0 commit comments