Future that wait until a specific event

I'm working on a project with two parts : a server that listen to a socket for client to register and hold the list of the client, and a second part that use the client list from the server part.

I want the second part to wait until at least one client is registered, but I want to avoid active waiting like this:

while server.client_count() == 0 {}
// do stuff

Is it possible to make a future that resolve as soon as a client is registered ?
Like this:

server.wait_for_client().await // wait until at least one client is registered
// do stuff

It's possible, but figuring out the ownership puzzle can be a bit tricky. The future's poll() method effectively receives a Waker as an argument┬╣. If there's no client registered, it will need to store that Waker somewhere that the server can find, so that it can call wake() when a client registers.

┬╣ via Context

Looks like Notify in tokio::sync - Rust is what i'm looking for.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.