Skip to content

Commit

Permalink
cxx-qt-gen: add support for &str 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 be80f29 commit a1403ad
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions crates/cxx-qt-gen/src/generator/cpp/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ fn to_cpp_string(ty: &Type, cxx_names_map: &BTreeMap<String, String>) -> Result<
is_const = is_const,
ty = to_cpp_string(elem, cxx_names_map)?
)),
// str is a special type only available as a reference
// We need to map &str to rust::Str
// Note that CXX does not support &mut str
Type::Path(ty_path) if ty_path.path.is_ident("str") => Ok("::rust::Str".to_owned()),
// Other types pass through as normal
_others => Ok(format!(
"{is_const}{ty}&",
Expand Down Expand Up @@ -183,10 +187,7 @@ fn possible_built_in(ty: &str) -> String {
"f64" => "double",
"CxxString" => "::std::string",
"String" => "::rust::String",
// TODO: handle reference
// TODO: handle pointer
// TODO: need to handle Type::Reference for &str ?
"str" => "::rust::Str",
others => others,
}
.to_owned()
Expand All @@ -204,7 +205,6 @@ fn possible_built_in_template_base(ty: &str) -> String {
"SharedPtr" => "::std::shared_ptr",
"WeakPtr" => "::std::weak_ptr",
"CxxVector" => "::std::vector",
// TODO: handle Slice
// TODO: handle Fn pointer
// TODO: handle Array
others => others,
Expand Down Expand Up @@ -406,4 +406,22 @@ mod tests {
"::rust::Slice<::std::int32_t>"
);
}

#[test]
fn test_to_cpp_string_str() {
let ty = tokens_to_syn(quote! { &str });
assert_eq!(
to_cpp_string(&ty, &cxx_names_map_default()).unwrap(),
"::rust::Str"
);
}

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

0 comments on commit a1403ad

Please sign in to comment.