diff --git a/src/peering/fullmesh.rs b/src/peering/fullmesh.rs index 04318a3..cf10279 100644 --- a/src/peering/fullmesh.rs +++ b/src/peering/fullmesh.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, VecDeque}; +use std::collections::{hash_map::Entry::Vacant, HashMap, VecDeque}; use std::net::SocketAddr; use std::sync::atomic::{self, AtomicU64}; use std::sync::{Arc, RwLock}; @@ -432,10 +432,8 @@ impl FullMeshPeeringStrategy { fn on_connected(self: Arc, id: NodeID, addr: SocketAddr, is_incoming: bool) { if is_incoming { let mut known_hosts = self.known_hosts.write().unwrap(); - if !known_hosts.list.contains_key(&id) { - known_hosts - .list - .insert(id, self.new_peer(&id, addr)); + if let Vacant(entry) = known_hosts.list.entry(id) { + entry.insert(self.new_peer(&id, addr)); known_hosts.update_hash(); self.update_public_peer_list(&known_hosts); } @@ -446,12 +444,15 @@ impl FullMeshPeeringStrategy { host.state = PeerConnState::Connected; host.addr = addr; } else { - known_hosts.list.insert(id, PeerInfoInternal{ - state: PeerConnState::Connected, - addr, - last_seen: None, - ping: VecDeque::new(), - }); + known_hosts.list.insert( + id, + PeerInfoInternal { + state: PeerConnState::Connected, + addr, + last_seen: None, + ping: VecDeque::new(), + }, + ); } known_hosts.update_hash(); self.update_public_peer_list(&known_hosts);