The compiler complains about lifetimes within my mutable variant of a method, while the immutable version compiles just fine. The reproduction can be found here. Apologies if this reproduction is overwhelming, i tried to boil it down as much as I could while preserving the structure of my current code.
First some context, I’m trying to do two things;
Create an adapter around defined services through (temporary) composition, like a
ZoneAdaptercontains references to both Zone information and the
EntityService. Functionality is defined onto the Adapter and utilises both references to provide some higher-level functionality. The intention of
ZoneAdapteris to attach/manipulate Zone-information (when playing a board-game, all Entities in your hand are placed within the HandZone), without pushing that responsibility onto the
Abstract mutable and immutable references by using generics, this is what
MutSwitchdoes. The intention is to have to create only one Adapter structure for both mutable and immutable operations. These operations are then specialized in accordance with generic arguments.
This results in 1 structure and 2 implementation blocks instead of 2 structs + 2 implementation blocks.
To surface the compiler error you can uncomment lines 186-197. I’m unsure how to solve this.
I’m also interested in what the compiler sees differently between the immutable and mutable version of
ZoneAdapter::iter_zone, which causes the failed compilation.
From what I understand the lifetime of the returned mutable
Entity-reference cannot be inferred because it cannot outlive the borrow of
entity_service(r188) and transitively the borrow of
self(r187), while the
'a lifetime attached to
MutSwitch(r162) actually outlives that borrow of
Manually applying the lifetime
self and the return type(r187) does not help here. I suppose a generic lifetime parameter is necessary on one of the structures to make this work?
This is the first time I’m trying to create wrapper structures for lifetimes so something could be missing or my comprehension of it still lacks. I’d love to hear from other people who tried to tackle similar issues (and their conclusions) as well.
Thank you for reading!