tokio-tungstenite 与 tokio::select! 宏一起使用?

huangapple go评论64阅读模式
英文:

tokio-tungstenite with tokio::select! macro?

问题

以下是您要翻译的内容:

"The documentation for the tokio-tungstenite crate mostly just directs you to the examples section.

The client example hasn't been updated in a few years and I saw some unfamiliar async code here and because Rust's async APIs are still in flux I wanted to check the use futures_util::future::select was still idiomatic in this situation.

In particular, it seems to work fine if I replace it with the more commonly seen tokio::select! macro, which doesn't require pinning the passed futures.

I have good familiarity with tokio's APIs but not so much the lower level futures ones. Is there a reason to manually pin here and use the future::select instead? More generally, in modern idiomatic async Rust code when would one use the latter?

英文:

The documentation for the tokio-tungstenite crate mostly just directs you to the examples section.

The client example hasn't been updated in a few years and I saw some unfamiliar async code here and because Rust's async APIs are still in flux I wanted to check the use futures_util::future::select was still idiomatic in this situation.

In particular, it seems to work fine if I replace it with the more commonly seen tokio::select! macro, which doesn't require pinning the passed futures.

I have good familiarity with tokio's APIs but not so much the lower level futures ones. Is there a reason to manually pin here and use the future::select instead? More generally, in modern idiomatic async Rust code when would one use the latter?

答案1

得分: 1

可以用tokio::select!(或futures::select!)来替换任何使用futures::future::select()的地方,它们都执行相同的操作。select!允许选择多个未来,而select()则返回一个具名的未来。我倾向于在可以的情况下使用select!(我认为它可能也更高效,但我不太确定)。

英文:

It should be fine to replace any use of futures::future::select() with tokio::select! (or futures::select!). They both do the same thing. select! allow for more than one future, while select() gives you a named future. I prefer select! when I can (I think it is also more performant, but I'm not sure).

huangapple
  • 本文由 发表于 2023年2月18日 21:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定