https://github.com/maulingmonkey/abibool
C ABI compatible boolean types
https://github.com/maulingmonkey/abibool
bool ffi rust winapi
Last synced: about 1 month ago
JSON representation
C ABI compatible boolean types
- Host: GitHub
- URL: https://github.com/maulingmonkey/abibool
- Owner: MaulingMonkey
- License: other
- Created: 2020-11-29T13:38:04.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-21T23:18:49.000Z (over 3 years ago)
- Last Synced: 2024-11-19T17:58:46.604Z (over 1 year ago)
- Topics: bool, ffi, rust, winapi
- Language: Rust
- Homepage: https://docs.rs/abibool
- Size: 14.6 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# abibool - C ABI compatible boolean types
[](https://github.com/MaulingMonkey/abibool)
[](https://crates.io/crates/abibool)
[](https://docs.rs/abibool)
[](https://github.com/MaulingMonkey/abibool)
[](https://github.com/MaulingMonkey/abibool/actions?query=workflow%3Arust)
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](https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#BOOL) or
[BOOLEAN](https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#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.
## Type Map
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`](https://docs.rs/cc/)
a bunch of [`static_assert`](https://en.cppreference.com/w/cpp/language/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](https://github.com/OpenTTD/OpenTTD/commit/82f7140357b8b13e5f3c2eea715af936e5debb28) or worse
| `_Bool` (C99 / stdbool.h) | *varies* | Often 1 byte, [but sometimes 4](https://stackoverflow.com/a/10630231) or worse
| [`BOOLEAN`](https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#BOOLEAN) (Win32) | [b8] / [bool8] |
| [`BOOL`](https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#BOOL) (Win32) | [b32] / [bool32] |
| [`BOOL`](https://opensource.apple.com/source/objc4/objc4-706/runtime/objc.h.auto.html) (OS X / objc.h) | [b8] / [bool8] ? | Typically `signed char`, but sometimes [bool](https://stackoverflow.com/a/544250) or [unsigned char](https://code.woboq.org/gcc/libobjc/objc/objc.h.html)
| [`jboolean`](https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html) (Java / JNI) | [b8] / [bool8] |
## References
* [BOOL / bool / Boolean / NSCFBoolean](https://nshipster.com/bool/) - Objective C truthy types
License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or )
* MIT license ([LICENSE-MIT](LICENSE-MIT) or )
at your option.
Contribution
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.
[bool]: https://doc.rust-lang.org/core/primitive.bool.html
[b8]: https://docs.rs/abibool/*/abibool/struct.bool8.html
[bool8]: https://docs.rs/abibool/*/abibool/struct.bool8.html
[b32]: https://docs.rs/abibool/*/abibool/struct.bool32.html
[bool32]: https://docs.rs/abibool/*/abibool/struct.bool32.html
[winapi]: https://docs.rs/winapi/