Disclaimer: post title/post are intentionally provocative; no chance this is implemented soon. Pls. feel free to ignore if you see no merit
Topic on ehanced existentials in return position has prompted me to suggest:
- make
'a T
be approximately synonymous to yet non-existant&own 'a T
- make
let 'a T = ...
mean "allocate T on the stack in the correct scope to match lifetime'a
" - allow such
'a
to optionally befn
's parameter, e.g. a lifetime from a parent stack frame - implement this via an implicit buffer pointer passed into the function, one buffer per lifetime
- implicitly compute the sizes of all such buffers for each
fn
that takes them - make callers implicitly allocate buffers of the right size on their stack
- introduce rules that ensure that each such
fn
is invoked at most once per callsight per lifetime used in this manner - mark these lifetimes explicitly on
fn
definition, say viafn foo<super 'a>() -> 'a [u8]
- the general expectation will be these lifetimes are used in some way in function return type
This is very similar to what I was suggesting on that other topic. The only novelty here is the 'a T
syntax. On the one hand it is almost exactly synonymous to &own 'a T
much discussed and implementable in user code. On the other hand I find that it can be viewed as a significant leap in intuitiveness and makes the whole construct closer mentally to returning (linked graphs of) unsized types by value.
The suggested syntax allows to "reimplement" RVO/-> impl Trait
"manually". It is a more explicit way to implement RVO/-> impl Trait
. The special case when a function returns just one value and the pointer to that value matches the buffer pointer passed in could perhaps be internally optimized by the compiler so that the pointer is not passed back again needlessly - the caller knows it already.
My thinking is that when 'a T
syntax is used with let
in fn
body it would be compiled either to use of pointers or to use of normal variables on the stack - if 'a
is local to current function invocation. On the other hand when 'a T
is used as part of a struct
or enum
it will always mean what &own 'a T
would have meant.