Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit 98ef7d7

Browse files
committed
refactor(swarmapi): prefer hash_map::Entry api
1 parent fa38c27 commit 98ef7d7

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/p2p/swarm.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,21 @@ impl NetworkBehaviour for SwarmApi {
186186
trace!("inject_connection_closed {} {:?}", peer_id, cp);
187187
let closed_addr = connection_point_addr(cp).to_owned().try_into().unwrap();
188188

189-
let became_empty = if let Some(connections) = self.connected_peers.get_mut(peer_id) {
190-
if let Some(index) = connections.iter().position(|addr| *addr == closed_addr) {
191-
connections.swap_remove(index);
189+
match self.connected_peers.entry(*peer_id) {
190+
Entry::Occupied(mut oe) => {
191+
let connections = oe.get_mut();
192+
let pos = connections.iter().position(|addr| *addr == closed_addr);
193+
194+
if let Some(pos) = pos {
195+
connections.swap_remove(pos);
196+
}
197+
198+
if connections.is_empty() {
199+
oe.remove();
200+
}
192201
}
193-
connections.is_empty()
194-
} else {
195-
false
196-
};
197-
if became_empty {
198-
self.connected_peers.remove(peer_id);
202+
203+
Entry::Vacant(_) => {}
199204
}
200205
self.connections.remove(&closed_addr);
201206

0 commit comments

Comments
 (0)