Skip to content

Commit

Permalink
cxx-qt-gen: use qualified name for T in constructor generation
Browse files Browse the repository at this point in the history
Related to KDAB#404
  • Loading branch information
ahayzen-kdab committed Jul 26, 2023
1 parent 963eaca commit 8092596
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
27 changes: 20 additions & 7 deletions crates/cxx-qt-gen/src/generator/rust/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::collections::BTreeMap;

use crate::{
generator::{
naming::{namespace::NamespaceName, qobject::QObjectName, CombinedIdent},
rust::qobject::GeneratedRustQObjectBlocks,
utils::rust::syn_type_is_cxx_bridge_unsafe,
utils::rust::{syn_ident_cxx_bridge_to_qualified_impl, syn_type_is_cxx_bridge_unsafe},
},
parser::constructor::Constructor,
};

use convert_case::{Case, Casing};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use syn::{parse_quote, parse_quote_spanned, spanned::Spanned, Expr, Ident, Item, Result, Type};
use syn::{
parse_quote, parse_quote_spanned, spanned::Spanned, Expr, Ident, Item, Path, Result, Type,
};

const CONSTRUCTOR_ARGUMENTS: &str = "CxxQtConstructorArguments";
const BASE_ARGUMENTS: &str = "CxxQtConstructorBaseArguments";
Expand Down Expand Up @@ -136,6 +140,7 @@ pub fn generate(
constructors: &[Constructor],
qobject_idents: &QObjectName,
namespace: &NamespaceName,
qualified_mappings: &BTreeMap<Ident, Path>,
) -> Result<GeneratedRustQObjectBlocks> {
if constructors.is_empty() {
return Ok(generate_default_constructor(qobject_idents, namespace));
Expand All @@ -146,6 +151,8 @@ pub fn generate(

let qobject_name = &qobject_idents.cpp_class.cpp;
let qobject_name_rust = &qobject_idents.cpp_class.rust;
let qobject_name_rust_qualified =
syn_ident_cxx_bridge_to_qualified_impl(qobject_name_rust, qualified_mappings);
let qobject_name_snake = qobject_name.to_string().to_case(Case::Snake);

let rust_struct_name_rust = &qobject_idents.rust_struct.rust;
Expand Down Expand Up @@ -281,7 +288,7 @@ pub fn generate(
new_arguments,
base_arguments,
initialize_arguments
) = <#qobject_name_rust as cxx_qt::Constructor<(#(#argument_types,)*)>>
) = <#qobject_name_rust_qualified as cxx_qt::Constructor<(#(#argument_types,)*)>>
::route_arguments((#(#assign_arguments,)*));
#arguments_rust {
base: #init_base_arguments,
Expand All @@ -295,18 +302,18 @@ pub fn generate(
#[doc(hidden)]
#[allow(unused_variables)]
pub fn #new_rust(new_arguments: #new_arguments_rust) -> std::boxed::Box<#rust_struct_name_rust> {
std::boxed::Box::new(<#qobject_name_rust as cxx_qt::Constructor<(#(#argument_types,)*)>>::new((#(#extract_new_arguments,)*)))
std::boxed::Box::new(<#qobject_name_rust_qualified as cxx_qt::Constructor<(#(#argument_types,)*)>>::new((#(#extract_new_arguments,)*)))
}
},
parse_quote_spanned! {
constructor.imp.span() =>
#[doc(hidden)]
#[allow(unused_variables)]
pub fn #initialize_rust(
qobject: core::pin::Pin<&mut #qobject_name_rust>,
qobject: core::pin::Pin<&mut #qobject_name_rust_qualified>,
initialize_arguments: #initialize_arguments_rust
) {
<#qobject_name_rust as cxx_qt::Constructor<(#(#argument_types,)*)>>::initialize(
<#qobject_name_rust_qualified as cxx_qt::Constructor<(#(#argument_types,)*)>>::initialize(
qobject,
(#(#extract_initialize_arguments,)*));
}
Expand Down Expand Up @@ -340,7 +347,13 @@ mod tests {
}

fn generate_mocked(constructors: &[Constructor]) -> GeneratedRustQObjectBlocks {
generate(constructors, &mock_name(), &mock_namespace()).unwrap()
generate(
constructors,
&mock_name(),
&mock_namespace(),
&BTreeMap::<Ident, Path>::default(),
)
.unwrap()
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/cxx-qt-gen/src/generator/rust/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl GeneratedRustQObject {
&qobject.constructors,
&qobject_idents,
&namespace_idents,
qualified_mappings,
)?);

generated.blocks.append(&mut cxxqttype::generate(
Expand Down
15 changes: 9 additions & 6 deletions crates/cxx-qt-gen/test_outputs/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ pub mod cxx_qt_ffi {
#[allow(unused_variables)]
#[allow(clippy::let_unit_value)]
let (new_arguments, base_arguments, initialize_arguments) =
<MyObject as cxx_qt::Constructor<(i32, *mut QObject)>>::route_arguments((arg0, arg1));
<ffi::MyObject as cxx_qt::Constructor<(i32, *mut QObject)>>::route_arguments((
arg0, arg1,
));
CxxQtConstructorArgumentsMyObject0 {
base: CxxQtConstructorBaseArgumentsMyObject0 {
arg0: base_arguments.0,
Expand All @@ -245,17 +247,18 @@ pub mod cxx_qt_ffi {
pub fn new_rs_my_object_0(
new_arguments: CxxQtConstructorNewArgumentsMyObject0,
) -> std::boxed::Box<MyObjectRust> {
std::boxed::Box::new(<MyObject as cxx_qt::Constructor<(i32, *mut QObject)>>::new(
(new_arguments.arg0,),
))
std::boxed::Box::new(<ffi::MyObject as cxx_qt::Constructor<(
i32,
*mut QObject,
)>>::new((new_arguments.arg0,)))
}
#[doc(hidden)]
#[allow(unused_variables)]
pub fn initialize_my_object_0(
qobject: core::pin::Pin<&mut MyObject>,
qobject: core::pin::Pin<&mut ffi::MyObject>,
initialize_arguments: CxxQtConstructorInitializeArgumentsMyObject0,
) {
<MyObject as cxx_qt::Constructor<(i32, *mut QObject)>>::initialize(qobject, ());
<ffi::MyObject as cxx_qt::Constructor<(i32, *mut QObject)>>::initialize(qobject, ());
}
impl core::ops::Deref for ffi::MyObject {
type Target = MyObjectRust;
Expand Down

0 comments on commit 8092596

Please sign in to comment.