简体   繁体   English

实现Index trait时无约束的生命周期错误

[英]Unconstrained lifetime error when implementing Index trait

I have a struct that owns a HashMap<String, String> , 我有一个拥有HashMap<String, String>

struct Test {
    data: HashMap<String, String>,
}

I am trying to implement the Index trait for this type to map to the Index implementation of the hashmap (there's other logic involved so I cannot expose the hashmap). 我正在尝试为此类型实现Index trait以映射到hashmap的Index实现(涉及其他逻辑,因此我无法公开hashmap)。

This works if I am just getting a reference to the value in the hashmap: 如果我只是获取对hashmap中的值的引用,这是有效的:

impl<'b> Index<&'b str> for Test {
    type Output = String;
    fn index(&self, k: &'b str) -> &String {
        self.data.get(k).unwrap()
    }
}

However, I want to get &Option<&String> out of it, like data.get() . 但是,我希望得到&Option<&String> ,就像data.get() So I tried this: 所以我尝试了这个:

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)
    }
}

This results in: 这导致:

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

I understand the " unconstrained lifetime parameter in 'a ". 我理解'a的“ unconstrained lifetime parameter ”。 Now 'a is the lifetime of Test itself, so I want (I think) where 'Self: 'a (so self lives at least as long as 'a ) . 现在'aTest本身的生命,所以我想(我认为) where 'Self: 'a (所以self生活至少和'a一样长)。 I cannot seem to figure this out for Index impl? 我似乎无法想出这个Index impl? I tried some things with adding PhantomData to my Test . 我尝试将PhantomData添加到我的Test But I am not getting anywhere. 但我没有到达任何地方。 Any suggestions? 有什么建议?

As has been pointed out in the comments, you won't be able to do exactly what you want. 正如评论中指出的那样,您将无法完全按照自己的意愿行事。 But, what it seems like you really want is to replicate HashMap 's get method. 但是,你真正想要的是复制HashMapget方法。 So I would suggest either writing your own, or implmenting Deref (and not DerefMut ) to give the struct's owner immutable access directly to the internal HashMap . 因此,我建议您自己编写或者使用Deref (而不是 DerefMut )来直接向内部HashMap提供struct的所有者不可变访问权限。 Hopefully that means the user can't mess up your struct's internal logic. 希望这意味着用户不会搞乱你的struct的内部逻辑。 Keep in mind that if you do both then Deref will not be used to called HashMap::get because Test::get will be available. 请记住,如果同时执行这两项操作,则Deref将不会用于调用HashMap::get因为Test::get将可用。

struct FooMap {
    data: HashMap<String, String>
}

Replicating get : 复制get

impl FooMap {
    pub fn get(&self, index: &str) -> Option<&String> { self.data.get(index) }
}

Using Deref : 使用Deref

impl Deref for FooMap {
    type Target = HashMap<String, String>;
    fn deref(&self) -> &Self::Target { &self.data }
}

Example code on Rust Playground Rust Playground上的示例代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用生命周期实现索引特征 - Implementing Index trait with lifetime 为具有生命周期的类型实现 Borrow trait - Implementing Borrow trait for a type with a lifetime 实现特征时如何明确指定生存期? - How can I explicitly specify a lifetime when implementing a trait? 实现迭代器特征时如何修复生命周期不匹配? - How to fix lifetime mismatch when implementing Iterator Trait? 如果生命周期未被使用,为什么在引用类型上实现特征时需要生命周期,在Rust <1.31? - Why is a lifetime needed when implementing a trait on a reference type if the lifetime is otherwise unused, in Rust < 1.31? 在实现Deref特征时无法推断生命周期参数的适当生命周期 - Cannot infer an appropriate lifetime for lifetime parameter while implementing Deref trait 在实现结构特征时,为什么会出现“缺少生存期说明符”或“类型参数数量错误”的情况? - Why do I get “missing lifetime specifier” or “wrong number of type arguments” when implementing a trait for a struct? 为具有生命周期参数的类型实现索引 - Implementing Index for type with a lifetime parameter 在另一个特征的特征实现中使用 const generic 会导致“不受约束的 const 参数”错误 - using const generic in implementation of trait for another trait causes "unconstrained const parameter" error 为实现具有关联生命周期的特征的泛型类型实现特征时的生命周期问题 - Lifetime issue while implementing a trait for a generic type which implements a trait with an associated lifetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM