Async in Traits Just Save Us
1 The Challenge of Implementing the Future Trait for Custom Types
Developers who venture into crafting their own async
/await
implementations in Rust may encounter the intricate task of implementing the Future
trait for their custom types. Rust’s approach to async
/await
is nuanced, offering a stark contrast to languages like Go, which employs preemptive scheduling. Instead, Rust embraces lazy evaluation and cooperative scheduling, allowing developers to meticulously control the yield points to the executor.
This level of control, however, introduces complexity in implementing the Future
trait for custom types. The intricacies arise because .await
can’t be invoked within a non-async function, necessitating the development of a state machine(or similiar) for these custom types. This endeavor can be laborious and fraught with potential errors, difficult to maintain, and may prompt developers to opt for BoxFuture<T>
, a choice that could compromise performance.