<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Metehan Yıldırım on Medium]]></title>
        <description><![CDATA[Stories by Metehan Yıldırım on Medium]]></description>
        <link>https://medium.com/@meteyldrm?source=rss-b769ce9dd370------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Uey3_Gg_6bbgqux_NBuiNg.png</url>
            <title>Stories by Metehan Yıldırım on Medium</title>
            <link>https://medium.com/@meteyldrm?source=rss-b769ce9dd370------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:57:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@meteyldrm/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Async I/O with Tokio & Rust]]></title>
            <link>https://medium.com/@meteyldrm/async-i-o-with-tokio-rust-256f47742e26?source=rss-b769ce9dd370------2</link>
            <guid isPermaLink="false">https://medium.com/p/256f47742e26</guid>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[rustlang]]></category>
            <dc:creator><![CDATA[Metehan Yıldırım]]></dc:creator>
            <pubDate>Sun, 09 Apr 2023 13:23:39 GMT</pubDate>
            <atom:updated>2023-04-09T13:23:39.613Z</atom:updated>
            <content:encoded><![CDATA[<p>Modern applications often deal with a multitude of concurrent I/O operations, such as reading and writing files, network communication, or interacting with external devices. With traditional blocking I/O, the application comes to a halt, waiting for each I/O operation to complete before moving forward. This can lead to performance issues and limit the application’s scalability. To address these challenges, we turn to asynchronous I/O. This is where Tokio Rust comes into play — a powerful asynchronous runtime library for the Rust programming language that enables developers to build high-performance, non-blocking applications using async/await syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JH_HLLsRBLg-iju6.jpg" /></figure><h4>A Core Building Block: Async I/O</h4><p>As mentioned earlier, blocking I/O can be a bottleneck when working with numerous concurrent I/O operations. With blocking I/O, threads are not allowed to run until the I/O operation is complete.</p><p>In contrast, non-blocking I/O allows a thread to queue an I/O request with the OS and continue executing other tasks. The I/O request is then executed at some later point by the kernel. The thread needs to check with the OS whether the I/O request is ready before accessing the result.</p><p>Repeatedly checking with the OS for completion can be resource-intensive and cumbersome. This is where system calls like epoll come into play. With epoll, you can bundle together multiple unfinished I/O requests and ask the OS to wake up your thread when any of these requests are complete.</p><h3>AsyncRead, AsyncWrite, and more</h3><p>To read from a file asynchronously, we can use the <strong>tokio::fs::File</strong> struct and the <strong>AsyncReadExt</strong> trait. Let&#39;s create a simple example that reads the contents of a file and prints it to the console:</p><pre>use tokio::io::{self, AsyncReadExt};<br>use tokio::fs::File;<br><br>#[tokio::main]<br>async fn main() -&gt; io::Result&lt;()&gt; {<br>    let mut f = File::open(&quot;foo.txt&quot;).await?;<br>    let mut buffer = [0; 10];<br>    // read up to 10 bytes<br>    let n = f.read(&amp;mut buffer).await?;<br>    println!(&quot;The bytes: {:?}&quot;, &amp;buffer[..n]);<br>    Ok(())<br>}</pre><p>In this example, we use <strong>tokio::fs::File::open</strong> to open a file asynchronously. We then read the contents of the file into a <strong>String</strong> using the <strong>AsyncReadExt::read_to_string</strong> method. Finally, we print the contents to the console.</p><p>Tokio also furnishes an async counterpart to the <strong>std::io::BufRead</strong> trait, called <strong>AsyncBufRead</strong>. Additionally, it provides async <strong>BufReader</strong> and <strong>BufWriter</strong> structs that wrap readers and writers, respectively. By utilizing a buffer, these wrappers reduce the number of system calls and offer more convenient methods for accessing the data you need.</p><p>For instance, the <strong>BufReader</strong> works in conjunction with the <strong>AsyncBufRead</strong> trait to introduce extra methods for any async reader:</p><pre>// &lt;https://docs.rs/tokio/latest/tokio/io/index.html&gt;<br>use tokio::io::{self, BufReader, AsyncBufReadExt};<br>use tokio::fs::File;<br><br>#[tokio::main]<br>async fn main() -&gt; io::Result&lt;()&gt; {<br>    let f = File::open(&quot;foo.txt&quot;).await?;<br>    let mut reader = BufReader::new(f);<br>    let mut buffer = String::new();<br>    // Read a line into the buffer<br>    reader.read_line(&amp;mut buffer).await?;<br>    println!(&quot;{}&quot;, buffer);<br>    Ok(())<br>}</pre><p>On the other hand, <strong>BufWriter</strong> does not introduce new writing methods. Instead, it buffers every call to write. To ensure that any buffered data is written, you must flush the <strong>BufWriter</strong>.</p><pre>// &lt;https://docs.rs/tokio/latest/tokio/io/index.html&gt;<br>use tokio::io::{self, BufWriter, AsyncWriteExt};<br>use tokio::fs::File;<br><br>#[tokio::main]<br>async fn main() -&gt; io::Result&lt;()&gt; {<br>    let f = File::create(&quot;foo.txt&quot;).await?;<br>    {<br>        let mut writer = BufWriter::new(f);<br>        // Write a byte to the buffer<br>        writer.write(&amp;[42u8]).await?;<br>        // Flush the buffer before it goes out of scope<br>        writer.flush().await?;<br>    } // If not flushed or shut down, the contents of the buffer are discarded on drop<br>    Ok(())<br>}</pre><p>In summary, Tokio’s buffered readers and writers enhance the efficiency of async I/O operations by minimizing system calls and providing a more user-friendly interface.</p><h3>Custom Implementations</h3><p>Let’s create a custom <strong>AsyncRead</strong> and <strong>AsyncWrite</strong> implementation for a simplified in-memory buffer. Our <strong>MemBuffer</strong> will act as an async I/O resource, allowing us to read and write data asynchronously.</p><pre>use std::io::Result;<br>use std::pin::Pin;<br>use std::task::{Context, Poll};<br>use tokio::io::{AsyncRead, AsyncWrite};<br>use tokio::io::ReadBuf;<br><br>struct MemBuffer {<br>    data: Vec&lt;u8&gt;,<br>    read_pos: usize,<br>}<br>impl MemBuffer {<br>    fn new() -&gt; Self {<br>        Self {<br>            data: Vec::new(),<br>            read_pos: 0,<br>        }<br>    }<br>}<br>impl AsyncRead for MemBuffer {<br>    fn poll_read(<br>        mut self: Pin&lt;&amp;mut Self&gt;,<br>        _cx: &amp;mut Context,<br>        buf: &amp;mut ReadBuf&lt;&#39;_&gt;,<br>    ) -&gt; Poll&lt;Result&lt;()&gt;&gt; {<br>        // Calculate the number of available bytes to read<br>        let available = self.data.len() - self.read_pos;<br>        // Determine the number of bytes to read, taking the minimum of the remaining capacity of ReadBuf and the available bytes<br>        let bytes_to_read = available.min(buf.remaining());<br>        // Get the slice of data to be read from MemBuffer<br>        let data = &amp;self.data[self.read_pos..self.read_pos + bytes_to_read];<br>        // Put the data slice into the ReadBuf<br>        buf.put_slice(data);<br>        // Update the read position in MemBuffer<br>        self.read_pos += bytes_to_read;<br>        Poll::Ready(Ok(()))<br>    }<br>}<br>impl AsyncWrite for MemBuffer {<br>    fn poll_write(<br>        mut self: Pin&lt;&amp;mut Self&gt;,<br>        _cx: &amp;mut Context,<br>        buf: &amp;[u8],<br>    ) -&gt; Poll&lt;Result&lt;usize&gt;&gt; {<br>        self.data.extend_from_slice(buf);<br>        Poll::Ready(Ok(buf.len()))<br>    }<br>    fn poll_flush(self: Pin&lt;&amp;mut Self&gt;, _cx: &amp;mut Context) -&gt; Poll&lt;Result&lt;()&gt;&gt; {<br>        // Since our buffer is in-memory, we don&#39;t need to do anything to flush.<br>        Poll::Ready(Ok(()))<br>    }<br>    fn poll_shutdown(self: Pin&lt;&amp;mut Self&gt;, _cx: &amp;mut Context) -&gt; Poll&lt;Result&lt;()&gt;&gt; {<br>        // No special shutdown procedure is required for our in-memory buffer.<br>        Poll::Ready(Ok(()))<br>    }<br>}</pre><p>Now that we have our custom <strong>MemBuffer</strong> implementation, we can use it in an async context. Let&#39;s create a simple example that writes to and reads from our <strong>MemBuffer</strong>.</p><pre>use tokio::io::{AsyncReadExt, AsyncWriteExt};<br><br>#[tokio::main]<br>async fn main() -&gt; std::result::Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {<br>    let mut buffer = MemBuffer::new();<br>    // Write data to the buffer<br>    buffer.write_all(b&quot;Hello, world!&quot;).await?;<br>    // Reset the read position to read from the beginning<br>    buffer.read_pos = 0;<br>    // Read the data back from the buffer<br>    let mut read_buf = vec![0; 13];<br>    buffer.read_exact(&amp;mut read_buf).await?;<br>    assert_eq!(read_buf, b&quot;Hello, world!&quot;);<br>    println!(&quot;Successfully read data: {:?}&quot;, String::from_utf8(read_buf)?);<br>    Ok(())<br>}</pre><p>You can follow a similar approach for other custom I/O resources that you might need in your applications.</p><h3>Supporting Platforms</h3><p>Tokio is primarily tailored for Unix-like systems, encompassing Linux, macOS, and select BSD variants, as well as Windows. However, it’s worth noting that Tokio currently lacks comprehensive support for WebAssembly (Wasm). Efforts are underway to enable Tokio to function on Wasm platforms in the future, but for now, it remains unofficially supported.</p><p>For WebAssembly, you might want to consider alternative options, such as leveraging <strong>wasm-bindgen</strong> and <strong>web-sys</strong> to facilitate asynchronous I/O operations. These libraries offer a means of working with native JavaScript APIs for web applications, enabling you to interact with DOM elements, network requests, and other essential resources for web applications developed using Rust and Wasm.</p><h3>End</h3><p>With its well-designed abstractions, Tokio simplifies the complexities of async programming, making it more accessible and enjoyable for developers. Although it primarily targets Unix-like systems and Windows, there is an ongoing effort to extend its support to WebAssembly platforms.</p><p>By embracing the async capabilities of Tokio, you can build robust, high-performance, and scalable applications that are ready to tackle the demanding workloads of today’s modern computing environments. So go ahead, explore the world of Tokio, and unlock the full potential of asynchronous programming in Rust.</p><h3>Async Rust Resources</h3><ul><li><a href="https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html">Rust Async Book</a></li><li><a href="https://rust-unofficial.github.io/patterns/intro.html">Rust Design Patterns Book</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=256f47742e26" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FuturesUnordered: An Efficient Way to Manage Multiple Futures in Rust]]></title>
            <link>https://medium.com/better-programming/futuresunordered-an-efficient-way-to-manage-multiple-futures-in-rust-a24520abc3f6?source=rss-b769ce9dd370------2</link>
            <guid isPermaLink="false">https://medium.com/p/a24520abc3f6</guid>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[rustlang]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Metehan Yıldırım]]></dc:creator>
            <pubDate>Mon, 03 Apr 2023 18:09:38 GMT</pubDate>
            <atom:updated>2023-04-10T15:12:05.197Z</atom:updated>
            <content:encoded><![CDATA[<h4>FuturesUnordered is the life of the async party.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b8jQvb5pSrbGNp7RixbOcA.png" /></figure><p>In this post, we’re going to explore futures::stream::FuturesUnordered, a mighty and efficient tool for juggling multiple asynchronous tasks concurrently. This utility allows us to poll multiple futures in a non-blocking manner, automatically handling the completion of individual tasks and yielding their results as they become available.</p><p>Let’s create a few asynchronous functions that simulate different tasks and use FuturesUnordered to run them concurrently. First, add the required dependencies to your Cargo.toml file:</p><pre>[dependencies]<br>tokio = { version = &quot;1.0&quot;, features = [&quot;full&quot;] }<br>futures = &quot;0.3&quot;</pre><p>Next, create the asynchronous functions:</p><pre>use std::time::Duration;<br>use tokio::time::sleep;<br><br>// Define the async functions that return a String.<br>async fn task_one() -&gt; String {<br>    sleep(Duration::from_secs(3)).await;<br>    &quot;Task one completed&quot;.to_owned()<br>}<br>async fn task_two() -&gt; String {<br>    sleep(Duration::from_secs(1)).await;<br>    &quot;Task two completed&quot;.to_owned()<br>}<br>async fn task_three() -&gt; String {<br>    sleep(Duration::from_secs(2)).await;<br>    &quot;Task three completed&quot;.to_owned()<br>}</pre><p>We defined three asynchronous functions task_one, task_two, and task_three, each of which simulates a task with varying durations using tokio::time::sleep. Now, we&#39;ll use FuturesUnordered to run these tasks concurrently:</p><pre>use futures::stream::FuturesUnordered;<br>use futures::StreamExt;<br>use std::pin::Pin;<br>use std::future::Future;<br><br>#[tokio::main]<br>async fn main() {<br>    // Create a FuturesUnordered struct with the same type for all futures: Box&lt;dyn Future&lt;Output = String&gt;&gt;.<br>    let mut tasks = FuturesUnordered::&lt;Pin&lt;Box&lt;dyn Future&lt;Output = String&gt;&gt;&gt;&gt;::new();<br>    // Use Box::pin to create a pinned, heap-allocated version of each future.<br>    // This ensures that the memory location of the future does not change once it starts executing.<br>    tasks.push(Box::pin(task_one()));<br>    tasks.push(Box::pin(task_two()));<br>    tasks.push(Box::pin(task_three()));<br>    while let Some(result) = tasks.next().await {<br>        println!(&quot;{}&quot;, result);<br>    }<br>}</pre><p>When you run this example, you’ll see that the tasks are executed concurrently, and their results are printed as they complete. The output may vary in order, as it depends on the completion times of each task:</p><pre>Task two completed<br>Task three completed<br>Task one completed</pre><p>In this example, we defined three asynchronous functions task_one, task_two, and task_three, each of which simulates a task with varying durations using tokio::time::sleep.</p><p>We created a FuturesUnordered instance called tasks and pushed our three tasks into it.</p><h3>How does it work?</h3><p>The theory behind futures::stream::FuturesUnordered lies in its implementation, which combines efficient task management and Rust&#39;s asynchronous programming model. Here&#39;s an overview of how it works under the hood:</p><ol><li>FuturesUnordered is a data structure that holds a set of futures. Internally, it maintains two linked lists of futures — one for futures that are ready to be polled and another for futures that are not yet ready.</li><li>When you add a future to FuturesUnordered using push(), it is initially placed in the list of not-ready futures. The future is then registered with an async runtime to be notified when it&#39;s ready to make progress (i.e. when it&#39;s ready to be polled).</li><li>When a future becomes ready, it’s moved from the not-ready list to the ready list. This happens as a result of the waker associated with the future being notified by the async runtime.</li><li>The FuturesUnordered struct itself implements the Stream trait. When you call next() on a FuturesUnordered instance, it polls the futures in the ready list.</li><li>The next().await call will resolve to a completed future from the ready list when one is available. If there are no ready futures, it will asynchronously wait until a future becomes ready.</li><li>When a future is polled and completed, it’s removed from the ready list, and the result is returned by next().await. If the future is not completed, it remains on the ready list, and another future may be polled in the meantime. This process ensures that FuturesUnordered efficiently handles multiple futures concurrently, leveraging the available resources and avoiding blocking.</li><li>If a future is canceled or dropped, it will be removed from its respective list (ready or not ready), ensuring that any resources associated with the future are freed.</li><li>The FuturesUnordered stream continues to return values as long as there are futures in the ready or not-ready lists. Once all futures have been completed or canceled, the FuturesUnordered stream will return None, indicating that the stream has ended.</li></ol><h3>End</h3><p>FuturesUnordered is the life of the async party. It makes sure no future is left behind, and every future gets its moment in the spotlight when the time is right.</p><p>We’ve seen how FuturesUnordered maintains two lists, one for the futures that are ready to be polled, and another for those still waiting for their time.</p><p>Utilizing the capabilities of FuturesUnordered, Rust applications can achieve improved performance, effectively manage concurrency, and maximize resource utilization. It is essential to acknowledge the significance of this remarkable tool and recognize that asynchronous programming in Rust can be both efficient and rewarding.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a24520abc3f6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/futuresunordered-an-efficient-way-to-manage-multiple-futures-in-rust-a24520abc3f6">FuturesUnordered: An Efficient Way to Manage Multiple Futures in Rust</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>