Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Rust expose call stack depth?

Background information: I have a function that calls itself both directly and indirectly through other functions, and would like a cheap way to fail gracefully should the program run out of call stack. I could count calls manually, but I would like a more elegant and robust way of achieving this.

Is it possible to determine current call stack depth using stable Rust?

The only options I could find are:

  1. Following the stack in ASM, but it requires unstable and is not portable.
  2. Using a GNU libc extension (backtrace). However, backtrace is too wasteful and also not standard.
like image 248
Doe Avatar asked Sep 02 '17 07:09

Doe


1 Answers

The stacker crate seems to provide the functionality you seek:

  • A function to guarantee the amount of available stack
  • A function to query the amount of remaining stack, which you could use to bail before the program actually stack-overflows

The library supports the Linux, Windows and macOS targets.

like image 145
mcarton Avatar answered Sep 27 '22 20:09

mcarton