From ed8d4b5a8b4f6062517e1098cc61f0d6172620cd Mon Sep 17 00:00:00 2001 From: Bart Willems Date: Sun, 5 Apr 2020 22:30:15 +0200 Subject: [PATCH] removed unnecessary unwraps from websocket-chat Signed-off-by: Bart Willems --- websocket-chat/src/server.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/websocket-chat/src/server.rs b/websocket-chat/src/server.rs index 7a9c4c2a..821e0185 100644 --- a/websocket-chat/src/server.rs +++ b/websocket-chat/src/server.rs @@ -117,7 +117,10 @@ impl Handler for ChatServer { self.sessions.insert(id, msg.addr); // auto join session to Main room - self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id); + self.rooms + .entry("Main".to_owned()) + .or_insert(HashSet::new()) + .insert(id); // send id back id @@ -193,10 +196,11 @@ impl Handler for ChatServer { self.send_message(&room, "Someone disconnected", 0); } - if self.rooms.get_mut(&name).is_none() { - self.rooms.insert(name.clone(), HashSet::new()); - } + self.rooms + .entry(name.clone()) + .or_insert(HashSet::new()) + .insert(id); + self.send_message(&name, "Someone connected", id); - self.rooms.get_mut(&name).unwrap().insert(id); } }