I'm dealing with some FFI structs that basically implement the following:
trait LinkedIter {
fn next_mut(&mut self) -> Option<&mut Self>;
}
Is it possible to build something that implements Iterator
on top of this?
I'm dealing with some FFI structs that basically implement the following:
trait LinkedIter {
fn next_mut(&mut self) -> Option<&mut Self>;
}
Is it possible to build something that implements Iterator
on top of this?
It depends on what exactly you mean by "something which implements Iterator"... but probably not. In your LinkedIter, the lifetime of the return value is tied to the lifetime of the iterator itself. The definition of Iterator doesn't allow this; the lifetime of the items produced by an iterator have to be independent of the lifetime of the iterator itself.
Another way to look at it: the definition of Iterator allows you to write the following.
fn collect_mut<'a, T, I: Iterator<Item=&'a mut T>>(i: I) -> Vec<&'a mut T> {
i.collect()
}
Thanks. That does concisely demonstrate the impossibility of this