나는 순진하게도이 작업을 수행하려고 :클로저를 녹이 사용하여 반복기를 만드는 방법은 무엇입니까?
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,));
}
}
내가 쉽게 폐쇄를 사용하여 작업에 보낼 수 반복자를 만들 수 있습니다.
그러나, 지칠대로 지친 수명 불일치 오류가 발생
<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
내가 오류를 이해하지 않습니다.
클로저는 구조체의 수명 인 평생 'a'인수를 사용해야합니다.
상태는 struct에 의해 소유되므로 수명이 a입니다.
next_.call ((& mut self.state,))을 사용하면 작업이 호출되지 않습니다. 호출의 지속 기간 동안 만 유효해야합니다. 유효한만큼 유효해야합니다.
여기에서 불일치는 next()에서의 자기 수명과 '전화 거는 것 ...'사이에 있지만 왜 그렇게되지 않을지는 알 수 없습니다.
위의 코드를 수정하는 올바른 방법은 무엇입니까?
더 좋은 방법이 있습니까?
는 놀이 틀 : 폐쇄의 수명이 유형 서명의 일부가 될 수 없습니다해야하기 때문에 http://is.gd/hyNi0S이
그리고 필요한 경우 Unfold 예제가 있습니다. http://www.reddit.com/r/rust/comments/2iunxo/yield_and_impl_iterator/cl5mdoh – ArtemGr