The compiler complains about the Item not having a lifetime, and I realize what the problem is, but I cannot seem to figure out which lifetime annotations to put in there, and where to put them.
My real-world use-case is to implement an iterator for the rosbag api, similar to the example in the linked readme but with an iterator instead of a nested loop. That library is used to read serialized messages out of large files in the rosbag format, and internally works by memory mapping subsequent chunks of that file, since the file would be too big to load it at once. Loading the next chunk unloads the current one, and that's why the references' lifetime must be limited to the current iteration. I'm aware that I could just use clone and hand out owned structs, but since a regular use case is having many messages to iterate over and only being interested in a few of them, I'd like to see whether using references instead brings a performance improvement.
This isn't possible with Iterator. The signature of Iterator::next() says that the lifetime of the returned value is independent of the borrowing of self.
It might make sense to write a custom smart pointer type that is usually a pointer to the current page, but gets replaced with an owned version of the message when the backing page gets replaced. This would involve some extra bookkeeping in the rosbag itself, but would let your iterator yield something that acts like it’s owned, but doesn’t require cloning in the common case.
hm, yes, but that would have weird corner cases. there are always multiple messages in one chunk, and if I kept smart pointers to many messages in many different chunks, the app will run out of memory even though the messages themselves would fit into ram.
Yes. As I said, this is not about any missing language feature, this is about the very signature of Iterator.
GATs merely make it possible to create a LendingIterator trait with a different signature. You still won't be able to implement the old Iterator for your type discussed above.