From 8ff51123523f9951558bce564dc15fc3ee211da7 Mon Sep 17 00:00:00 2001 From: Norbert Morawski Date: Sat, 8 Jul 2023 05:47:58 +0200 Subject: [PATCH] more rewrites to tokio --- src/main.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index ae76396..dba06cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ -use std::collections::HashMap; use std::env; use std::error::Error; use std::net::SocketAddr; use std::sync::Arc; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use std::collections::HashMap; +use tokio::io::{AsyncReadExt, AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::net::{TcpListener, TcpStream}; use tokio::sync::Mutex; @@ -94,7 +94,8 @@ async fn start_client(addr: &str) -> Result<(), Box> { println!("Enter a username: "); let mut username = String::new(); - std::io::stdin().read_line(&mut username)?; + let mut reader = BufReader::new(tokio::io::stdin()); + reader.read_line(&mut username).await?; // Send username to the server stream.write_all(username.trim().as_bytes()).await?; @@ -102,7 +103,7 @@ async fn start_client(addr: &str) -> Result<(), Box> { println!("Connected to server at {}", addr); let mut input = String::new(); loop { - std::io::stdin().read_line(&mut input)?; + reader.read_line(&mut input).await?; stream.write_all(input.trim().as_bytes()).await?; // Print sent message @@ -110,3 +111,4 @@ async fn start_client(addr: &str) -> Result<(), Box> { input.clear(); } } +