Hi folks, this may seem like kind of a big, odd and specific topic, but it goes much deeper into the compiler than I first thought so I think the internals forum is still the better place, but be free to correct me :>
This year, I've tried to wrap my head around how some could make a zero-cost but really low-level framework for modelling UI. The goal here was to be modular and give the feeling of writing component functions similar to React etc. but actually having code generated that resembles low level structs with setters that call other effects etc.
This means I want to write smth like let count = signal(0);
but letting the compiler know that this widget is exactly (i32,)
big and that the value can be stored in that thing on the stack, so no hidden allocations.
After many experiments, I've found a "solution" which uses name shadowing of a &mut tuple, which uses the type inference to resolve that tuple. An example of how this looks can be found here.
A problem with that approach is that it's pretty fragile, I have to shadow the store
on every statement that needs it and can't use a decl macro because of hygiene. Proc macros are slow, and when I would want to pass the store to another function instead of just storing something, the macro wouldn't cover it. Also, this whole construct breaks at the point where I use another function instead of closures, as I can't just specify store: Store<_>
as an argument, but need to name it, which makes the whole type inference thing impossible.
So, after this wall of text, sorry for that, why what do I want/need?
The thing that made me create this topic was that even in this narrow use-case of a library, I already had found use twice, one time for storing values, and one time for creating a chain of callbacks on compile time. So I wonder if there are more use-cases out there. If so, I would like to ask if we could get language support for those compile-time self-modifying/accumulating types? Or maybe I am overseeing another way of doing something like that which doesn't break on the first normal function declaration?
Also sorry for not proposing something directly after such a long post, but I feel like I don't know enough of the internals of the compiler to propose anything good enough for rust.
Thanks a lot and have a great day!