Support c_char type

This commit is contained in:
David Tolnay 2020-11-25 19:47:49 -08:00
parent 248215e206
commit b3873dcd10
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 13 additions and 3 deletions

View File

@ -159,7 +159,7 @@ fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
Some(Isize) => out.builtin.rust_isize = true,
Some(CxxString) => out.include.string = true,
Some(RustString) => out.builtin.rust_string = true,
Some(Bool) | Some(F32) | Some(F64) | None => {}
Some(Bool) | Some(Char) | Some(F32) | Some(F64) | None => {}
},
Type::RustBox(_) => out.builtin.rust_box = true,
Type::RustVec(_) => out.builtin.rust_vec = true,
@ -924,6 +924,7 @@ fn write_type(out: &mut OutFile, ty: &Type) {
fn write_atom(out: &mut OutFile, atom: Atom) {
match atom {
Bool => write!(out, "bool"),
Char => write!(out, "char"),
U8 => write!(out, "uint8_t"),
U16 => write!(out, "uint16_t"),
U32 => write!(out, "uint32_t"),

View File

@ -5,6 +5,7 @@ use std::fmt::{self, Display};
#[derive(Copy, Clone, PartialEq)]
pub enum Atom {
Bool,
Char, // C char, not Rust char
U8,
U16,
U32,
@ -30,6 +31,7 @@ impl Atom {
use self::Atom::*;
match s {
"bool" => Some(Bool),
"c_char" => Some(Char),
"u8" => Some(U8),
"u16" => Some(U16),
"u32" => Some(U32),
@ -60,6 +62,7 @@ impl AsRef<str> for Atom {
use self::Atom::*;
match self {
Bool => "bool",
Char => "c_char",
U8 => "u8",
U16 => "u16",
U32 => "u32",

View File

@ -102,7 +102,7 @@ fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
| Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
| Some(RustString) => return,
Some(Bool) => { /* todo */ }
Some(Bool) | Some(Char) => { /* todo */ }
Some(CxxString) => {}
}
}
@ -140,6 +140,7 @@ fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
| Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
| Some(CxxString) => return,
Some(Char) => { /* todo */ }
Some(Bool) | Some(RustString) => {}
}
}
@ -503,6 +504,8 @@ fn describe(cx: &mut Check, ty: &Type) -> String {
"opaque Rust type".to_owned()
} else if Atom::from(&ident.rust) == Some(CxxString) {
"C++ string".to_owned()
} else if Atom::from(&ident.rust) == Some(Char) {
"C char".to_owned()
} else {
ident.rust.to_string()
}

View File

@ -11,7 +11,10 @@ impl ToTokens for Type {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Type::Ident(ident) => {
if ident.rust == CxxString {
if ident.rust == Char {
let span = ident.rust.span();
tokens.extend(quote_spanned!(span=> ::std::os::raw::));
} else if ident.rust == CxxString {
let span = ident.rust.span();
tokens.extend(quote_spanned!(span=> ::cxx::));
}