Edit: My problem is resolved. TAIT RFC already discussed this and it is partially implemented in the compiler, and those unimplemented parts confused me.
I propose to add a way to unsafely erase lifetimes from any type, generically.
One long-lived example from the ecosystem is Google's yoke - Rust Yokable
trait for those purposes. They are replacing lifetimes with 'static
, but actual lifetime is tied to something like Rc
. It is their approach to generically transform Foo<'a>
to Foo<'static>
. But their approach only works for types you can slap derive macro on and it's enough for their ser/de purposes. It does not work for futures, my primary use case.
I have a need to store allocations in static
variables instead of the heap due to predictability. If you want to use heap, then Box::leak
is 100% sufficient. And for T: 'static
crates like static_cell - Rust are working fine. The problem with those for me: if you want to store non-'static data, then you'll be bitten by invariance - you can't have &'static mut Foo<'a>
, but borrow checker enforces that relation to exists and fails.
My problem is, you cannot in general case store Foo<'a>
inside static
. For "Foo<'a>" you can write static mut FOO: Foo<'static>
and then store Foo<'a>
here. But often Foo
is unnamable - for example, large future. Even TAIT would not help, because it would allow you to name Foo<'a>
, but for that unsafe trick to work you need to rewrite Foo<'a>
to Foo<'static>
, and you just cant...
I wrote that proof of concept (playground) that uses generic_const_expressions
to (1) construct type equivalent to T
by alignment and size and (2) verify that alignment and size of T
are matching to declared ones. It passes miri for a couple of tests.
If you want to provide advice or solution, please do that after confirming that it would work with my example. Thank you.
How can one approach to get this use case get supported in Rust? Specifically, ergonomics of Box
but with memory in static
. I don't think that my solution with generic_const_expressions
is viable.