chiark / gitweb /
Clippy fix: borrow the HashMap Entry we want to insert into.
authorSimon Tatham <anakin@pobox.com>
Fri, 5 Jan 2024 23:31:21 +0000 (23:31 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 6 Jan 2024 09:18:31 +0000 (09:18 +0000)
I hadn't known about this method of HashMap at all: you can test
whether a key exists, in such a way that you also get a reference to
the slot you'd want to insert it into if it didn't. Then you don't
need the two hash lookups you would with the more obvious approach of
separately testing for contains_key and then maybe inserting.

src/file.rs

index f0beffa6b26105b9bc55bcfd28ef354f43cc9e72..39e5a60a77a7f46fa3a8fd979b401f4510e68bb3 100644 (file)
@@ -1,7 +1,7 @@
 use itertools::Itertools;
 use regex::Regex;
 use std::cmp::{min, max};
-use std::collections::{HashMap, HashSet};
+use std::collections::{HashMap, HashSet, hash_map};
 
 use super::activity_stack::{
     NonUtilityActivity, UtilityActivity, OverlayActivity,
@@ -346,7 +346,7 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
     fn ensure_item_rendered(&mut self, index: isize, w: usize) ->
         &Vec<ColouredString>
     {
-        if !self.rendered.contains_key(&index) {
+        if let hash_map::Entry::Vacant(e) = self.rendered.entry(index) {
             let mut lines = Vec::new();
 
             let highlight = match self.ui_mode {
@@ -369,7 +369,7 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
                 }
             }
 
-            self.rendered.insert(index, lines);
+            e.insert(lines);
         }
 
         self.rendered.get(&index).expect("We just made sure this was present")