From d04a353e52134c61e9ce3602a4bce36a2ecfd68b Mon Sep 17 00:00:00 2001 From: RafayAhmad7548 Date: Fri, 11 Apr 2025 07:02:02 +0500 Subject: [PATCH] removed unecassary arc from sender type --- src/main.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 08bf61e..a359364 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use tokio_tungstenite::{accept_async, tungstenite::Message}; #[tokio::main] async fn main() { let mut connection_count: usize = 0; - let senders = Arc::new(Mutex::new(HashMap::>>::new())); + let senders = Arc::new(Mutex::new(HashMap::>::new())); let server = TcpListener::bind("0.0.0.0:9001").await.unwrap(); @@ -16,7 +16,7 @@ async fn main() { println!("Connection Received from ip: {}, port: {}", socket.ip(), socket.port()); let t_senders = Arc::clone(&senders); - let (sender, mut receiver): (Sender>, Receiver>) = channel(100); + let (sender, mut receiver): (Sender, Receiver) = channel(100); { let mut sender_lock = t_senders.lock().await; connection_count += 1; @@ -33,22 +33,19 @@ async fn main() { while let Some(res) = websocket_read.next().await { match res { Ok(msg) => { - let arc_msg = Arc::new(msg); - if arc_msg.is_close() { + if msg.is_close() { let sender_lock = t_senders.lock().await; - let arc_msg_clone = Arc::clone(&arc_msg); - match (*sender_lock).get(&id).unwrap().send(arc_msg_clone).await { + match (*sender_lock).get(&id).unwrap().send(msg).await { Ok(_) => println!("close message sent to thread no {}", id), Err(e) => println!("My Error: {}", e) } break; } - else if arc_msg.is_text() || arc_msg.is_binary() { + else if msg.is_text() || msg.is_binary() { let sender_lock = t_senders.lock().await; for (iter_id, sender) in (*sender_lock).iter() { - let arc_msg_clone = Arc::clone(&arc_msg); if *iter_id != id { - match (*sender).send(arc_msg_clone).await { + match (*sender).send(msg.clone()).await { Ok(_) => println!("message sent to thread no {}", *iter_id), Err(e) => println!("My Error: {}", e) } @@ -73,10 +70,9 @@ async fn main() { let id = connection_count; loop { match receiver.recv().await { - Some(arc_msg) => { + Some(msg) => { println!("Message received by thread {}", id); - println!("The message is {}", arc_msg); - let msg = (*arc_msg).clone(); + println!("The message is {}", msg); match websocket_send.send(msg).await { Ok(_) => println!("message sent to client {}", id), Err(e) => {