Procházet zdrojové kódy

use `HashMap::entry()`

Justin Tracey před 1 rokem
rodič
revize
05b5a489a2
2 změnil soubory, kde provedl 32 přidání a 38 odebrání
  1. 29 31
      src/bin/mgen-peer.rs
  2. 3 7
      src/bin/mgen-server.rs

+ 29 - 31
src/bin/mgen-peer.rs

@@ -338,37 +338,35 @@ fn process_config(
             );
 
         for recipient in conversation.recipients.iter() {
-            let state_to_writer = if !recipient_map.contains_key(recipient) {
-                let (state_to_writer, writer_from_state) = mpsc::unbounded_channel();
-                let mut reader_to_states = HashMap::new();
-                reader_to_states.insert(conversation.group.clone(), reader_to_state.clone());
-                let address = hosts_map
-                    .get(recipient.as_str())
-                    .unwrap_or_else(|| panic!("recipient not in hosts file: {}", recipient));
-                let str_params = SocksParams {
-                    socks: config.socks.clone(),
-                    target: address.to_string(),
-                    user: config.user.clone(),
-                    recipient: recipient.clone(),
-                };
-                let for_io = ForIoThreads {
-                    state_to_writer: state_to_writer.clone(),
-                    writer_from_state,
-                    reader_to_states,
-                    str_params,
-                    retry: conversation.retry,
-                };
-                recipient_map.insert(recipient.clone(), for_io);
-                state_to_writer
-            } else {
-                let for_io = recipient_map.get_mut(recipient).unwrap();
-                if !for_io.reader_to_states.contains_key(&conversation.group) {
-                    for_io
-                        .reader_to_states
-                        .insert(conversation.group.clone(), reader_to_state.clone());
-                }
-                for_io.state_to_writer.clone()
-            };
+            let for_io = recipient_map
+                .entry(recipient.to_string())
+                .and_modify(|e| {
+                    e.reader_to_states
+                        .entry(conversation.group.clone())
+                        .or_insert_with(|| reader_to_state.clone());
+                })
+                .or_insert_with(|| {
+                    let (state_to_writer, writer_from_state) = mpsc::unbounded_channel();
+                    let mut reader_to_states = HashMap::new();
+                    reader_to_states.insert(conversation.group.clone(), reader_to_state.clone());
+                    let address = hosts_map
+                        .get(recipient.as_str())
+                        .unwrap_or_else(|| panic!("recipient not in hosts file: {}", recipient));
+                    let str_params = SocksParams {
+                        socks: config.socks.clone(),
+                        target: address.to_string(),
+                        user: config.user.clone(),
+                        recipient: recipient.clone(),
+                    };
+                    ForIoThreads {
+                        state_to_writer,
+                        writer_from_state,
+                        reader_to_states,
+                        str_params,
+                        retry: conversation.retry,
+                    }
+                });
+            let state_to_writer = for_io.state_to_writer.clone();
             conversation_recipient_map.insert(
                 recipient.clone(),
                 StateToWriter {

+ 3 - 7
src/bin/mgen-server.rs

@@ -73,13 +73,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
             // message channel, for sending messages between threads
             let (msg_snd, msg_rcv) = mpsc::unbounded_channel::<Arc<SerializedMessage>>();
 
-            let group_snds = if let Some(table) = snd_db.get(&handshake.group) {
-                table
-            } else {
-                let table = Arc::new(RwLock::new(HashMap::new()));
-                snd_db.insert(handshake.group.clone(), table);
-                &snd_db[&handshake.group]
-            };
+            let group_snds = snd_db
+                .entry(handshake.group.clone())
+                .or_insert_with(|| Arc::new(RwLock::new(HashMap::new())));
             group_snds
                 .write()
                 .await