Skip to content

Commit 0414d25

Browse files
ahayzen-kdabBe-ing
authored andcommitted
cxx-qt-gen: move QObject struct outside the bridge and use a type alias
Related to #559
1 parent f05b4a3 commit 0414d25

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1342
-1151
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
- `#[qinvokable]` is now defined as a signature in `extern "RustQt"`
3333
- `rust_mut` is now safe to call
3434
- `#[qproperty]` is now defined as an attribute on the qobject rather than the field
35+
- QObject struct is now split between the bridge and implementation outside via a type alias
3536

3637
### Fixed
3738

Diff for: crates/cxx-qt-gen/src/generator/cpp/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ mod tests {
5454
let module: ItemMod = parse_quote! {
5555
#[cxx_qt::bridge]
5656
mod ffi {
57-
#[cxx_qt::qobject]
58-
pub struct MyObject;
57+
extern "RustQt" {
58+
#[cxx_qt::qobject]
59+
type MyObject = super::MyObjectRust;
60+
}
5961
}
6062
};
6163
let parser = Parser::from(module).unwrap();
@@ -71,8 +73,10 @@ mod tests {
7173
let module: ItemMod = parse_quote! {
7274
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
7375
mod ffi {
74-
#[cxx_qt::qobject]
75-
pub struct MyObject;
76+
extern "RustQt" {
77+
#[cxx_qt::qobject]
78+
type MyObject = super::MyObjectRust;
79+
}
7680
}
7781
};
7882
let parser = Parser::from(module).unwrap();
@@ -88,8 +92,10 @@ mod tests {
8892
let module: ItemMod = parse_quote! {
8993
#[cxx_qt::bridge(namespace = "cxx_qt")]
9094
mod ffi {
91-
#[cxx_qt::qobject]
92-
pub struct MyObject;
95+
extern "RustQt" {
96+
#[cxx_qt::qobject]
97+
type MyObject = super::MyObjectRust;
98+
}
9399
}
94100
};
95101
let parser = Parser::from(module).unwrap();

Diff for: crates/cxx-qt-gen/src/generator/cpp/qobject.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,10 @@ mod tests {
165165
let module: ItemMod = parse_quote! {
166166
#[cxx_qt::bridge]
167167
mod ffi {
168-
#[cxx_qt::qobject]
169-
pub struct MyObject;
168+
extern "RustQt" {
169+
#[cxx_qt::qobject]
170+
type MyObject = super::MyObjectRust;
171+
}
170172
}
171173
};
172174
let parser = Parser::from(module).unwrap();
@@ -188,8 +190,10 @@ mod tests {
188190
let module: ItemMod = parse_quote! {
189191
#[cxx_qt::bridge(namespace = "cxx_qt")]
190192
mod ffi {
191-
#[cxx_qt::qobject(base = "QStringListModel")]
192-
pub struct MyObject;
193+
extern "RustQt" {
194+
#[cxx_qt::qobject(base = "QStringListModel")]
195+
type MyObject = super::MyObjectRust;
196+
}
193197
}
194198
};
195199
let parser = Parser::from(module).unwrap();
@@ -209,8 +213,10 @@ mod tests {
209213
let module: ItemMod = parse_quote! {
210214
#[cxx_qt::bridge(namespace = "cxx_qt")]
211215
mod ffi {
212-
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_name = "MyQmlElement")]
213-
pub struct MyNamedObject;
216+
extern "RustQt" {
217+
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_name = "MyQmlElement")]
218+
type MyNamedObject = super::MyNamedObjectRust;
219+
}
214220
}
215221
};
216222
let parser = Parser::from(module).unwrap();
@@ -233,8 +239,10 @@ mod tests {
233239
let module: ItemMod = parse_quote! {
234240
#[cxx_qt::bridge(namespace = "cxx_qt")]
235241
mod ffi {
236-
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
237-
pub struct MyObject;
242+
extern "RustQt" {
243+
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
244+
type MyObject = super::MyObjectRust;
245+
}
238246
}
239247
};
240248
let parser = Parser::from(module).unwrap();
@@ -258,8 +266,10 @@ mod tests {
258266
let module: ItemMod = parse_quote! {
259267
#[cxx_qt::bridge(namespace = "cxx_qt")]
260268
mod ffi {
261-
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_uncreatable)]
262-
pub struct MyObject;
269+
extern "RustQt" {
270+
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_uncreatable)]
271+
type MyObject = super::MyObjectRust;
272+
}
263273
}
264274
};
265275
let parser = Parser::from(module).unwrap();

