From c3c6732a052d83482a105dbb3ec86b0b89827785 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 28 Oct 2024 09:03:30 +0100 Subject: [PATCH] Exercise 08-05 --- exercises/08_futures/05_blocking/src/lib.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/exercises/08_futures/05_blocking/src/lib.rs b/exercises/08_futures/05_blocking/src/lib.rs index e9b4354..8f4909a 100644 --- a/exercises/08_futures/05_blocking/src/lib.rs +++ b/exercises/08_futures/05_blocking/src/lib.rs @@ -2,17 +2,24 @@ // When running the tests, you should observe that it hangs, due to a // deadlock between the caller and the server. // Use `spawn_blocking` inside `echo` to resolve the issue. -use std::io::{Read, Write}; +use std::{ + any, + io::{Read, Write}, +}; use tokio::net::TcpListener; pub async fn echo(listener: TcpListener) -> Result<(), anyhow::Error> { loop { let (socket, _) = listener.accept().await?; let mut socket = socket.into_std()?; - socket.set_nonblocking(false)?; - let mut buffer = Vec::new(); - socket.read_to_end(&mut buffer)?; - socket.write_all(&buffer)?; + + tokio::task::spawn_blocking(move || -> Result<(), anyhow::Error> { + socket.set_nonblocking(false)?; + let mut buffer = Vec::new(); + socket.read_to_end(&mut buffer)?; + socket.write_all(&buffer)?; + Ok(()) + }); } } -- 2.45.2