I naively tried to do this:
struct Foo<'a, S: Send, T:Send> {
next_:Box<Fn<(&'a mut S,), Option<T>> + Send>,
state:S
}
impl<'a, S: Send, T: Send> Iterator<T> for Foo<'a, S, T> {
fn next(&mut self) -> Option<T> {
return self.next_.call((&mut self.state,));
}
}
To create an iterator I could send to a task easily using a closure.
However, it generates the dreaded lifetime mismatch error:
<anon>:8:33: 8:48 error: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
<anon>:8 return self.next_.call((&mut self.state,));
^~~~~~~~~~~~~~~
<anon>:7:5: 9:6 help: consider using an explicit lifetime parameter as shown: fn next(&'a mut self) -> Option<T>
<anon>:7 fn next(&mut self) -> Option<T> {
<anon>:8 return self.next_.call((&mut self.state,));
<anon>:9 }
error: aborting due to previous error
playpen: application terminated with error code 101
I don't understand the error.
The closure should take an argument with the lifetime 'a, which is the lifetime of the struct.
The state is owned by the struct, so it has a lifetime of 'a.
Using next_.call((&mut self.state,)) doesn't invoke a task; it should only be for the duration of the call(), which as far as I can tell should be valid.
So the mismatch here is between the lifetime on self in next(), and the 'a in call... but I don't see why it would not be 'a.
What's the right way to fix the code above?
Is there a better approach to take to do this?
playpen: http://is.gd/hyNi0S