1

I have a C header file which contains string constants

#define SOLCLIENT_SESSION_PROP_USERNAME "SESSION_USERNAME"

and bindgen translates them to

pub const SOLCLIENT_SESSION_PROP_USERNAME: &'static [u8; 17usize] = b"SESSION_USERNAME\0";

(1) How will bindgen behave when C char is wider that 8 bits. Will it generate u16 array?

(2) If so how to consume the translated constants so my Rust code works both on systems where char is 8 bits and 16 bits. Currently I use std::ffi::CStr::from_bytes_with_nul() followed by .as_ptr() to convert these constants to *const c_char but that won't work because from_bytes_with_nul expects u8 slice.

Radek Micek
  • 447
  • 2
  • 9

1 Answers1

1

https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#char states: (emphasis added)

Note: Rust char type is not layout compatible with C / C++ char types. The C / C++ char types correspond to either Rust's i8 or u8 types on all currently supported platforms, depending on their signedness. Rust does not support C platforms in which C char is not 8-bit wide.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45