Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl futures::stream::Stream for Controller/Listener #71

Merged
merged 1 commit into from
Feb 14, 2024

Conversation

ratmice
Copy link
Contributor

@ratmice ratmice commented Feb 10, 2024

In the example tokio usage in #45 it spawns an async task for each controller,
I thought it would be nice to be able to treat each controller as a stream and poll all controllers from a StreamMap, I implemented stream for Listener too, but didn't actually end up using it.

This allows something to the effect of the following to work, with the Controller automatically removed from the stream map on Event::Disconnect.

let mut stream_map = StreamMap::new();
loop {
    tokio::select! {
        c = &mut listener => {
            stdout.write_all(format!("Listener: {:?}\n", c).as_bytes()).await?;
            stream_map.insert(c.id(), c);     
        }
        Some(e) = stream_map.next() => stdout.write_all(format!("Event: {:?}", e).as_bytes()).await?,
    }
}

The main reason I added it in it's own feature is because it adds a new dependency on the futures crate.
Given that I wasn't sure if you would not want it to be in by default.

@AldaronLau
Copy link
Member

@ratmice Thanks!

I think it makes sense behind a feature, so looks good to merge to me.

@AldaronLau AldaronLau added this pull request to the merge queue Feb 14, 2024
Merged via the queue into ardaku:stable with commit 317a4a9 Feb 14, 2024
33 checks passed
@ratmice ratmice deleted the stream_impls branch February 14, 2024 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants