cbindgen
cbindgen copied to clipboard
Support for constant byte literal
The code contains a TODO for adding byte string support.
Is it on the roadmap and/or what would a preferred implementation look like?
My use case looks something like this:
pub const GET_INFO_FN_NAME: &'static u8 = b"get_info\0";
but this would also work:
pub const GET_INFO_FN_NAME: *const u8 = b"get_info\0".as_ptr();
Yeah, this shouldn't be terrible to add I think, wanna give it a shot? I'm happy to help out.
Seems like generating a const uint8_t[] should be the most straight-forward implementation unless I'm missing something.
Yes, I can start looking into it. It might take me a while, because I also have some other higher priority tasks for my open-source project.
W.r.t. design, in Rust one might write the following:
pub const NAME: &[u8] = b"test\0";
For which the equivalent, depending on chosen style and lang version, could be:
constexpr char NAME[] = "test\0";
You might already notice that there is a type difference u8 vs c_char. How do we want to handle that? Do we parse for the byte string literal pattern and assume it's intended to be a c_char?
Strings are implicitly null-terminated in C/C++, so you don't need the "test\0" there, just "test" would do in that case.
But I think you really need a const uint8_t NAME[] = [x, y, z]; when generating code. Otherwise you can't represent stuff like b"not null terminated", for example.
I am trying to implementing c runtime in rust , how to export string literal properly.
Please use this to reproduce https://github.com/rulibc/rulibc/tree/cbindgen
The branch cbindgen