我有一个拥有a的结构HashMap<String, String>,
struct Test {
data: HashMap<String, String>,
}
Run Code Online (Sandbox Code Playgroud)
我试图实现Index这种类型的特征来映射到Indexhashmap 的实现(涉及其他逻辑,所以我不能公开hashmap).
如果我只是获取对hashmap中的值的引用,这是有效的:
impl<'b> Index<&'b str> for Test {
type Output = String;
fn index(&self, k: &'b str) -> &String {
self.data.get(k).unwrap()
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想&Option<&String>摆脱它,就像data.get().所以我尝试了这个:
impl<'b, 'a> Index<&'b str> for Test {
type Output = Option<&'a String>;
fn index(&'a self, k: &'b str) -> &Option<&'a String> {
&self.data.get(k)
}
}
Run Code Online (Sandbox Code Playgroud)
这导致:
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> <anon>:8:10
|
8 | impl<'b, 'a> Index<&'b str> for Test {
| ^^ unconstrained lifetime parameter
Run Code Online (Sandbox Code Playgroud)
我理解" unconstrained lifetime parameter在'a".现在'a是Test它自己的生命,所以我想(我认为)where 'Self: 'a(所以self至少生活在一起'a).我似乎无法想出这个问题Index吗?我尝试了一些事情添加PhantomData到我的Test.但我没有到达任何地方.有什么建议?
正如评论中所指出的,您将无法完全按照您的意愿去做。但是,看起来您真正想要的是复制HashMap的get方法。因此,我建议要么编写您自己的,要么实现Deref(而不是 DerefMut)为结构的所有者提供直接对内部HashMap. 希望这意味着用户不能搞乱你的结构的内部逻辑。请记住,如果您同时执行这两项操作,Deref则将不会被调用,HashMap::get因为Test::get将有空。
struct FooMap {
data: HashMap<String, String>
}
Run Code Online (Sandbox Code Playgroud)
复制get:
impl FooMap {
pub fn get(&self, index: &str) -> Option<&String> { self.data.get(index) }
}
Run Code Online (Sandbox Code Playgroud)
使用Deref:
impl Deref for FooMap {
type Target = HashMap<String, String>;
fn deref(&self) -> &Self::Target { &self.data }
}
Run Code Online (Sandbox Code Playgroud)