I have a structure that owns a HashMap<String, String> ,
struct Test { data: HashMap<String, String>, }
I am trying to implement the Index attribute for this type to match with the implementation of the hashmap Index (there is different logic, so I cannot expose the hash map).
This works if I just get a reference to the value in 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> from it, for example 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 leads to:
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 ". Now 'a is the lifetime of Test , so I want (I think) where 'Self: 'a (so self lives at least as long as 'a ). I can't seem to figure this out for Index impl? I have tried some things with the addition of PhantomData to my Test . But I'm not going anywhere. Any suggestions?
source share