3 ms·
I’m a big fan of Rust’s Future model, but it is still colored in the same way as most languages that offer a “block until future/promise/whatever is complete” (
by johncolanduoni 6y ago
I’m a big fan of Rust’s Future model, but it is still colored in the same way as most languages that offer a “block until future/promise/whatever is complete” (C#, Java, etc.). You can do that, but depending on your executor model it can vary from inefficient (e.g. blocking a thread on the thread pool that is supposed to be running futures) to deadlocking (e.g. a single-threaded executor, or blocking on all threads of a thread pool).
Rust’s futures library actually contains a defense mechanism against precisely this behavior: a function “futures::executor::enter” that returns a guard that causes an immediate panic if another “futures::executor::enter” call is made on the same thread while it is alive. The idea is that every executor calls this when it takes over execution on a thread, so you get an immediate panic if you try to block on a future inside another future instead of risking a deadlock.