@@ -114,6 +114,12 @@ cfg_target_has_atomic! {
114
114
#[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
115
115
pub use self :: try_buffer_unordered:: TryBufferUnordered ;
116
116
117
+ #[ cfg( feature = "alloc" ) ]
118
+ mod try_buffered;
119
+ #[ cfg( feature = "alloc" ) ]
120
+ #[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
121
+ pub use self :: try_buffered:: TryBuffered ;
122
+
117
123
#[ cfg( feature = "alloc" ) ]
118
124
mod try_for_each_concurrent;
119
125
#[ cfg( feature = "alloc" ) ]
@@ -773,7 +779,7 @@ pub trait TryStreamExt: TryStream {
773
779
assert_future :: < Result < Self :: Ok , Self :: Error > , _ > ( TryConcat :: new ( self ) )
774
780
}
775
781
776
- /// Attempt to execute several futures from a stream concurrently.
782
+ /// Attempt to execute several futures from a stream concurrently (unordered) .
777
783
///
778
784
/// This stream's `Ok` type must be a [`TryFuture`](futures_core::future::TryFuture) with an `Error` type
779
785
/// that matches the stream's `Error` type.
@@ -842,6 +848,80 @@ pub trait TryStreamExt: TryStream {
842
848
)
843
849
}
844
850
851
+ /// Attempt to execute several futures from a stream concurrently.
852
+ ///
853
+ /// This stream's `Ok` type must be a [`TryFuture`](futures_core::future::TryFuture) with an `Error` type
854
+ /// that matches the stream's `Error` type.
855
+ ///
856
+ /// This adaptor will buffer up to `n` futures and then return their
857
+ /// outputs in the order. If the underlying stream returns an error, it will
858
+ /// be immediately propagated.
859
+ ///
860
+ /// The returned stream will be a stream of results, each containing either
861
+ /// an error or a future's output. An error can be produced either by the
862
+ /// underlying stream itself or by one of the futures it yielded.
863
+ ///
864
+ /// This method is only available when the `std` or `alloc` feature of this
865
+ /// library is activated, and it is activated by default.
866
+ ///
867
+ /// # Examples
868
+ ///
869
+ /// Results are returned in the order of addition:
870
+ /// ```
871
+ /// # futures::executor::block_on(async {
872
+ /// use futures::channel::oneshot;
873
+ /// use futures::future::lazy;
874
+ /// use futures::stream::{self, StreamExt, TryStreamExt};
875
+ ///
876
+ /// let (send_one, recv_one) = oneshot::channel();
877
+ /// let (send_two, recv_two) = oneshot::channel();
878
+ ///
879
+ /// let mut buffered = lazy(move |cx| {
880
+ /// let stream_of_futures = stream::iter(vec![Ok(recv_one), Ok(recv_two)]);
881
+ ///
882
+ /// let mut buffered = stream_of_futures.try_buffered(10);
883
+ ///
884
+ /// assert!(buffered.try_poll_next_unpin(cx).is_pending());
885
+ ///
886
+ /// send_two.send(2i32)?;
887
+ /// assert!(buffered.try_poll_next_unpin(cx).is_pending());
888
+ /// Ok::<_, i32>(buffered)
889
+ /// }).await?;
890
+ ///
891
+ /// send_one.send(1i32)?;
892
+ /// assert_eq!(buffered.next().await, Some(Ok(1i32)));
893
+ /// assert_eq!(buffered.next().await, Some(Ok(2i32)));
894
+ ///
895
+ /// assert_eq!(buffered.next().await, None);
896
+ /// # Ok::<(), i32>(()) }).unwrap();
897
+ /// ```
898
+ ///
899
+ /// Errors from the underlying stream itself are propagated:
900
+ /// ```
901
+ /// # futures::executor::block_on(async {
902
+ /// use futures::channel::mpsc;
903
+ /// use futures::stream::{StreamExt, TryStreamExt};
904
+ ///
905
+ /// let (sink, stream_of_futures) = mpsc::unbounded();
906
+ /// let mut buffered = stream_of_futures.try_buffered(10);
907
+ ///
908
+ /// sink.unbounded_send(Ok(async { Ok(7i32) }))?;
909
+ /// assert_eq!(buffered.next().await, Some(Ok(7i32)));
910
+ ///
911
+ /// sink.unbounded_send(Err("error in the stream"))?;
912
+ /// assert_eq!(buffered.next().await, Some(Err("error in the stream")));
913
+ /// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
914
+ /// ```
915
+ #[ cfg_attr( feature = "cfg-target-has-atomic" , cfg( target_has_atomic = "ptr" ) ) ]
916
+ #[ cfg( feature = "alloc" ) ]
917
+ fn try_buffered ( self , n : usize ) -> TryBuffered < Self >
918
+ where
919
+ Self :: Ok : TryFuture < Error = Self :: Error > ,
920
+ Self : Sized ,
921
+ {
922
+ TryBuffered :: new ( self , n)
923
+ }
924
+
845
925
// TODO: false positive warning from rustdoc. Verify once #43466 settles
846
926
//
847
927
/// A convenience method for calling [`TryStream::try_poll_next`] on [`Unpin`]
0 commit comments