After having fixed my previous issue, I encountered the next one and I cannot wrap my head around how to fix this. I suspect I have to change some lifetimes, i.e. I cannot put 'a
everywhere and assume it's correct, but the error message isn't very helpful in this case. I've simplified the code as much as I could, but that didn't help me find a solution, so far.
What do I have to do to be able to loop over the references in the drop
method?
use ::std::marker::PhantomData;
trait Collection<'a, Element>
where
Self: 'a,
&'a Self: IntoIterator<Item = &'a Element>,
Element: 'a,
{
}
struct MyStruct<'a, Collection>
where
Collection: self::Collection<'a, ()>,
&'a Collection: IntoIterator<Item = &'a ()>,
{
references: Collection,
_data: PhantomData<&'a ()>,
}
impl<'a, Collection> Drop for MyStruct<'a, Collection>
where
Collection: self::Collection<'a, ()>,
&'a Collection: IntoIterator<Item = &'a ()>,
{
fn drop(&mut self) {
for reference in &self.references {}
}
}
fn main() {}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
--> src/lib.rs:26:26
|
26 | for reference in &self.references {}
| ^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 25:5...
--> src/lib.rs:25:5
|
25 | / fn drop(&mut self) {
26 | | for reference in &self.references {}
27 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/lib.rs:26:26
|
26 | for reference in &self.references {}
| ^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 20:6...
--> src/lib.rs:20:6
|
20 | impl<'a, Collection> Drop for MyStruct<'a, Collection>
| ^^
= note: ...so that the types are compatible:
expected std::iter::IntoIterator
found std::iter::IntoIterator
error: aborting due to previous error
error: could not compile `playground`.
To learn more, run the command again with --verbose.