Skip to content

Commit 18a487e

Browse files
WIP Fix constructor generic args
1 parent 502a955 commit 18a487e

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

crates/cxx-qt-gen/src/generator/rust/constructor.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,19 @@ pub fn generate(
207207

208208
let rust_struct_name_rust = qobject_names.rust_struct.rust_unqualified();
209209

210-
result
211-
.cxx_qt_mod_contents
212-
.append(&mut vec![parse_quote_spanned! {
210+
for (index, constructor) in constructors.iter().enumerate() {
211+
let mut arguments = TokenStream::new();
212+
for elem in &constructor.arguments {
213+
arguments.extend(quote!(#elem,));
214+
}
215+
216+
result
217+
.cxx_qt_mod_contents
218+
.append(&mut vec![parse_quote_spanned! {
213219
qobject_name_rust.span() => // TODO! Improve this span
214-
impl ::cxx_qt::ConstructorDeclared for #qobject_name_rust_qualified {}
220+
impl ::cxx_qt::ConstructorDeclared<(#arguments) > for #qobject_name_rust_qualified {}
215221
}]);
216222

217-
for (index, constructor) in constructors.iter().enumerate() {
218223
let lifetime = constructor.lifetime.as_ref().map(|lifetime| {
219224
quote! {
220225
< #lifetime >
@@ -610,7 +615,7 @@ mod tests {
610615
assert_tokens_eq(
611616
&blocks.cxx_qt_mod_contents[0],
612617
quote! {
613-
impl ::cxx_qt::ConstructorDeclared for qobject::MyObject {}
618+
impl ::cxx_qt::ConstructorDeclared<()> for qobject::MyObject {}
614619
},
615620
);
616621

@@ -741,6 +746,13 @@ mod tests {
741746

742747
assert_tokens_eq(
743748
&blocks.cxx_qt_mod_contents[4],
749+
quote! {
750+
impl ::cxx_qt::ConstructorDeclared<(*const QObject,)> for qobject::MyObject {}
751+
},
752+
);
753+
754+
assert_tokens_eq(
755+
&blocks.cxx_qt_mod_contents[5],
744756
quote! {
745757
#[doc(hidden)]
746758
pub fn route_arguments_MyObject_1<'lifetime>(arg0: *const QObject) -> qobject::CxxQtConstructorArgumentsMyObject1<'lifetime>
@@ -768,7 +780,7 @@ mod tests {
768780
},
769781
);
770782
assert_tokens_eq(
771-
&blocks.cxx_qt_mod_contents[5],
783+
&blocks.cxx_qt_mod_contents[6],
772784
quote! {
773785
#[doc(hidden)]
774786
#[allow(unused_variables)]
@@ -782,7 +794,7 @@ mod tests {
782794
},
783795
);
784796
assert_tokens_eq(
785-
&blocks.cxx_qt_mod_contents[6],
797+
&blocks.cxx_qt_mod_contents[7],
786798
quote! {
787799
#[doc(hidden)]
788800
#[allow(unused_variables)]
@@ -821,7 +833,7 @@ mod tests {
821833
]);
822834

823835
assert_eq!(blocks.cxx_mod_contents.len(), 10);
824-
assert_eq!(blocks.cxx_qt_mod_contents.len(), 7);
836+
assert_eq!(blocks.cxx_qt_mod_contents.len(), 8);
825837

826838
let namespace_attr = quote! {
827839
#[namespace = "qobject::cxx_qt_MyObject"]

crates/cxx-qt-gen/test_outputs/invokables.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ unsafe impl ::cxx_qt::casting::Upcast<::cxx_qt::QObject> for ffi::MyObject {
334334
ffi::cxx_qt_ffi_MyObject_downcastPtr(base)
335335
}
336336
}
337-
impl ::cxx_qt::ConstructorDeclared for ffi::MyObject {}
337+
impl ::cxx_qt::ConstructorDeclared<(i32, &'a QString)> for ffi::MyObject {}
338338
#[doc(hidden)]
339339
pub fn route_arguments_MyObject_0<'a>(
340340
arg0: i32,
@@ -377,6 +377,7 @@ pub fn initialize_MyObject_0<'a>(
377377
) {
378378
<ffi::MyObject as cxx_qt::Constructor<(i32, &'a ffi::QString)>>::initialize(qobject, ());
379379
}
380+
impl ::cxx_qt::ConstructorDeclared<()> for ffi::MyObject {}
380381
#[doc(hidden)]
381382
pub fn route_arguments_MyObject_1() -> ffi::CxxQtConstructorArgumentsMyObject1 {
382383
#[allow(unused_variables)]

crates/cxx-qt/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub trait Threading: Sized {
217217
}
218218

219219
#[doc(hidden)]
220-
pub trait ConstructorDeclared {}
220+
pub trait ConstructorDeclared<Arguments> {}
221221

222222
/// This trait can be implemented on any [CxxQtType] to define a
223223
/// custom constructor in C++ for the QObject.
@@ -310,7 +310,7 @@ pub trait ConstructorDeclared {}
310310
///
311311
/// If a QObject implements the `Initialize` trait, and the inner Rust struct is [Default]-constructible it will automatically implement `cxx_qt::Constructor<()>`.
312312
/// Additionally, implementing `impl cxx_qt::Initialize` will act as shorthand for `cxx_qt::Constructor<()>`.
313-
pub trait Constructor<Arguments>: CxxQtType + ConstructorDeclared {
313+
pub trait Constructor<Arguments>: CxxQtType + ConstructorDeclared<Arguments> {
314314
/// The arguments that are passed to the [`new()`](Self::new) function to construct the inner Rust struct.
315315
/// This must be a tuple of CXX compatible types.
316316
///
@@ -397,7 +397,7 @@ pub trait Constructor<Arguments>: CxxQtType + ConstructorDeclared {
397397
/// ```
398398
// TODO: Once the QObject type is available in the cxx-qt crate, also auto-generate a default
399399
// constructor that takes QObject and passes it to the parent.
400-
pub trait Initialize: CxxQtType + ConstructorDeclared {
400+
pub trait Initialize: CxxQtType + ConstructorDeclared<()> {
401401
/// This function is called to initialize the QObject after construction.
402402
fn initialize(self: core::pin::Pin<&mut Self>);
403403
}

0 commit comments

Comments
 (0)