5 ms·
Can you give an example of what you're talking about? I'm pretty new to async and Rust in general but I'm having a hard time thinking of an example (almost cert
by ahepp 4y ago
Can you give an example of what you're talking about? I'm pretty new to async and Rust in general but I'm having a hard time thinking of an example (almost certainly a deficiency on my end haha).
- paulgb 4y agoHere’s an example: https://docs.rs/tokio/latest/tokio/time/fn.sleep.html#panics https://docs.rs/tokio/latest/tokio/time/fn.sleep.html#panics > It can also panic whenever a timer is created outside of a Tokio runtime. Note that nothing in the signature tells you that, just the function documentation. In practice, this ends up not being a big deal, IMHO. The ecosystem has largely coalesced around tokio as an executor. The only times I've run into it are when running Actix, which has its own global state on top of tokio.
- ahepp 4y agoAh, that makes sense. In fact, I was trying to unit test some time based code in tokio a month or two back, and it was really painful. I couldn’t figure out how to inject a fake clock into the runtime to test my timers.
- lalaithion 4y agoIt’s not a big deal because the ecosystem has coalesced around tokio, but it’s coalesced around tokio partially because it’s a big deal.
- davidatbu 4y agoIf one is using a function from tokio itself, is it not a reasonable expectation that one should check the docs as to whether the function requires the tokio executor?
- ahepp 4y agoPart of me agrees with what you’re saying. I mean, tokio is right there in the namespace. But another part of me questions why it’s ok that calling a function in the wrong context is a runtime panic rather than a compilation error. Coming from a C background it seems pretty normal to just have a “tokio_context*” argument on tokio functions that would at least make the statefullness less obscure?
- davidatbu 4y agoIs what you're suggesting that one has to pass that `tokio_context` into every `.await()`? If so, I personally would be in strong opposition, cuz I don't think the verbosity cost is justified, in any of the use cases for async I've come across.
- ahepp 4y agoI don't think you'd have to pass the context into an await? Await is a Rust construct, not a tokio one. Unless I'm making a complete fool of myself... Maybe I'm just jaded from writing too much C, but it doesn't seem like a big deal to me to change sleep(duration) to sleep(ctx, duration). Three letters to be correct, transparent, and not have global state? Look at what a complete mess the C standard library is from where they used global state like that. I'm sure there are lots of other ways you could do it, like make the context it's own struct and call a sleep impl for the struct or something?
- davidatbu 4y agoYeah, I guess we're disagreeing about a very non-concrete proposal, so there isn't much point to our convo :)
- lalaithion 4y agoHere's an example from outside the tokio library: https://github.com/google/tarpc/issues/297 https://github.com/google/tarpc/issues/297
- davidatbu 4y agoThanks! I guess it's more common than what my personal experience suggests (which is that it never happens).