Function mz_ore::stack::maybe_grow

source ·
pub fn maybe_grow<F, R>(f: F) -> R
where F: FnOnce() -> R,
Available on crate feature stack only.
Expand description

Grows the stack if necessary before invoking f.

This function is intended to be called at manually instrumented points in a program where arbitrarily deep recursion is known to happen. This function will check to see if it is within STACK_RED_ZONE bytes of the end of the stack, and if so it will allocate a new stack of at least STACK_SIZE bytes.

The closure f is guaranteed to run on a stack with at least STACK_RED_ZONE bytes, and it will be run on the current stack if there’s space available.

It is generally better to use CheckedRecursion to enforce a limit on the stack growth. Not all recursive code paths support returning errors, however, in which case unconditionally growing the stack with this function is still preferable to panicking.