Hi Guys,
I have this glory question, could you check if I have any chance to solve it?
I have these structs:
struct Storage<T> {
data: Vec<T>,
path: &'static str,
}
struct StorageIter<'a, T> {
data: &'a mut Vec,
path: &'static str,
}
I would like to implement Iterator for Filter<'a, T>, and in fn next(&mut self) ->Optionself::Item I would like to get a mű table reference to Filter.data vector - which is also a reference.
But i have issues, as in Iter next() function I cannot manage lifetimes, as Iterator trait has fn next(&mut self), and I cannot change it to &'a mut self.
My relevant code is here:
pub struct StorageIter<'a, T> {
data: &'a mut Vec<T>,
index: usize,
path: &'static str,
}
impl<'a, T> Iterator for StorageIter<'a, T> {
type Item = &'a mut T;
#[inline]
fn next(&mut self) -> Option<&'a mut T> {
if let Some(item) = &self.data.get_mut(self.index) {
self.index += 1;
return Some(*item);
}
None
}
}
The error message is:
&mut StorageIter<'a, T>
cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
note: ...so that reference does not outlive borrowed content
note: ...so that the expression is assignable:
expected std::option::Option<&'a mut T>
found std::option::Option<&mut T>
Do you have any advice to solve this?
Thank you in advance,
Best wishes,
Peter Mezei.