Crates.io | abibool |
lib.rs | abibool |
version | 0.5.3 |
source | src |
created_at | 2020-11-29 13:38:02.766607 |
updated_at | 2022-08-21 23:18:40.213692 |
description | C ABI compatible boolean types |
homepage | |
repository | https://github.com/MaulingMonkey/abibool |
max_upload_size | |
id | 317757 |
size | 29,885 |
For most sane rust APIs, you should prefer bool in your interfaces and simply convert between types. However, bool isn't legal for all bit patterns, making it unusable for most FFI without conversions. For simple FFI, this isn't a problem, but C APIs writing arrays of BOOL or BOOLEAN, or structures containing these types, become problematic and require allocations and copies to avoid undefined behavior. Alternatively, you could just use integer types, that can obfuscate intent and result in bugs if multiple truthy-but-different value are directly compared when you expect boolean logic.
Note that this is incredibly system specific.
E.g. BOOL for windows is 4 bytes, but for OS X it's 1 byte... probably.
When using abibool to write FFI crates, you may wish to cc
a bunch of static_assert
s
in a build script to validate the size of your underlying C types.
C / C++ type | abibool type | notes |
---|---|---|
bool (C++) |
varies | Often 1 byte, but sometimes 4 or worse |
_Bool (C99 / stdbool.h) |
varies | Often 1 byte, but sometimes 4 or worse |
BOOLEAN (Win32) |
b8 / bool8 | |
BOOL (Win32) |
b32 / bool32 | |
BOOL (OS X / objc.h) |
b8 / bool8 ? | Typically signed char , but sometimes bool or unsigned char |
jboolean (Java / JNI) |
b8 / bool8 |
BOOL / bool / Boolean / NSCFBoolean - Objective C truthy types
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.