Consider the following (incomplete) function signature:
unsafe fn foo<'a, T: 'a>(func: impl FnOnce() -> T + 'a) -> ...
Is there a way to (unsafely of course) transmute
the input function so, that it becomes impl FnOnce() -> S + 'static
where S is the same type as T but with S: 'static
.
I know it is possible to transmute the lifetime bounds on the closure itself by using a boxed trait (FnBox
) and then calling transmute on the box. However, this does not affect the return type (T
). As far as I understand, T: 'a
and T: 'static
are different types as far as the type system goes. So I wonder if it is even possible to express this in Rust.
I suppose the signature would have to look like this (ignoring the lifetime bounds on the closure itself):
unsafe fn<'a, T, S>(func: impl FnOnce() -> T) -> impl FnOnce() -> S
where
T: 'a,
S: 'static`
but then how do you call this function without the specification that T
and S
are identical except for their lifetime bound.
Disclaimer I am aware that tinkering with lifetime bounds is generally a bad idea, however this is for spawning threads where the lifetime restrictions are enforced by other means.