News for hackers. Stuff that matters.
Re:Await in Rust by amelius on Monday August 12, 2019 @11:04AM (#20676038)
The problem is that you don't always know beforehand whether you'd like to perform a task synchronously or not.
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.
Re:Await in Rust by amelius on Monday August 12, 2019 @01:19PM (#20677566)
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?
Re:Await in Rust by Sharlin on Monday August 12, 2019 @01:24PM (#20677638)
No. A function that makes no async calls has no reason to be marked async.
Re:Await in Rust by amelius on Tuesday August 13, 2019 @02:53AM (#20683393)
And if someone wants to add an async call later, all callers need to change too.
Re:Await in Rust by Sharlin on Tuesday August 13, 2019 @03:44AM (#20683613)
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>`.
Re:Await in Rust by amelius on Tuesday August 13, 2019 @04:05PM (#20689682)
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.)
Re:Await in Rust by dnautics on Monday August 12, 2019 @11:22AM (#20676214)
and it can change. Let's say your bandwidth goes from 1GbE to 10GbE and now your bottleneck has changed. You may want to refactor which task is async.