CHashMap iter does not exist? Why?

The crate CHashMap is puzzling me. Why does it only have into_iter, but not iter or iter_mut? I just want to be able to loop through and do something like this (wherein there is dependency upon one of the T's inner fields, but not the key which maps to T within the hashmap itself)

    /// Returns a client by IP Address
    pub fn get_client_by_addr(&self, addr: &IpAddr, prefer_ipv6: bool) -> Option<ClientNetworkAccount> {
        for cnac in self.map.iter() {
            if let Some(ip) = cnac.read().nac.get_addr(prefer_ipv6) {
                if ip.eq(addr) {
                    return Some(cnac.clone())
                }
            }
        }

        None
    }

Well into_iter requires consuming the HashMap meaning noone is concurrently using it. It's probably because it is an expensive operation to iterate in a concurrent way. Perhaps you can simulate what you want using the retain method?

1 Like

Yeah that makes sense. The problem with retain is, of course, if there are a list of 100 items, and the item I'm looking for happens to be one of the first results. I can't prematurely break from iterated closures (as far as I know).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.