@@ -22,6 +22,7 @@ use crate::topic::TopicHash;
2222use crate :: types:: { MessageId , RawGossipsubMessage } ;
2323use libp2p_core:: PeerId ;
2424use log:: { debug, trace} ;
25+ use std:: collections:: hash_map:: Entry ;
2526use std:: fmt:: Debug ;
2627use std:: {
2728 collections:: { HashMap , HashSet } ,
@@ -73,22 +74,22 @@ impl MessageCache {
7374 ///
7475 /// Returns true if the message didn't already exist in the cache.
7576 pub fn put ( & mut self , message_id : & MessageId , msg : RawGossipsubMessage ) -> bool {
76- trace ! ( "Put message {:?} in mcache" , message_id) ;
77- let cache_entry = CacheEntry {
78- mid : message_id . clone ( ) ,
79- topic : msg . topic . clone ( ) ,
80- } ;
81-
82- if self
83- . msgs
84- . insert ( message_id . clone ( ) , ( msg , HashSet :: new ( ) ) )
85- . is_none ( )
86- {
87- // Don't add duplicate entries to the cache.
88- self . history [ 0 ] . push ( cache_entry ) ;
89- return true ;
90- } else {
91- return false ;
77+ match self . msgs . entry ( message_id. clone ( ) ) {
78+ Entry :: Occupied ( _ ) => {
79+ // Don't add duplicate entries to the cache.
80+ false
81+ }
82+ Entry :: Vacant ( entry ) => {
83+ let cache_entry = CacheEntry {
84+ mid : message_id . clone ( ) ,
85+ topic : msg . topic . clone ( ) ,
86+ } ;
87+ entry . insert ( ( msg , HashSet :: default ( ) ) ) ;
88+ self . history [ 0 ] . push ( cache_entry ) ;
89+
90+ trace ! ( "Put message {:?} in mcache" , message_id ) ;
91+ true
92+ }
9293 }
9394 }
9495
0 commit comments