Hello,
I have a code style question. According to tokio_stream docs when using both futures and tokio StreamExt, one should prefer import one and use other with fullly quallified syntax.
I would like to use both in stream adaptors and keep the code readable. Are there are any tips or good practices?
Here's a code example I have right now. I chose to shadow producer_stream because I couldn't use .chunks_timeout
directly.
use futures::stream::{self, StreamExt};
use rand::{distributions::Uniform, Rng};
use tokio::time::{self, Duration};
#[tokio::main]
async fn main() {
let duration = Duration::from_millis(200);
let interval = time::interval(duration);
let mut rng = rand::thread_rng();
let value_range = Uniform::new(0.0, 10.0);
let producer_stream = tokio_stream::wrappers::IntervalStream::new(interval)
.map(|_| {
// A random vector
let n: usize = rng.gen_range(1..5);
(0..n)
.map(|_| rng.sample(&value_range))
.collect::<Vec<f64>>()
})
.flat_map(|v| stream::iter(v));
let producer_stream =
tokio_stream::StreamExt::chunks_timeout(producer_stream, 10, Duration::from_millis(500));
tokio::pin!(producer_stream);
while let Some(items) = producer_stream.next().await {
println!("{:?}", items);
}
}