I'm trying to mess with the Query system of Bevy, and I'm running into lifetime issue that I've tried to simplify and reproduce here. This is a rather contrived example, but I think accurately reproduces the error I'm getting.
Can anybody give some pointers on this?
trait Fetch<'a> {
type Item;
fn get(strings_from_world: &'a Vec<String>, index: usize) -> Self::Item;
}
struct Data<'world, F> {
strings_from_world: &'world Vec<String>,
_marker: core::marker::PhantomData<F>
}
impl<'world, F: Fetch<'world>> Data<'world, F> {
fn iter<'data>(&'data mut self) -> DataIter<'world, 'data, F> {
DataIter {
data: self,
index: 0,
}
}
}
struct DataIter<'data, 'world, F> {
data: &'data Data<'world, F>,
index: usize,
}
impl<'data, 'world, F: Fetch<'world>> Iterator for DataIter<'data, 'world, F> {
type Item = <F as Fetch<'world>>::Item;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
impl<'data, 'world, F: Fetch<'world>> IntoIterator for &'data mut Data<'world, F> {
type IntoIter = DataIter<'data, 'world, F>;
type Item = <F as Fetch<'world>>::Item;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
fn main() {
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/main.rs:38:14
|
38 | self.iter()
| ^^^^
|
note: first, the lifetime cannot outlive the lifetime `'data` as defined on the impl at 33:6...
--> src/main.rs:33:6
|
33 | impl<'data, 'world, F: Fetch<'world>> IntoIterator for &'data mut Data<'world, F> {
| ^^^^^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:38:9
|
38 | self.iter()
| ^^^^
note: but, the lifetime must be valid for the lifetime `'world` as defined on the impl at 33:13...
--> src/main.rs:33:13
|
33 | impl<'data, 'world, F: Fetch<'world>> IntoIterator for &'data mut Data<'world, F> {
| ^^^^^^
note: ...so that the types are compatible
--> src/main.rs:37:42
|
37 | fn into_iter(self) -> Self::IntoIter {
| __________________________________________^
38 | | self.iter()
39 | | }
| |_____^
= note: expected `std::iter::IntoIterator`
found `std::iter::IntoIterator`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.