Skip to content

Commit 533dff7

Browse files
BenFordTytheringtonahayzen-kdab
authored andcommitted
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, qml_features and docs
1 parent 477bbba commit 533dff7

File tree

10 files changed

+50
-7
lines changed

10 files changed

+50
-7
lines changed

book/src/bridge/extern_rustqt.md

+13
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ For more information on inheritance and how to override methods see the [Inherit
9999
### Traits
100100

101101
The [`Default` trait](https://doc.rust-lang.org/std/default/trait.Default.html) needs to be implemented for the `#[qobject]` marked struct either by hand or by using the derive macro `#[derive(Default)]`. Or the [`cxx_qt::Constructor`](https://docs.rs/cxx-qt/latest/cxx_qt/trait.Constructor.html) trait needs to be implemented for the type.
102+
In order to simply implement the `Constructor` trait, the following shorthand is available:
103+
104+
```rust,ignore
105+
impl cxx_qt::Initialize for x {}
106+
```
107+
108+
is equivalent to writing
109+
110+
```rust,ignore
111+
impl cxx_qt::Constructor<()> for x {}
112+
```
113+
114+
inside the bridge.
102115

103116
For further documentation see the [traits page](./traits.md).
104117

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

+26-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,13 @@ 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+
// Inside the bridge, Initialize is shorthand for the line below
52+
let default_constructor: ItemImpl = parse_quote! {
53+
impl cxx_qt::Constructor<()> for #struct_name {}
54+
};
55+
Self::parse_constructor(&default_constructor)
4956
} else {
5057
// TODO: Give suggestions on which trait might have been meant
5158
Err(Error::new_spanned(
@@ -55,6 +62,7 @@ impl TraitKind {
5562
CXX-Qt currently only supports:
5663
- cxx_qt::Threading
5764
- cxx_qt::Constructor
65+
- cxx_qt::Initialize (as shorthand for Constructor<()>)
5866
- (cxx_qt::Locking has been removed as of CXX-Qt 0.7)
5967
Note that the trait must always be fully-qualified.
6068
"},
@@ -130,6 +138,23 @@ mod tests {
130138
assert!(matches!(marker.kind, TraitKind::Constructor(_)))
131139
}
132140

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

135160
#[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/demo_threading/rust/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub mod qobject {
5151
fn sensor_power(self: Pin<&mut EnergyUsage>, uuid: &QString) -> f64;
5252
}
5353

54-
impl cxx_qt::Constructor<()> for EnergyUsage {}
54+
impl cxx_qt::Initialize for EnergyUsage {}
5555
}
5656

5757
use crate::{

examples/qml_features/rust/src/custom_parent_class.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub mod qobject {
5454
fn update(self: Pin<&mut CustomParentClass>);
5555
}
5656

57-
impl cxx_qt::Constructor<()> for CustomParentClass {}
57+
impl cxx_qt::Initialize for CustomParentClass {}
5858
}
5959

6060
use core::pin::Pin;

examples/qml_features/rust/src/nested_qobjects.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub mod qobject {
6262
fn reset(self: Pin<&mut OuterObject>);
6363
}
6464

65-
impl cxx_qt::Constructor<()> for OuterObject {}
65+
impl cxx_qt::Initialize for OuterObject {}
6666
}
6767

6868
use core::pin::Pin;

examples/qml_features/rust/src/properties.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub mod qobject {
4242
fn reset_url(self: Pin<&mut RustProperties>);
4343
}
4444

45-
impl cxx_qt::Constructor<()> for RustProperties {}
45+
impl cxx_qt::Initialize for RustProperties {}
4646

4747
// Dummy constructor, added for an example in the book.
4848
// ANCHOR: book_constructor_new_decl

examples/qml_features/rust/src/signals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub mod qobject {
5656
// ANCHOR_END: book_rust_obj_impl
5757

5858
// ANCHOR: book_initialize_decl
59-
impl cxx_qt::Constructor<()> for RustSignals {}
59+
impl cxx_qt::Initialize for RustSignals {}
6060
// ANCHOR_END: book_initialize_decl
6161

6262
// ANCHOR: book_constructor_decl

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)