Skip to content

FuturesUnordered guarantiesΒ #2837

@stormshield-gt

Description

@stormshield-gt

In the documentation of FuturesUnordered it's written

"When new futures are added, poll_next must be called in order to begin receiving wake-ups for new futures."

Does it mean that if I consume the FuturesUnordered as a Stream and there is no more futures inside it, it would return None but if then later I add a future, it will return Some again?

In the current implementation, this seems to be the case:

use std::time::Duration;

use futures::stream::{FusedStream, FuturesUnordered};
use futures::StreamExt;

fn main() {
   tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(run())
}

async fn run() {
   let mut fuo = [f(), f()].into_iter().collect::<FuturesUnordered<_>>();

   assert!(fuo.next().await.is_some());
   assert!(fuo.next().await.is_some());
   assert!(fuo.next().await.is_none());

   assert!(fuo.is_terminated());

   fuo.push(f());

   assert!(fuo.next().await.is_some());

   println!("Done !");
}

async fn f() {
   tokio::time::sleep(Duration::from_millis(100)).await;
}

PlayGround Link

Is it a behavior I can rely upon? In other words, is this guarantee by FuturesUnordered ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions