jpernst/rental

No easy to find documentation for structs with 3+ items

spease opened this issue · 1 comments

In looking through the documentation today, I didn't see any examples that specified how to access the middle item of a struct (including mutably). The examples for two-item structs don't exactly carry over here.

My idea here is that it would be easier if each item was borrowed via a function that corresponded to the item's name. Eg

self.item2_mut().do_something()

If this introduces lifetime problems, then I'd go with rent_*:

self.rent_item2_mut(|item2| item2.do_something())

This would make it easy to remember, as the 'rules' for figuring out the function name for a given item would be simple.

The rent_all suite of methods is what you want to access fields of a struct other than the suffix. Note, however, that it is not possible to mutably access the middle field of a struct, because a reference is currently held to is by the subsequent field, and this would violate rust's aliasing rules.

As for generating methods for each field, I considered it but ultimately opted not to since I wanted to keep the API for any rental struct identical in the hopes that it could eventually be expressed as a trait. I'm open to reconsidering this in a future version of rental, but honestly I hope there are no more future versions before the language just fixes this problem.