Skip to content

Commit 8115e75

Browse files
Add shorthand for cxx_qt::Initialize
- `impl cxx_qt::Initialize for x {}` can now be written in the bridge as shorthand - It is shorthand for `impl cxx_qt::Constructor<()> for x {}` - Update book and docs
1 parent 64d4ee6 commit 8115e75

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

book/src/bridge/traits.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ For further documentation, refer to the documentation of the individual traits:
2424
- [CxxQtType](https://docs.rs/cxx-qt/latest/cxx_qt/trait.CxxQtType.html) - trait to reach the Rust implementation of a `QObject`
2525
- This trait is automatically implemented for any `#[qobject]` type inside `extern "RustQt"` blocks.
2626
- [Constructor](https://docs.rs/cxx-qt/latest/cxx_qt/trait.Constructor.html) - custom constructor
27-
- [Initialize](https://docs.rs/cxx-qt/latest/cxx_qt/trait.Initialize.html) - execute Rust code when the object is constructed
27+
- [Initialize](https://docs.rs/cxx-qt/latest/cxx_qt/trait.Initialize.html) - execute Rust code when the object is constructed, or as shorthand for an empty constructor
2828
- [Threading](https://docs.rs/cxx-qt/latest/cxx_qt/trait.Threading.html) - marker trait whether CXX-Qt threading should be enabled
2929

3030
> ⚠️ These traits should only be implemented if you are sure you need to, they are automatically implemented for RustQt types.

crates/cxx-qt-gen/src/parser/trait_impl.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use indoc::formatdoc;
33
// SPDX-FileContributor: Leon Matthes <[email protected]>
44
//
55
// SPDX-License-Identifier: MIT OR Apache-2.0
6-
use syn::{Error, Ident, ItemImpl, Path, Result, Token, Type, TypePath};
6+
use syn::{parse_quote, Error, Ident, ItemImpl, Path, Result, Token, Type, TypePath};
77

88
use crate::{parser::constructor::Constructor, syntax::path::path_compare_str};
99

@@ -46,6 +46,12 @@ impl TraitKind {
4646
Self::parse_threading(not, path, imp)
4747
} else if path_compare_str(path, &["cxx_qt", "Constructor"]) {
4848
Self::parse_constructor(imp)
49+
} else if path_compare_str(path, &["cxx_qt", "Initialize"]) {
50+
let struct_name = &*imp.self_ty;
51+
let default_constructor: ItemImpl = parse_quote! {
52+
impl cxx_qt::Constructor<()> for #struct_name {}
53+
};
54+
Self::parse_constructor(&default_constructor)
4955
} else {
5056
// TODO: Give suggestions on which trait might have been meant
5157
Err(Error::new_spanned(
@@ -55,6 +61,7 @@ impl TraitKind {
5561
CXX-Qt currently only supports:
5662
- cxx_qt::Threading
5763
- cxx_qt::Constructor
64+
- cxx_qt::Initialize (as shorthand for Constructor<()>)
5865
- (cxx_qt::Locking has been removed as of CXX-Qt 0.7)
5966
Note that the trait must always be fully-qualified.
6067
"},
@@ -130,6 +137,23 @@ mod tests {
130137
assert!(matches!(marker.kind, TraitKind::Constructor(_)))
131138
}
132139

140+
#[test]
141+
fn parse_constructor_shorthand() {
142+
let imp = parse_quote! {
143+
impl cxx_qt::Constructor<()> for MyObject {}
144+
};
145+
let marker = TraitImpl::parse(imp).unwrap();
146+
assert_eq!(marker.qobject, format_ident!("MyObject"));
147+
assert!(matches!(marker.kind, TraitKind::Constructor(_)));
148+
149+
let imp = parse_quote! {
150+
impl cxx_qt::Initialize for MyObject {}
151+
};
152+
let shorthand = TraitImpl::parse(imp).unwrap();
153+
assert_eq!(marker.kind, shorthand.kind);
154+
assert_eq!(marker.qobject, shorthand.qobject);
155+
}
156+
133157
use crate::tests::assert_parse_errors;
134158

135159
#[test]

crates/cxx-qt/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ impl<T: Sized> Downcast for T {}
313313
/// To reduce the boilerplate of this use-case, CXX-Qt provides the [Initialize] trait.
314314
///
315315
/// If a QObject implements the `Initialize` trait, and the inner Rust struct is [Default]-constructible it will automatically implement `cxx_qt::Constructor<()>`.
316+
/// Additionally, implementing `impl cxx_qt::Initialize` will act as shorthand for `cxx_qt::Constructor<()>`.
316317
pub trait Constructor<Arguments>: CxxQtType {
317318
/// The arguments that are passed to the [`new()`](Self::new) function to construct the inner Rust struct.
318319
/// This must be a tuple of CXX compatible types.
@@ -361,6 +362,7 @@ pub trait Constructor<Arguments>: CxxQtType {
361362
/// that calls the `initialize` function after constructing a default Rust struct.
362363
///
363364
/// Ensure that the `impl cxx_qt::Constructor<()> for ... {}` is declared inside the CXX-Qt bridge.
365+
/// Alternatively, using `impl cxx_qt::Initialize for ... {}` acts as shorthand for the above line.
364366
///
365367
/// # Example
366368
///
@@ -376,6 +378,8 @@ pub trait Constructor<Arguments>: CxxQtType {
376378
///
377379
/// // Remember to tell the bridge about the default constructor
378380
/// impl cxx_qt::Constructor<()> for MyStruct {}
381+
/// // or
382+
/// impl cxx_qt::Initialize for MyStruct {}
379383
/// }
380384
///
381385
/// // Make sure the inner Rust struct implements `Default`

examples/qml_minimal/rust/src/cxxqt_object.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl qobject::MyObject {
7676
println!("Hi from Rust! String is '{string}' and number is {number}");
7777
}
7878
}
79+
7980
// ANCHOR_END: book_rustobj_invokable_impl
8081

8182
// ANCHOR_END: book_cxx_qt_module

0 commit comments

Comments
 (0)