<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Trait on 300.Watts</title><link>https://300watts.me/tags/trait/</link><description>Recent content in Trait on 300.Watts</description><generator>Hugo</generator><language>en</language><managingEditor>morristai01@gmail.com (Morris)</managingEditor><webMaster>morristai01@gmail.com (Morris)</webMaster><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Sun, 10 Mar 2024 10:04:55 +0800</lastBuildDate><atom:link href="https://300watts.me/tags/trait/index.xml" rel="self" type="application/rss+xml"/><item><title>Async in Traits Just Save Us</title><link>https://300watts.me/posts/async-in-traits-just-save-us/</link><pubDate>Sun, 10 Mar 2024 10:04:55 +0800</pubDate><author>morristai01@gmail.com (Morris)</author><guid>https://300watts.me/posts/async-in-traits-just-save-us/</guid><description>&lt;h2 id="the-challenge-of-implementing-the-future-trait-for-custom-types" class="headerLink"&gt;
 &lt;a href="#the-challenge-of-implementing-the-future-trait-for-custom-types" class="header-mark"&gt;&lt;/a&gt;The Challenge of Implementing the Future Trait for Custom Types&lt;/h2&gt;&lt;p&gt;Developers who venture into crafting their own &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; implementations in Rust may encounter the intricate task of implementing the &lt;code&gt;Future&lt;/code&gt; trait for their custom types. Rust&amp;rsquo;s approach to &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; is nuanced, offering a stark contrast to languages like Go, which employ 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 &lt;code&gt;Future&lt;/code&gt; trait for custom types. The intricacies arise because &lt;code&gt;.await&lt;/code&gt; can&amp;rsquo;t be invoked within a &lt;strong&gt;non-async&lt;/strong&gt; function, necessitating the development of a state machine (or similar) for these custom types. This endeavor can be laborious and fraught with potential errors, difficult to maintain, and may prompt developers to opt for &lt;code&gt;BoxFuture&amp;lt;T&amp;gt;&lt;/code&gt;, a choice that could compromise performance.&lt;/p&gt;</description></item></channel></rss>