From 840cc3888ee91c297c0415da4092c022e97a9c29 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 27 Oct 2024 12:17:32 +0100 Subject: [PATCH] Exercise 08-01 --- exercises/08_futures/00_intro/src/lib.rs | 2 +- exercises/08_futures/01_async_fn/src/lib.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/exercises/08_futures/00_intro/src/lib.rs b/exercises/08_futures/00_intro/src/lib.rs index c730220..d464426 100644 --- a/exercises/08_futures/00_intro/src/lib.rs +++ b/exercises/08_futures/00_intro/src/lib.rs @@ -1,6 +1,6 @@ fn intro() -> &'static str { // TODO: fix me 👇 - "I'm ready to _!" + "I'm ready to learn about futures!" } #[cfg(test)] diff --git a/exercises/08_futures/01_async_fn/src/lib.rs b/exercises/08_futures/01_async_fn/src/lib.rs index b8a83d8..dc825f9 100644 --- a/exercises/08_futures/01_async_fn/src/lib.rs +++ b/exercises/08_futures/01_async_fn/src/lib.rs @@ -1,4 +1,7 @@ -use tokio::net::TcpListener; +use tokio::{ + io::{AsyncReadExt, AsyncWriteExt}, + net::TcpListener, +}; // TODO: write an echo server that accepts incoming TCP connections and // echoes the received data back to the client. @@ -11,7 +14,11 @@ use tokio::net::TcpListener; // - `tokio::net::TcpStream::split` to obtain a reader and a writer from the socket // - `tokio::io::copy` to copy data from the reader to the writer pub async fn echo(listener: TcpListener) -> Result<(), anyhow::Error> { - todo!() + loop { + let (mut tcp_stream, _socket_addr) = listener.accept().await?; + let (mut reader, mut writer) = tcp_stream.split(); + tokio::io::copy(&mut reader, &mut writer).await?; + } } #[cfg(test)] -- 2.45.2