Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mash-graz opened this issue Sep 26, 2018 · 4 comments

Comments

@mash-graz
Copy link

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.

@emilio
Copy link
Contributor

emilio commented Oct 7, 2018

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
Copy link
Contributor

emilio commented Oct 7, 2018

@lucab
Copy link

lucab commented Sep 7, 2019

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.

@Dushistov
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants