Skip to content

Commit 683889c

Browse files
Fix constructor generic args
- Add lifetime - Update span
1 parent 5303de3 commit 683889c

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,25 @@ 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! {
213-
qobject_name_rust.span() => // TODO! Improve this span
214-
impl ::cxx_qt::ConstructorDeclared for #qobject_name_rust_qualified {}
215-
}]);
216-
217210
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+
218216
let lifetime = constructor.lifetime.as_ref().map(|lifetime| {
219217
quote! {
220218
< #lifetime >
221219
}
222220
});
221+
222+
result
223+
.cxx_qt_mod_contents
224+
.append(&mut vec![parse_quote_spanned! {
225+
constructor.imp.span() => // TODO! Improve this span
226+
impl #lifetime ::cxx_qt::ConstructorDeclared<(#arguments) > for #qobject_name_rust_qualified {}
227+
}]);
228+
223229
let arguments_lifetime =
224230
lifetime_of_arguments(&constructor.lifetime, &constructor.arguments)?;
225231
let base_lifetime =
@@ -610,7 +616,7 @@ mod tests {
610616
assert_tokens_eq(
611617
&blocks.cxx_qt_mod_contents[0],
612618
quote! {
613-
impl ::cxx_qt::ConstructorDeclared for qobject::MyObject {}
619+
impl ::cxx_qt::ConstructorDeclared<()> for qobject::MyObject {}
614620
},
615621
);
616622

@@ -741,6 +747,13 @@ mod tests {
741747

742748
assert_tokens_eq(
743749
&blocks.cxx_qt_mod_contents[4],
750+
quote! {
751+
impl<'lifetime> ::cxx_qt::ConstructorDeclared<(*const QObject,)> for qobject::MyObject {}
752+
},
753+
);
754+
755+
assert_tokens_eq(
756+
&blocks.cxx_qt_mod_contents[5],
744757
quote! {
745758
#[doc(hidden)]
746759
pub fn route_arguments_MyObject_1<'lifetime>(arg0: *const QObject) -> qobject::CxxQtConstructorArgumentsMyObject1<'lifetime>
@@ -768,7 +781,7 @@ mod tests {
768781
},
769782
);
770783
assert_tokens_eq(
771-
&blocks.cxx_qt_mod_contents[5],
784+
&blocks.cxx_qt_mod_contents[6],
772785
quote! {
773786
#[doc(hidden)]
774787
#[allow(unused_variables)]
@@ -782,7 +795,7 @@ mod tests {
782795
},
783796
);
784797
assert_tokens_eq(
785-
&blocks.cxx_qt_mod_contents[6],
798+
&blocks.cxx_qt_mod_contents[7],
786799
quote! {
787800
#[doc(hidden)]
788801
#[allow(unused_variables)]
@@ -821,7 +834,7 @@ mod tests {
821834
]);
822835

823836
assert_eq!(blocks.cxx_mod_contents.len(), 10);
824-
assert_eq!(blocks.cxx_qt_mod_contents.len(), 7);
837+
assert_eq!(blocks.cxx_qt_mod_contents.len(), 8);
825838

826839
let namespace_attr = quote! {
827840
#[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<'a> ::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)