// One day...
trait Dummy {
type Var<'a>;
fn foo(&mut self) -> Self::Var<'_>;
}
If you are not in control of the trait... that's tough. All I can say is that the current signature of Dummy makes it impossible to do what you want to do.
Crap. Dummy is actually std::Iterator.. Guess I should just make Actor take ownership of T and not take a reference. Which means I'll have to copy over my data structures which kinda sucks. Could I make a super trait by extending Dummy and add the lifetime "somehow"? Then I could just have Acter impl the super trait.
Nah, I think that was my mistake for jumping to conclusions after seeing so many similar threads!
Yes, you're right, you can write an iterator that produces things bound to the lifetime of Self, as you have. All of the items produced will have the same lifetime, but for use cases such as chunks_mut, this is not an issue.
The thing that cannot be done without modifying the trait is to let each item have a different lifetime. (i.e. bound to the &mut self argument of Iterator::next). This would enable methods like windows_mut, where the returned items alias each other (and therefore each one must fall out of scope before you can call next again).
The Iterator trait is designed to allow collect(), so user of the iterator can always be able to use all of the items, at the same time, even after the iteration has finished and the Iterator has been destroyed.
Because of that it's not possible to lend any temporary value from the iterator object. It can only pass things through.
Note also that lifetimes don't do anything. They don't make the compiler emit any code. They only explain to the compiler what the code does anyway, and let the compiler check this information is consistent.