#static #reference #safely #no-alloc #promote #argument #short-lived

no-std static-on-stack

Tool to safely promote short-lived references to 'static for the duration of a non-terminating function

1 unstable release

0.1.0 Oct 17, 2023

#2 in #promote

MIT/Apache

5KB

static-on-stack

Safely wrap the promotion of a short-lived reference to a 'static reference, under the condition that it is passed to a function that never terminates.

See [promote_to_static()] for both how to use it and why it is assumed to be sound.


lib.rs:

Safely wrap the promotion of a short-lived reference to a 'static reference, under the condition that it is passed to a function that never terminates.

See [promote_to_static()] for both how to use it and why it is assumed to be sound. Execute the function f, and pass it &T promoted to be a &'static T.

How this is sound

The precondition for this to be sound is that the function not only never terminates, but that any panic flying out of the function causes an immediate abort of the program. A drop guard is in place around the function's execution that makes any panic a double panic.

As per the Drop documentation a double panic "will likely abort the program" (i.e. it is not guaranteed), an extra panic guard is in place that runs an infinite loop on drop. That is not very pretty, but it will only even make it into optimized code if there is any way in which the double panic does not cause an abort, in which case it does serve its critical role of ensuring that the lifetime of the original argument still does not end.

No runtime deps