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
//! # futures-buffered
//!
//! This project provides a single future structure: `FuturesUnorderedBounded`.
//!
//! Much like [`futures::stream::FuturesUnordered`](https://docs.rs/futures/0.3.25/futures/stream/struct.FuturesUnordered.html),
//! this is a thread-safe, `Pin` friendly, lifetime friendly, concurrent processing stream.
//!
//! The is different to `FuturesUnordered` in that `FuturesUnorderedBounded` has a fixed capacity for processing count.
//! This means it's less flexible, but produces better memory efficiency.
//!
//! ## Benchmarks
//!
//! ### Speed
//!
//! Running 65536 100us timers with 256 concurrent jobs in a single threaded tokio runtime:
//!
//! ```text
//! FuturesUnordered time: [420.47 ms 422.21 ms 423.99 ms]
//! FuturesUnorderedBounded time: [366.02 ms 367.54 ms 369.05 ms]
//! ```
//!
//! ### Memory usage
//!
//! Running 512000 `Ready<i32>` futures with 256 concurrent jobs.
//!
//! - count: the number of times alloc/dealloc was called
//! - alloc: the number of cumulative bytes allocated
//! - dealloc: the number of cumulative bytes deallocated
//!
//! ```text
//! FuturesUnordered
//! count: 1024002
//! alloc: 40960144 B
//! dealloc: 40960000 B
//!
//! FuturesUnorderedBounded
//! count: 2
//! alloc: 8264 B
//! dealloc: 0 B
//! ```
//!
//! ### Conclusion
//!
//! As you can see, `FuturesUnorderedBounded` massively reduces you memory overhead while providing a significant performance gain.
//! Perfect for if you want a fixed batch size
//!
//! # Example
//! ```
//! use futures::future::Future;
//! use futures::stream::StreamExt;
//! use futures_buffered::FuturesUnorderedBounded;
//! use hyper::client::conn::http1::{handshake, SendRequest};
//! use hyper::body::Incoming;
//! use hyper::{Request, Response};
//! use hyper_util::rt::TokioIo;
//! use tokio::net::TcpStream;
//!
//! # #[cfg(miri)] fn main() {}
//! # #[cfg(not(miri))] #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // create a tcp connection
//! let stream = TcpStream::connect("example.com:80").await?;
//!
//! // perform the http handshakes
//! let (mut rs, conn) = handshake(TokioIo::new(stream)).await?;
//! tokio::spawn(conn);
//!
//! /// make http request to example.com and read the response
//! fn make_req(rs: &mut SendRequest<String>) -> impl Future<Output = hyper::Result<Response<Incoming>>> {
//! let req = Request::builder()
//! .header("Host", "example.com")
//! .method("GET")
//! .body(String::new())
//! .unwrap();
//! rs.send_request(req)
//! }
//!
//! // create a queue that can hold 128 concurrent requests
//! let mut queue = FuturesUnorderedBounded::new(128);
//!
//! // start up 128 requests
//! for _ in 0..128 {
//! queue.push(make_req(&mut rs));
//! }
//! // wait for a request to finish and start another to fill its place - up to 1024 total requests
//! for _ in 128..1024 {
//! queue.next().await;
//! queue.push(make_req(&mut rs));
//! }
//! // wait for the tail end to finish
//! for _ in 0..128 {
//! queue.next().await;
//! }
//! # Ok(()) }
//! ```
//!
//! # Cooperative Scheduling
//!
//! The functionality provided by this crate are technically their own async schedulers. If you are using this functionality within another
//! async scheduler, you might not cooperate effectively and might starve other tasks in your scheduler.
//!
//! This crate makes sure it doesn't get stuck forever by forcing a yield periodically,
//! but if you're using Tokio you will get better scheduling behaviour if you enable the `tokio-coop` feature.
extern crate alloc;
extern crate std;
use Future;
use Stream;
pub use ;
pub use FuturesOrdered;
pub use FuturesOrderedBounded;
pub use FuturesUnordered;
pub use FuturesUnorderedBounded;
pub use IterExt;
pub use ;
pub use ;
pub use MergeUnbounded;
pub use ;
pub use ;
/// A convenience for futures that return `Result` values that includes
/// a variety of adapters tailored to such futures.
///
/// This is [`futures::TryFuture`](futures_core::future::TryFuture) except it's stricter on the future super-trait.
/// A convenience for streams that return `Result` values that includes
/// a variety of adapters tailored to such futures.
///
/// This is [`futures::TryStream`](futures_core::stream::TryStream) except it's stricter on the stream super-trait.