简体   繁体   English

Rust暴露调用堆栈深度?

[英]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? 是否可以使用稳定的Rust确定当前的调用堆栈深度?

The only options I could find are: 我能找到的唯一选择是:

  1. Following the stack in ASM, but it requires unstable and is not portable. 在ASM中堆栈之后,但它需要不稳定且不可移植。
  2. Using a GNU libc extension (backtrace). 使用GNU libc扩展(回溯)。 However, backtrace is too wasteful and also not standard. 然而,回溯太浪费,也不标准。

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. 该库支持Linux,Windows和macOS目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 unsafe Rust 中的堆栈引用,但确保不安全不泄漏堆栈? - Stack of references in unsafe Rust, but ensuring that the unsafeness does not leak out of the stack? 使用节点的深度优先搜索给出RangeError:超出最大调用堆栈大小 - Depth first search using node gives, RangeError: Maximum call stack size exceeded Go有“无限调用堆栈”等价吗? - Does Go have an “infinite call stack” equivalent? 为什么递归返回堆栈中的第一个调用而不是最后一个调用? - Why does recursion return the first call in the stack and not the last? 什么时候在 Javascript 中设置调用堆栈? - When does the call stack get set up in Javascript? 在递归调用期间,方法的参数如何存储在堆栈中? - How does parameters of a method get stored in stack during a recursive call? 为什么调用堆栈在返回或存储时不同 - Why does a call stack differs when returning or storing 在深度优先搜索中实现显式堆栈 - Implementation of an explicit stack in a depth first search 是否可以可靠地跟踪堆栈高度/递归深度? - Is it possible to track the stack height / recursion depth reliably? 堆栈溢出二进制搜索树计算深度 - stack overflow binary search tree calculating depth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM