Crate static_on_stack

Source
Expand description

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.

Functions§

promote_to_static

Type Aliases§

Never
An alias for the ! type, accessed through trait trickery originally described by SkiFire13. It is present only to allow expressing a FnOnce(...) -> ! in the argument requirement of promote_to_static.