Re:Await in Rust
by Sharlin on Monday August 12, 2019 @11:19AM
(#20676189)
It’s easy to run an async function synchronously; just drop the `await` keyword and wait on the returned future.
[ Parent
| on HN ]
And so all library functions should be async by default? And if everything is async, then shouldn't synchronous programming require the extra keyword instead of the other way around?
No. A function that makes no async calls has no reason to be marked async.
And if someone wants to add an async call later, all callers need to change too.
Only if the function "leaks" the async-ness instead of containing it (that is, waiting on any async calls synchronously). But yes, making a function async is a backward-incompatible change, just like manually changing the return type from `T` to `Future<T>`.
Ok let me phrase it differently. If you have some library function that does some IO (for example) and you want to make it as useful as possible, would you declare it async or not? If you make it blocking, then you'd ruin any opportunity to have it called from async functions (because they don't want to be blocked). Therefore, you'd make it async. So that means that all IO library routines will become async, and possibly a lot of others too. Wouldn't it make more sense to have async be the default?
Re:Await in Rust
by laughinghan on Tuesday August 13, 2019 @08:52PM
(#20691759)
But when all those IO functions are rewritten to be async, their public names can be different. The sync versions needn't change names, so none of the callers would need to change. Nor would the sync versions need to duplicate code, because they can just be thin wrappers that call and wait on their async counterparts. (Note that this wouldn't be possible in JS.)