The main challenge here is the use of a double RefCell. With a single RefCell, it would be possible to implement a custom iterator type, but even that is not possible with a double RefCell.
How would it work with a single RefCell? It still feels like there would be some self-referencing inside the object (assuming I don't want to recreate the actual iterator, wrapped in the custom iterator object, on every call of next)
Hmm, with a bit more thought, it's not possible with a single either, but with a single you can make an MyIntoIter and implement IntoIterator on &MyIntoIter, and have that return an iterator. With a double RefCell, you would need yet another step of conversion to make it possible.
On a different tack, what do you think about generalizing the "TrashBag" into a single object that could be used to collect temporaries that needed to exist to support extant borrows, but the code was otherwise finished with them.
I'm remembering ObjectiveC had an object called an AutoReleasePool, that served a similar purpose. At the end of a scope, all temporaries that were still being referenced were pushed into the autorelease pool, to be dropped later.
The Rust version in my mind would be a bit more manual, as there might be some runtime overhead, such as needing such objects to be heap-allocated, in order to outlast the stack frame. (or allocated in the stack frame where the trash_pool was created)
I think the internals would need to be unsafe (to get around the internal self referencing) but I think it could be implemented with a safe interface.
Are you aware of something like this that's already been done?
It's a placeholder for either a ref to a much bigger struct that's too expensive to copy willy-nilly, or a mutable ref. You're right that a const ref to u32 is pointless as the ref itself is 64 bits on most systems so there's no point in referencing it.
Thanks for telling me about the rental crate! I didn't realize it existed.