cxx
cxx copied to clipboard
Add CxxChar to bind to C++ char32_t type
This makes it possible to implement functions in Rust that accept a char32_t. The CxxChar type is necessary because C++ does not guarantee that char32_t contains a valid Unicode scalar value.
There is not yet a way for C++ code to create a Rust char.
I'll update the docs after getting feedback on the design and implementation.
Partly addresses #592.
The problem with using straight u32 for the Rust type is that if we do this:
fn rust_fn(ch: u32) {}
#[cxx::bridge]
pub mod ffi {
extern "Rust" {
fn rust_fn(ch: u32);
}
}
...then the generated C++ declaration will look something like this:
void rust_fn(::std::uint32_t ch) noexcept;
...and uint32_t and char32_t are distinct C++ types, mangled differently. (Reference link for the IA64 ABI followed by gcc and clang.)
It's not specifically important to me to provide a wrapper type, but it is important to me to be able to generate C++ function signatures compatible with char32_t. If there's a better way to accomplish this, then I'm all ears.
So just to be clear, would you expect to be able to write:
fn rust_fn(ch: u32) {}
#[cxx::bridge]
pub mod ffi {
extern "Rust" {
fn rust_fn(ch: char32_t);
}
}
...and then generate a C++ declaration using char32_t?
Right. That is how the standard library and libc do C integer types.
https://doc.rust-lang.org/1.50.0/std/os/raw/index.html https://docs.rs/libc/0.2.91/libc/#types
Canonically the rust_fn implementation would be written using cxx::num::char32_t though.