首页 > 解决方案 > 以忽略 HashMap 值的通用方式公开 HashMap

问题描述

我有不同的结构,它们都包含一个HashMapwithString作为键,但具有不同的值类型。例如,一个结构有一个 type 的成员HashMap<String, String>,另一个将有一个 type 的成员HashMap<String, u8>,依此类推。

我想定义一个可以访问这些HashMap成员并对它们执行不涉及值的通用操作的方法。例如,我想计算键的数量,删除键,检查键是否存在等。我不确定如何实现此行为。

到目前为止,我想到的最好方法是定义一个特征,该特征具有一个公开HashMap并让每个结构实现它的方法。但是我不知道如何以“忽略”值类型的方式编写此特征和方法。我尝试使用通配符 ( _) 但它不起作用。我该如何实施?

这是我的代码(无法编译):

use std::collections::HashMap;

pub trait HashMapContainer {
    fn get_hash_map(&self) -> HashMap<String, _>;
}

struct HashMapContainerImpl1 {
    map: HashMap<String, String>,
}

impl HashMapContainerImpl1 {
    pub fn new() -> HashMapContainerImpl1 {
        HashMapContainerImpl1 {
            map: HashMap::new(),
        }
    }

    fn internal_logic_on_map(&mut self) {
        //....
    }
}

impl HashMapContainer for HashMapContainerImpl1 {
    fn get_hash_map(&self) -> HashMap<String, _> {
        self.map
    }
}

struct HashMapContainerImpl2 {
    map: HashMap<String, u8>,
}

impl HashMapContainerImpl2 {
    pub fn new() -> HashMapContainerImpl2 {
        HashMapContainerImpl2 {
            map: HashMap::new(),
        }
    }

    fn internal_logic_on_map(&mut self) {
        //....
    }
}

impl HashMapContainer for HashMapContainerImpl2 {
    fn get_hash_map(&self) -> HashMap<String, _> {
        self.map
    }
}

fn do_generic_actions_on_map(hm_container: &HashMapContainer) {
    println!("key count: {}", hm_container.get_hash_map().len());
    println!(
        "key esists? {}",
        hm_container.get_hash_map().get("key1").is_some()
    );
    hm_container.get_hash_map().remove("key2");
}

fn main() {
    let cont1 = HashMapContainerImpl1::new();
    let cont2 = HashMapContainerImpl2::new();
    do_generic_actions_on_map(cont1);
    do_generic_actions_on_map(cont2);
}

标签: hashmaprust

解决方案


具有关联类型

下面使用泛型的代码是正确的,但想了想,我觉得这里使用关联类型可能更合适。特征应该如下所示:

pub trait HashMapContainer {
    type Value;
    fn get_hash_map(&self) -> &HashMap<String, Self::Value>;
    fn get_hash_map_mut(&mut self) -> &mut HashMap<String, Self::Value>;
}

不同之处在于,您现在只能为结构实现特征一次,而不是多次,这在这种情况下更正确。

实现与泛型类型参数大致相同。

impl HashMapContainer for HashMapContainerImpl1 {
    type Value = String;
    fn get_hash_map(&self) -> &HashMap<String, Self::Value> {
        &self.map
    }
    fn get_hash_map_mut(&mut self) -> &mut HashMap<String, Self::Value> {
        &mut self.map
    }
}

impl HashMapContainer for HashMapContainerImpl2 {
    type Value = u8;
    fn get_hash_map(&self) -> &HashMap<String, Self::Value> {
        &self.map
    }
    fn get_hash_map_mut(&mut self) -> &mut HashMap<String, Self::Value> {
        &mut self.map
    }
}

游乐场

您还可以查看何时使用关联类型与泛型类型比较合适?这很好地解释了这两者之间的区别。

使用泛型类型

HashMapContainer这可以通过在您的trait中引入泛型类型来解决。

pub trait HashMapContainer<T> {
    fn get_hash_map(&self) -> &HashMap<String, T>;
    fn get_hash_map_mut(&mut self) -> &mut HashMap<String, T>;
}

我更改了签名以返回对HashMap. 可以在没有的情况下做到这一点,例如通过使用cloneself作为价值,而不是作为参考。我还介绍了一个_mut版本。

实现是直接的前言:

impl HashMapContainer<String> for HashMapContainerImpl1 {
    fn get_hash_map(&self) -> &HashMap<String, String> {
        &self.map
    }
    fn get_hash_map_mut(&mut self) -> &mut HashMap<String, String> {
        &mut self.map
    }
}

impl HashMapContainer<u8> for HashMapContainerImpl2 {
    fn get_hash_map(&self) -> &HashMap<String, u8> {
        &self.map
    }
    fn get_hash_map_mut(&mut self) -> &mut HashMap<String, u8> {
        &mut self.map
    }
}

推荐阅读