I'm trying to create a HashMap
that has tuple keys, where some of the tuple entries can be looked up by reference. I've seen mentioned before that borrow::Cow
could be used to solve this problem. However, it seems to cause some issue I'm having difficulty understanding.
Here's a (very) reduced form of my problem:
use std::collections::HashMap;
use std::borrow::Cow;
struct Table {
map: HashMap<(Cow<'static, str>, Cow<'static, str>), String>,
}
impl Table {
fn get_chars<'a, 'b>(&'a self, first: &'b str, second: &'b str) -> &'a str {
let key: (Cow<'b, str>, Cow<'b, str>) = (Cow::Borrowed(first), Cow::Borrowed(second));
let m: &'a HashMap<(Cow<'static, str>, Cow<'static, str>), String> = &self.map;
let s: &'a String = HashMap::get(m, &key).unwrap();
return &s;
}
}
The issue I have is that borrowck is convinced that s
cannot have lifetime 'a
, since the key has lifetime 'b
. I don't know why this is the case, since m
clearly has lifetime 'a
. I might expect some error due to the Cow
lifetimes not matching, but the error messages on nightly and stable don't mention it at all.
Is there a way of working around this problem? Am I missing something?
Alternatively, is it possible to create a type like Cow
that doesn't have this problem?