Jelajahi Sumber

net: change hosts from Vec to HashSet

ghassmo 3 tahun lalu
induk
melakukan
ab2fbd97d1
1 mengubah file dengan 10 tambahan dan 11 penghapusan
  1. 10 11
      src/net/hosts.rs

+ 10 - 11
src/net/hosts.rs

@@ -8,31 +8,30 @@ pub type HostsPtr = Arc<Hosts>;
 
 /// Manages a store of network addresses.
 pub struct Hosts {
-    addrs: Mutex<Vec<Url>>,
+    addrs: Mutex<FxHashSet<Url>>,
 }
 
 impl Hosts {
     /// Create a new host list.
     pub fn new() -> Arc<Self> {
-        Arc::new(Self { addrs: Mutex::new(Vec::new()) })
-    }
-
-    /// Checks if a host address is in the host list.
-    async fn contains(&self, addrs: &[Url]) -> bool {
-        let a_set: FxHashSet<_> = addrs.iter().cloned().collect();
-        self.addrs.lock().await.iter().any(|item| a_set.contains(item))
+        Arc::new(Self { addrs: Mutex::new(FxHashSet::default()) })
     }
 
     /// Add a new host to the host list.
     pub async fn store(&self, addrs: Vec<Url>) {
-        if !self.contains(&addrs).await {
-            self.addrs.lock().await.extend(addrs)
+        for addr in addrs {
+            self.addrs.lock().await.insert(addr);
         }
     }
 
     /// Return the list of hosts.
     pub async fn load_all(&self) -> Vec<Url> {
-        self.addrs.lock().await.clone()
+        self.addrs.lock().await.iter().cloned().collect()
+    }
+
+    /// Remove an Url from the list
+    pub async fn remove(&self, url: &Url) -> bool {
+        self.addrs.lock().await.remove(url)
     }
 
     /// Check if the host list is empty.