[英]How do I create a Rust function or macro for debugging that gets optimized away in a release build?
In a debug build, I want to check for OpenGL errors after almost every OpenGL call to ease debugging. 在调试版本中,我想在几乎每个OpenGL调用之后检查OpenGL错误,以简化调试。 As this is a costly operation, I don't want to do it in a release build.
由于这是一项昂贵的操作,因此我不想在发行版本中进行。 Right now I'm using functions like:
现在我正在使用类似的功能:
pub fn debug_panic_on_errors() {
if cfg!(debug_assertions) {
get_errors().unwrap();
}
}
Am I correct in assuming that this method will always be optimized away completely? 我是否可以始终完全优化此方法,这是否正确? Is there a better, more future-proof way?
有没有更好,更适应未来的方法?
In release mode the function will be expanded to if false { … }
which is very trivial to optimize away, so yes you could just use it as-is. 在发布模式下,该功能将扩展为
if false { … }
,这对于优化起来非常简单,因此是的,您可以按原样使用它。
If you are being paranoid you could #[cfg]
two functions like 如果您偏执狂,可以
#[cfg]
两个函数,例如
#[cfg(debug_assertions)]
pub fn debug_panic_on_errors() {
get_errors().unwrap();
}
#[cfg(not(debug_assertions))]
pub fn debug_panic_on_errors() {
}
so that the outcome is chosen during parsing, to ensure we don't rely on the optimizer. 以便在解析过程中选择结果,以确保我们不依赖优化器。 But I don't really recommend this...
但是我真的不推荐这个...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.