eventsource client: learning std::futures
I wrote the beginnings of a library to interface with EventSource. I thought it would be a quick and easy project, but the async part has taken me over a day by now.
The code is still in rough shape, since I've just managed to get the hard stuff cobbled together. Still, I wanted to post this here in case anybody can learn from it, and perhaps I can get some feedback also.
(I may write a blog post on this, especially if there's another round of improvements)
Some of the stuff I learned:
-
how to poll future/stream inside of an implementation of
Future/Stream(don't use a hot loop, I ended up using a state machine, I think this answer came somewhere on stackoverflow, can't find it now). -
working with
Pin, although I still don't understand it deeply. This took a long time because the error message was just that the method 'poll' was not available, without any suggestions. I ended up usingpoll_unpin. -
using
tokio::timerinside of aFuture/Streamimplementation. This one took several hours, I went down a lot of dead ends. The docs were definitely not enough for me, and the api has changed a bit. A working example would probably have been really helpful. This issue provided the aha moment. Fortunately, since I was using the tokio runtime, the solution was simple. But something about it was just hard to grasp, dunno why exactly. -
wrapping a stream within a stream, in order to flatten out chunked input. It was fun to poke at the docs for
stream::flatten. Not too hard, just took a while to decide that this was the (seemingly) best way.
Anyways, I'm a pretty stubborn guy so I feel satisfied having made it through. However, I can see how it's not easy to dive in; there's a whole new model to learn, and tons of details on top of the usual Rust stuff. If I didn't have any foundational grasp of what I was trying to do, I don't think I could have muddled through it.
Posted Nov 13, 2019, 7:49 PM UTC