Should (array) view be an internal type?

The biggest flaw with proposals to make indexing return a custom view type, as I understand it, is actually quite simple and fundamental: thing[index] isn't actually sugar for calling Index::index! It's actually quite complicated and very directly coupled to references and autoref rules, in that roughly:

  • thing[index] is typed as a place (rvalue) of type Index::Output.
  • If the place is used by mutable reference, either via autoref rules or directly taking &mut of the place,
    • IndexMut::index_mut is called.
  • If the place is used by shared reference, either via autoref rules or directly taking & of the place,
    • Index::index is called.
  • If the place is not autoref'd or explicitly re-referenced,
    • Index::index is called and immediately dereferenced by the compiler, initiating an (attempt to) copy.
  • (I have no idea how it interacts with ptr::addr_of[_mut]!.)

Any proposal to hook thing[index] syntax to return a GAT has to resolve how closely coupled it currently is to references. Note that returning custom DSTs suffer no such issue, as they work normally with composition via reference.

2 Likes