以无视HashMap值的通用方式公开HashMap [英] Expose a HashMap in a generic way that disregards the HashMap value
问题描述
我有不同的结构,它们都包含一个以String
为键的HashMap
,但是具有不同的值类型.例如,一个结构的成员类型为HashMap<String, String>
,另一个结构的成员类型为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);
}
具有关联的类型
下面使用泛型的代码是正确的,但是考虑一下之后,我认为在此处使用关联类型可能更合适.特质应如下所示:
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
}
}
(游乐场 )
您还可以查看_) but it doesn't work. How do I implement this?
Here is my code (which doesn't compile):
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);
}
With an associated type
The code below using a generic is correct, but after thinking about it, I think that using an associated type might be more suitable here. The trait should look like this:
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>;
}
The difference is, that you now can only implement the trait once for a struct and not multiple times, which is more correct in this case.
The implementations are roughly the same as with the generic type parameter.
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
}
}
You can also look at When is it appropriate to use an associated type versus a generic type? which explains the difference between those two pretty good.
With a generic type
This is solvable by introducing a generic type in your 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>;
}
I changed the signature to return a reference to the HashMap
. It would be possible to do it without, e.g. by using clone
or taking self
as value, instead of as reference. I also introduced a _mut
version.
The implementation is straight foreword:
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
}
}
这篇关于以无视HashMap值的通用方式公开HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!