rust-bindgen
rust-bindgen copied to clipboard
literal string definitons, their translation to charakter arrays and `*const c_char` compatibility
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.
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"
Err, this line I mean:
https://github.com/rust-lang-nursery/rust-bindgen/blob/35a349dd76cb5ba9470de30bb2eb51ea152a9640/src/codegen/mod.rs#L538
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.
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