I'm trying to rewrite a method to use existential types, and I am having trouble deciphering the error:
use std::result;
pub struct Child {
pub value: u32,
}
pub struct Parent {
pub name: u32,
}
impl Parent {
pub fn process(&self, _l: &Child) -> result::Result<(), ()> {
Ok(())
}
pub fn convert(&self, l: &Child) {
()
}
pub fn looper(&self, l: Vec<Child>) -> impl Iterator<Item = Result<Child, ()>> {
let x: Vec<_> = l.into_iter()
.map(|tr| self.process(&tr).map(|_| tr))
.collect(); // Calling collect() here forces all debits to complete
let y = x.into_iter().map(|d| {
d.map(|c| {
self.convert(&c);
c
})
});
y
}
}
fn main() {
let b = Parent { name: 0 };
let l = vec![Child { value: 10 }, Child { value: 20 }];
let _: Vec<Child> = b.looper(l).map(|x| x.unwrap()).collect();
}
My error message states:
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:25:35
|
25 | let y = x.into_iter().map(|d| {
| ___________________________________^
26 | | d.map(|c| {
27 | | self.convert(&c);
28 | | c
29 | | })
30 | | });
| |_________^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 20:5...
--> src/main.rs:20:5
|
20 | / pub fn looper(&self, l: Vec<Child>) -> impl Iterator<Item = Result<Child, ()>> {
21 | | let x: Vec<_> = l.into_iter()
22 | | .map(|tr| self.process(&tr).map(|_| tr))
23 | | .collect(); // Calling collect() here forces all debits to complete
... |
31 | | y
32 | | }
| |_____^
= note: ...so that the types are compatible:
expected &&Parent
found &&Parent
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that return value is valid for the call
--> src/main.rs:20:44
|
20 | pub fn looper(&self, l: Vec<Child>) -> impl Iterator<Item = Result<Child, ()>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^