Skip to content

Commit

Permalink
cxx-qt-gen: add support for Fn pointer in C++ generation
Browse files Browse the repository at this point in the history
Related to KDAB#328
  • Loading branch information
ahayzen-kdab committed Dec 19, 2022
1 parent cc80f7d commit e561c27
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions crates/cxx-qt-gen/src/generator/cpp/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use std::collections::BTreeMap;
use syn::{
spanned::Spanned, Error, Expr, GenericArgument, Lit, PathArguments, PathSegment, Result, Type,
TypeArray, TypeReference, TypeSlice,
spanned::Spanned, Error, Expr, GenericArgument, Lit, PathArguments, PathSegment, Result,
ReturnType, Type, TypeArray, TypeBareFn, TypeReference, TypeSlice,
};

/// A helper for describing a C++ type
Expand Down Expand Up @@ -73,6 +73,24 @@ fn to_cpp_string(ty: &Type, cxx_names_map: &BTreeMap<String, String>) -> Result<
len = len
))
}
Type::BareFn(TypeBareFn { inputs, output, .. }) => {
let ret = if let ReturnType::Type(_, ty) = output {
to_cpp_string(ty, cxx_names_map)?
} else {
"void".to_owned()
};

let args = inputs
.iter()
.map(|arg| to_cpp_string(&arg.ty, cxx_names_map))
.collect::<Result<Vec<String>>>()?;

Ok(format!(
"::rust::Fn<{ret}, ({args})>",
ret = ret,
args = args.join(", ")
))
}
Type::Path(ty_path) => {
let ty_strings = ty_path
.path
Expand Down Expand Up @@ -226,7 +244,6 @@ fn possible_built_in_template_base(ty: &str) -> String {
"SharedPtr" => "::std::shared_ptr",
"WeakPtr" => "::std::weak_ptr",
"CxxVector" => "::std::vector",
// TODO: handle Fn pointer
others => others,
// TODO: what happens with Result<T> ?
}
Expand Down Expand Up @@ -465,4 +482,22 @@ mod tests {
let ty = tokens_to_syn(quote! { [i32; String] });
assert!(to_cpp_string(&ty, &cxx_names_map_default()).is_err());
}

#[test]
fn test_to_cpp_string_fn() {
let ty = tokens_to_syn(quote! { fn(i32, i32) -> bool });
assert_eq!(
to_cpp_string(&ty, &cxx_names_map_default()).unwrap(),
"::rust::Fn<bool, (::std::int32_t, ::std::int32_t)>"
);
}

#[test]
fn test_to_cpp_string_fn_void() {
let ty = tokens_to_syn(quote! { fn() });
assert_eq!(
to_cpp_string(&ty, &cxx_names_map_default()).unwrap(),
"::rust::Fn<void, ()>"
);
}
}

0 comments on commit e561c27

Please sign in to comment.