I'm migrating a C application to rust that makes use of setjmp/longjmp in a similar way that C++ might take advantage of a try / catch / throw exception pattern and looking for a good Rust approach.
It's an implementation of classical (Koza style) Genetic Programming whereby random programs (individuals) represented by tree shaped Lisp S-Expressions are interpreted using recursive descent. A global integer "max_duration" acts like a timer ticking off each encountered ( interpreted ) node and when it hits zero the individual's interpretation (execution) needs to abort and perform a return to the top of the recursion with one control branch that should unwind stack no matter how deep, and drop the stack content (and any orphaned artifacts) to the point at which the top level recursive call is made. As mentioned, this is very easy to do with C using setjmp / longjmp or C++ try / catch / throw. ( of course with these languages much of the cleanup work (i.e garbage collection) is up to the code author to implement.
I see there is an experimental crate for using setjmp/longjmp
However I'm dubious about the disclaimer on needing to expect undefined behavior.
Is there any way in rust to get an equivalent sort of branch that unwinds the stack like C longjmp or, similar to C++ throw?
Short of this I'm thinking I would need to do this by killing a running worker thread when the time is out, but that would be more complex, for one, because clean up logic better left in the worker thread would need to be moved to the primary thread.
If the only solution to unwinding the stack requires threading I do see this article might be useful.
Again, it does appear to be lot more involved using this approach.
It turns out my C program uses threads for each of these tree descents however the abort logic does not kill the thread. Rather the worker thread clean up after itself instead of having yet another thread clean up after killing it.
Thank you for any insights on this you may have!