I'm making a struct that holds a
std::cell::Ref to a data structure. This struct will then implement
Iterator and return references to the elements contained in the data structure.
The usage of
Ref is a constraint that other parts of my codebase require. To make this thread easier to reason about, I will skim over these details and focus on the particular issue.
I've tried a couple of solutions but I just cannot get it to compile.
Here's a minimal example: Rust Playground
As I understand it: I cannot return references from
next that live as long as the iterator because they cannot outlive the reference
&mut self from which they are created.
Reasoning about it, I would say there should be some way to do this right? I mean, as long as the iterator is alive, then the
std::cell::Ref to the data structure is alive, therefore I should be able to return references that live that long.
To make things more confusing, as soon as I hold the datastructure as a
&'a T instead of a
Ref<'a ,T, everything suddenly works: Rust Playground