I have this code:
use std::hash::Hash;
use std::rc::Rc;
struct Counter<V> {
value: V,
}
struct LFUCache<K, V> {
values: Vec<(Rc<K>, Counter<V>)>,
}
impl<K: Hash + Eq, V> IntoIterator for LFUCache<K, V> {
type Item = (Rc<K>, V);
type IntoIter = Box<dyn Iterator<Item=(Rc<K>, V)>>;
fn into_iter(self) -> Self::IntoIter {
return Box::new(self
.values
.into_iter()
.map(|(key, value_counter)| (key, value_counter.value)));
}
}
I get an error:
error[E0310]: the parameter type `K` may not live long enough
--> src/lib.rs:167:16
|
162 | impl<K: Hash + Eq, V> IntoIterator for LFUCache<K, V> {
| -- help: consider adding an explicit lifetime bound `K: 'static`...
...
167 | return Box::new(self
| ________________^
168 | | .values
169 | | .into_iter()
170 | | .map(|(key, value_counter)| (key, value_counter.value)));
| |____________________________________________________________________^
|
note: ...so that the type `std::iter::Map<std::collections::hash_map::IntoIter<std::rc::Rc<K>, ValueCounter<V>>, [closure@src/lib.rs:170:18: 170:67]>` will meet its required lifetime bounds
--> src/lib.rs:167:16
|
167 | return Box::new(self
| ________________^
168 | | .values
169 | | .into_iter()
170 | | .map(|(key, value_counter)| (key, value_counter.value)));
I'd like to express that the intent that the boxed iterator should live as long as the LFU cache. However, since there are no references, I can't get any lifetimes.
How do I fix this?