cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Support for constant byte literal

Open Wodann opened this issue 5 years ago • 5 comments

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();

Wodann avatar Jul 19 '20 22:07 Wodann

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.

emilio avatar Jul 31 '20 10:07 emilio

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?

Wodann avatar Aug 05 '20 08:08 Wodann

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.

emilio avatar Aug 05 '20 12:08 emilio

I am trying to implementing c runtime in rust , how to export string literal properly.

lygstate avatar Oct 13 '20 10:10 lygstate

Please use this to reproduce https://github.com/rulibc/rulibc/tree/cbindgen

The branch cbindgen

lygstate avatar Oct 13 '20 10:10 lygstate