rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

literal string definitons, their translation to charakter arrays and `*const c_char` compatibility

Open mash-graz opened this issue 6 years ago • 4 comments

literal strings in C preprocessor definitions are translated to static character arrays by bindgen

e.g.

#define kOfxImageEffectPluginApi "OfxImageEffectPluginAPI"

becomes on my linux system:

pub const kOfxImageEffectPluginApi: &'static [u8; 24] = b"OfxImageEffectPluginAPI\x00"

the issue with this solution has to be seen in the choice of u8, because the affected C strings are very often used in places, where a *const c_char is expected, but c_char may be of type i8 on some systems and u8 on others. this behavior enforces a lot of unpleasant and error prone further type casts.

mash-graz avatar Sep 26 '18 00:09 mash-graz

This could be as easy as changing this line from using u8 to using raw_type(ctx, "c_char"), and addressing the relevant fallout... But I'm not sure what should happen when you have something like:

#define INVALID_UTF8 "\xf0\x28\x8c\x28"

emilio avatar Oct 07 '18 09:10 emilio

Err, this line I mean:

https://github.com/rust-lang-nursery/rust-bindgen/blob/35a349dd76cb5ba9470de30bb2eb51ea152a9640/src/codegen/mod.rs#L538

emilio avatar Oct 07 '18 09:10 emilio

On the other hand, having this as a slice of u8 makes the usage of the safe CStr constructor simpler (and portable): https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.from_bytes_with_nul.

lucab avatar Sep 07 '19 22:09 lucab

I expted that bindgen already have something like

enum HandleLiteralType {
   CCharArray,
   RustStr,
   U8Array,
}


impl Builder {

fn handle_literal_string_as(self, const_regex: &str, how_handle: HandleLiteralType) -> Self { ... }

In my case all string are ASCII strings, and it would be nice to have

const C1: &str = "...";

as result of bindgen

Dushistov avatar Mar 28 '22 10:03 Dushistov