Diff for: crates/cxx-qt-gen/src/generator/naming/namespace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct NamespaceName {
1515

1616
impl From<&ParsedQObject> for NamespaceName {
1717
fn from(qobject: &ParsedQObject) -> Self {
18-
NamespaceName::from_pair_str(&qobject.namespace, &qobject.qobject_struct.ident)
18+
NamespaceName::from_pair_str(&qobject.namespace, &qobject.qobject_ty.ident_left)
1919
}
2020
}
2121

Diff for: crates/cxx-qt-gen/src/generator/naming/qobject.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@ pub struct QObjectName {
2424

2525
impl From<&ParsedQObject> for QObjectName {
2626
fn from(qobject: &ParsedQObject) -> Self {
27-
Self::from(&qobject.qobject_struct.ident)
28-
}
29-
}
30-
31-
impl From<&Ident> for QObjectName {
32-
fn from(ident: &Ident) -> Self {
27+
let ident_left = &qobject.qobject_ty.ident_left;
3328
Self {
34-
ident: ident.clone(),
35-
cpp_class: CombinedIdent::cpp_class_from_rust_struct(ident.clone()),
36-
rust_struct: CombinedIdent::from_rust_struct(ident.clone()),
37-
cxx_qt_thread_class: cxx_qt_thread_class_from_ident(ident),
38-
cxx_qt_thread_queued_fn_struct: cxx_qt_thread_queued_fn_struct_from_ident(ident),
29+
ident: ident_left.clone(),
30+
// TODO: later we may support cxx_name or rust_name so keep these are CombinedIdents for now
31+
cpp_class: CombinedIdent::from_ident(ident_left.clone()),
32+
rust_struct: CombinedIdent::from_ident(qobject.qobject_ty.ident_right.clone()),
33+
cxx_qt_thread_class: cxx_qt_thread_class_from_ident(ident_left),
34+
cxx_qt_thread_queued_fn_struct: cxx_qt_thread_queued_fn_struct_from_ident(ident_left),
3935
}
4036
}
4137
}
@@ -61,16 +57,10 @@ fn cxx_qt_thread_queued_fn_struct_from_ident(ident: &Ident) -> Ident {
6157
}
6258

6359
impl CombinedIdent {
64-
/// For a given ident generate the C++ class name
65-
fn cpp_class_from_rust_struct(ident: Ident) -> Self {
66-
let rust = format_ident!("{ident}Qt");
67-
Self { cpp: ident, rust }
68-
}
69-
7060
/// For a given ident generate the Rust and C++ names
71-
fn from_rust_struct(ident: Ident) -> Self {
61+
fn from_ident(ident: Ident) -> Self {
7262
Self {
73-
cpp: format_ident!("{ident}Rust"),
63+
cpp: ident.clone(),
7464
rust: ident,
7565
}
7666
}
@@ -90,9 +80,9 @@ pub mod tests {
9080
fn test_parsed_property() {
9181
let names = QObjectName::from(&create_parsed_qobject());
9282
assert_eq!(names.cpp_class.cpp, format_ident!("MyObject"));
93-
assert_eq!(names.cpp_class.rust, format_ident!("MyObjectQt"));
83+
assert_eq!(names.cpp_class.rust, format_ident!("MyObject"));
9484
assert_eq!(names.rust_struct.cpp, format_ident!("MyObjectRust"));
95-
assert_eq!(names.rust_struct.rust, format_ident!("MyObject"));
85+
assert_eq!(names.rust_struct.rust, format_ident!("MyObjectRust"));
9686
assert_eq!(
9787
names.cxx_qt_thread_class,
9888
format_ident!("MyObjectCxxQtThread")

Diff for: crates/cxx-qt-gen/src/generator/rust/inherit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ mod tests {
9494
quote! {
9595
unsafe extern "C++" {
9696
#[cxx_name = "testCxxQtInherit"]
97-
fn test(self: Pin<&mut MyObjectQt>, a: B, b: C);
97+
fn test(self: Pin<&mut MyObject>, a: B, b: C);
9898
}
9999
},
100100
);
@@ -118,7 +118,7 @@ mod tests {
118118
quote! {
119119
unsafe extern "C++" {
120120
#[cxx_name = "testCxxQtInherit"]
121-
fn test(self: &MyObjectQt, a: B, b: C);
121+
fn test(self: &MyObject, a: B, b: C);
122122
}
123123
},
124124
);
@@ -143,7 +143,7 @@ mod tests {
143143
quote! {
144144
extern "C++" {
145145
#[cxx_name = "testCxxQtInherit"]
146-
unsafe fn test(self: &MyObjectQt,);
146+
unsafe fn test(self: &MyObject,);
147147
}
148148
},
149149
);

Diff for: crates/cxx-qt-gen/src/generator/rust/invokable.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,16 @@ mod tests {
172172
quote! {
173173
extern "Rust" {
174174
#[cxx_name = "voidInvokableWrapper"]
175-
fn void_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt);
175+
fn void_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject);
176176
}
177177
},
178178
);
179179
assert_tokens_eq(
180180
&generated.cxx_qt_mod_contents[0],
181181
quote! {
182-
impl MyObject {
182+
impl MyObjectRust {
183183
#[doc(hidden)]
184-
pub fn void_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt) {
184+
pub fn void_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject) {
185185
cpp.void_invokable();
186186
}
187187
}
@@ -194,16 +194,16 @@ mod tests {
194194
quote! {
195195
extern "Rust" {
196196
#[cxx_name = "trivialInvokableWrapper"]
197-
fn trivial_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: i32) -> i32;
197+
fn trivial_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: i32) -> i32;
198198
}
199199
},
200200
);
201201
assert_tokens_eq(
202202
&generated.cxx_qt_mod_contents[1],
203203
quote! {
204-
impl MyObject {
204+
impl MyObjectRust {
205205
#[doc(hidden)]
206-
pub fn trivial_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: i32) -> i32 {
206+
pub fn trivial_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: i32) -> i32 {
207207
return cpp.trivial_invokable(param);
208208
}
209209
}
@@ -216,16 +216,16 @@ mod tests {
216216
quote! {
217217
extern "Rust" {
218218
#[cxx_name = "opaqueInvokableWrapper"]
219-
fn opaque_invokable_wrapper(self: &mut MyObject, cpp: Pin<&mut MyObjectQt>, param: &QColor) -> UniquePtr<QColor>;
219+
fn opaque_invokable_wrapper(self: &mut MyObjectRust, cpp: Pin<&mut MyObject>, param: &QColor) -> UniquePtr<QColor>;
220220
}
221221
},
222222
);
223223
assert_tokens_eq(
224224
&generated.cxx_qt_mod_contents[2],
225225
quote! {
226-
impl MyObject {
226+
impl MyObjectRust {
227227
#[doc(hidden)]
228-
pub fn opaque_invokable_wrapper(self: &mut MyObject, cpp: Pin<&mut MyObjectQt>, param: &QColor) -> UniquePtr<QColor> {
228+
pub fn opaque_invokable_wrapper(self: &mut MyObjectRust, cpp: Pin<&mut MyObject>, param: &QColor) -> UniquePtr<QColor> {
229229
return cpp.opaque_invokable(param);
230230
}
231231
}
@@ -238,16 +238,16 @@ mod tests {
238238
quote! {
239239
extern "Rust" {
240240
#[cxx_name = "unsafeInvokableWrapper"]
241-
unsafe fn unsafe_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: *mut T) -> *mut T;
241+
unsafe fn unsafe_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: *mut T) -> *mut T;
242242
}
243243
},
244244
);
245245
assert_tokens_eq(
246246
&generated.cxx_qt_mod_contents[3],
247247
quote! {
248-
impl MyObject {
248+
impl MyObjectRust {
249249
#[doc(hidden)]
250-
pub unsafe fn unsafe_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: *mut T) -> *mut T {
250+
pub unsafe fn unsafe_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: *mut T) -> *mut T {
251251
return cpp.unsafe_invokable(param);
252252
}
253253
}

Diff for: crates/cxx-qt-gen/src/generator/rust/mod.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ mod tests {
7373
let module: ItemMod = parse_quote! {
7474
#[cxx_qt::bridge]
7575
mod ffi {
76-
#[cxx_qt::qobject]
77-
pub struct MyObject;
76+
extern "RustQt" {
77+
#[cxx_qt::qobject]
78+
type MyObject = super::MyObjectRust;
79+
}
7880
}
7981
};
8082
let parser = Parser::from(module).unwrap();
@@ -100,8 +102,10 @@ mod tests {
100102
let module: ItemMod = parse_quote! {
101103
#[cxx_qt::bridge(namespace = "cxx_qt")]
102104
mod ffi {
103-
#[cxx_qt::qobject]
104-
pub struct MyObject;
105+
extern "RustQt" {
106+
#[cxx_qt::qobject]
107+
type MyObject = super::MyObjectRust;
108+
}
105109
}
106110
};
107111
let parser = Parser::from(module).unwrap();
@@ -121,8 +125,10 @@ mod tests {
121125
mod ffi {
122126
use std::collections::HashMap;
123127

124-
#[cxx_qt::qobject]
125-
pub struct MyObject;
128+
extern "RustQt" {
129+
#[cxx_qt::qobject]
130+
type MyObject = super::MyObjectRust;
131+
}
126132
}
127133
};
128134
let parser = Parser::from(module).unwrap();
@@ -140,8 +146,10 @@ mod tests {
140146
let module: ItemMod = parse_quote! {
141147
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
142148
mod ffi {
143-
#[cxx_qt::qobject]
144-
pub struct MyObject;
149+
extern "RustQt" {
150+
#[cxx_qt::qobject]
151+
type MyObject = super::MyObjectRust;
152+
}
145153
}
146154
};
147155
let parser = Parser::from(module).unwrap();

0 commit comments

Comments
 (0)