You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Then I continue using retain but the event sender function changed to asynchronous, and "retain" does not allow asynchronous code. Therefore I had to extract the asynchronous part:
pubasyncfnremove_inactive(&mutself,current_cutoff:DurationSinceUnixEpoch) -> usize{letmut number_of_peers_removed = 0;letmut removed_peers = Vec::new();self.peers.retain(|_key, peer| {let is_active = peer::ReadInfo::get_updated(peer) > current_cutoff;if !is_active {// Update the metadata when removing a peer.if peer.is_seeder(){self.metadata.complete -= 1;}else{self.metadata.incomplete -= 1;}
number_of_peers_removed += 1;ifletSome(_event_sender) = self.event_sender.as_deref(){// Events can not be trigger here because retain does not allow// async closures.
removed_peers.push(*peer.clone());}}
is_active
});ifletSome(event_sender) = self.event_sender.as_deref(){for peer in&removed_peers {
event_sender
.send(Event::PeerRemoved{info_hash:self.info_hash,peer:*peer,}).await;}}
number_of_peers_removed
}
The problem is that after that change you need two loops:
One to delete the peers
Another to send the events
In the current version, we have one loop to get the list of removed and another loop to remove them without "retain".
This solution is worse because:
It requires two loops.
Depending on the implementation, we must store a temporary list of inactive peers or peers pending removal. It can be a long list.
I was [researching alternatives](https://github.com/josecelano/learning-
rust/issues/3). It seems we can use a simple "while" to implement a "retain" if you need async code in the condition. I have not tried it.
Relates to: josecelano/learning-rust#3
Some days ago, I refactored the method:
I made some changes, but there is one change relevant to this issue. It was using the "retain" collection method, and it's not using it anymore.
This is the previous version where it was still using the
peers.retain
:Then I continue using
retain
but the event sender function changed to asynchronous, and "retain" does not allow asynchronous code. Therefore I had to extract the asynchronous part:The problem is that after that change you need two loops:
In the current version, we have one loop to get the list of removed and another loop to remove them without "retain".
This solution is worse because:
I was [researching alternatives](https://github.com/josecelano/learning-
rust/issues/3). It seems we can use a simple "while" to implement a "retain" if you need async code in the condition. I have not tried it.
cc @da2ce7
The text was updated successfully, but these errors were encountered: