The*_*man 2 ffi signedness rust
我正在ResumeThread使用winapi crate从 Rust调用WinAPI 函数。
文档说:
如果函数成功,则返回值是线程先前的挂起计数。
如果函数失败,则返回值是 (DWORD) -1。
如何有效地检查是否有错误?
在 C 中:
if (ResumeThread(hMyThread) == (DWORD) -1) {
// There was an error....
}
Run Code Online (Sandbox Code Playgroud)
在 Rust 中:
unsafe {
if ResumeThread(my_thread) == -1 {
// There was an error....
}
}
Run Code Online (Sandbox Code Playgroud)
unsafe {
if ResumeThread(my_thread) == -1 {
// There was an error....
}
}
Run Code Online (Sandbox Code Playgroud)
我理解错误;但是在语义上与 C 代码相同的最佳方式是什么?对照std::u32::MAX?
在 C 中,(type) expression称为类型转换。在 Rust 中,您可以使用as关键字执行类型转换。我们还给文字一个显式类型:
if ResumeThread(my_thread) == -1i32 as u32 {
// There was an error....
}
Run Code Online (Sandbox Code Playgroud)
我个人会使用std::u32::MAX,可能会重命名,因为它们具有相同的值:
use std::u32::MAX as ERROR_VAL;
if ResumeThread(my_thread) == ERROR_VAL {
// There was an error....
}
Run Code Online (Sandbox Code Playgroud)
也可以看看:
| 归档时间: |
|
| 查看次数: |
210 次 |
| 最近记录: |