async-std icon indicating copy to clipboard operation
async-std copied to clipboard

Book: Select wants a FusedFuture for broker() in tutorial

Open clemensw opened this issue 6 years ago • 9 comments

The select! in client_writer() uses fuse(), but the final change to the broker currently doesn't and won't compile.

clemensw avatar Aug 23 '19 20:08 clemensw

Ran into this as well. It would be great if the tutorial briefly went into what these fuses are doing and why they are required too!

marcusklaas avatar Aug 26 '19 12:08 marcusklaas

Maybe CC @matklad? I know I've seen you chatting about fused things in the past.

jamesmunns avatar Aug 27 '19 12:08 jamesmunns

cc https://github.com/async-rs/async-std/pull/13 and https://github.com/async-rs/async-std/pull/40 :)

Fuse is definitely a pain point. #13 and #40 apply surgical fixes to it, but the underlying problem that one must call .fuse manually remains. It would be ideal if, instead of explaining the current quirks, we could just design a better API that doesn't require .fuse.

Suggested course of action:

  • merge #13 as it's a simple fix
  • write an RFC/issue for general stream integration and fuseing (cc @stjepang @yoshuawuyts ). My tentative proposal: just make all streams&futeres exposed by async-std fused. It seems adding fuse for futures is much cheaper than for iterators, so it's ok to always do this?

matklad avatar Aug 27 '19 13:08 matklad

It would be ideal if, instead of explaining the current quirks, we could just design a better API that doesn't require .fuse.

I think the "proper" solution would be to port crossbeam-channel to futures and provide a select macro that works with async channels.

ghost avatar Aug 27 '19 13:08 ghost

While improving the story around .fused() is desireable, merging this PR would make the example in the book compile (and be consistent with previous examples), which would help lower confusion for newbies like me.

clemensw avatar Aug 28 '19 05:08 clemensw

Unfortunately, the fix in this PR is not correct: as per select’s doc, fused should be called outside of the select loop, and this is a pretty bad footgun. Really, the underlying future is fused, we should just make sure it is reflected in the type system as in #13.

matklad avatar Aug 28 '19 06:08 matklad

To be more precise, the patch in this PR definitely makes the code work, but it doesn’t use select/fuse at they are supposed to be used, and I’d rather not do that in the docs.

matklad avatar Aug 28 '19 06:08 matklad

Thanks for the explanation.

clemensw avatar Aug 28 '19 06:08 clemensw

#208 and #187 add macro support for non-fused streams and futures respectively. I think this will allow us to bypass a lot of the problems with futures-rs's select blocks.

yoshuawuyts avatar Sep 18 '19 01:09 yoshuawuyts