Reputation: 5142
I'm using Tokio and I want to receive requests from two different mpsc
queues. select!
seems like the way to go, but I'm not sure what the difference is between futures::select!
and tokio::select!
. Under which circumstances one should you use one over the other?
Upvotes: 23
Views: 5238
Reputation: 4077
To complement @matthias247's answer, a related big difference is that futures::select!
takes futures in branch expressions by mutable reference, so uncompleted futures can be re-used in a loop.
tokio::select!
, on the other hand, consumes passed futures. To get behavior similar to futures::select!
you need to explicitly pass a reference (e.g. &mut future
), and pin it if necessary (e.g. if it is async fn
). Tokio docs have a section on this, Resuming an async operation
This thread has an in-depth explanation of why Tokio decided not to use FusedFuture
.
Upvotes: 7
Reputation: 10416
tokio::select!
was built out of experiences with futures::select!
, but improves a bit on it to make it more ergonomic. E.g. the futures-rs
version of select!
requires Future
s to implement FusedFuture
, whereas Tokio's version no longer requires this.
Instead of this, Tokio's version supports preconditions in the macro to cover the same use-cases.
The PR in the tokio repo elaborates a bit more on this.
This change was also proposed for the futures-rs version, but has not been implemented there so far.
If you already have Tokio included in your project, then using Tokio's version seems preferable. But if you have not and do not want to add an additional dependency, then the futures-rs version will cover most use-cases too in a nearly identical fashion. The main difference is that some Future
s might need to be converted into FusedFuture
s through the FutureExt::fuse()
extension method.
Upvotes: 25