Skip to content

Commit 998a00f

Browse files
committed
cxx-qt-gen: move QObject struct outside the bridge and use a type alias
Related to #559
1 parent 8097226 commit 998a00f

Some content is hidden

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

42 files changed

+769
-685
lines changed

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

Lines changed: 12 additions & 6 deletions
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::MyObject;
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::MyObject;
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::MyObject;
98+
}
9399
}
94100
};
95101
let parser = Parser::from(module).unwrap();

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

Lines changed: 20 additions & 10 deletions
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::MyObject;
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::MyObject;
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::MyNamedObject;
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::MyObject;
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::MyObject;
272+
}
263273
}
264274
};
265275
let parser = Parser::from(module).unwrap();

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

Lines changed: 1 addition & 1 deletion
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)
1919
}
2020
}
2121

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct QObjectName {
2424

2525
impl From<&ParsedQObject> for QObjectName {
2626
fn from(qobject: &ParsedQObject) -> Self {
27-
Self::from(&qobject.qobject_struct.ident)
27+
Self::from(&qobject.qobject_ty.ident)
2828
}
2929
}
3030

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

Lines changed: 16 additions & 8 deletions
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::MyObject;
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::MyObject;
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::MyObject;
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::MyObject;
152+
}
145153
}
146154
};
147155
let parser = Parser::from(module).unwrap();

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
parser::qobject::ParsedQObject,
1515
};
1616
use quote::quote;
17-
use syn::{Ident, ImplItem, Item, Result};
17+
use syn::{parse_quote, Ident, ImplItem, Item, Result};
1818

1919
#[derive(Default)]
2020
pub struct GeneratedRustQObjectBlocks {
@@ -62,11 +62,15 @@ impl GeneratedRustQObject {
6262
.blocks
6363
.append(&mut generate_qobject_definitions(&qobject_idents)?);
6464

65-
// Add our QObject struct to the implementation blocks
66-
generated
67-
.blocks
68-
.cxx_qt_mod_contents
69-
.push(syn::Item::Struct(qobject.qobject_struct.clone()));
65+
// Add a type alias so that generated code can still find T
66+
//
67+
// TODO: this should be removed once generated methods aren't in the hidden module
68+
generated.blocks.cxx_qt_mod_contents.push({
69+
let rust_struct_name_rust = &qobject_idents.rust_struct.rust;
70+
parse_quote! {
71+
type #rust_struct_name_rust = super::#rust_struct_name_rust;
72+
}
73+
});
7074

7175
// Generate methods for the properties, invokables, signals
7276
generated.blocks.append(&mut generate_rust_properties(
@@ -218,8 +222,10 @@ mod tests {
218222
let module: ItemMod = parse_quote! {
219223
#[cxx_qt::bridge]
220224
mod ffi {
221-
#[cxx_qt::qobject]
222-
pub struct MyObject;
225+
extern "RustQt" {
226+
#[cxx_qt::qobject]
227+
type MyObject = super::MyObject;
228+
}
223229
}
224230
};
225231
let parser = Parser::from(module).unwrap();
@@ -236,8 +242,10 @@ mod tests {
236242
let module: ItemMod = parse_quote! {
237243
#[cxx_qt::bridge(namespace = "cxx_qt")]
238244
mod ffi {
239-
#[cxx_qt::qobject]
240-
pub struct MyObject;
245+
extern "RustQt" {
246+
#[cxx_qt::qobject]
247+
type MyObject = super::MyObject;
248+
}
241249
}
242250
};
243251
let parser = Parser::from(module).unwrap();
@@ -254,8 +262,10 @@ mod tests {
254262
let module: ItemMod = parse_quote! {
255263
#[cxx_qt::bridge(namespace = "cxx_qt")]
256264
mod ffi {
257-
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
258-
pub struct MyObject;
265+
extern "RustQt" {
266+
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
267+
type MyObject = super::MyObject;
268+
}
259269
}
260270
};
261271
let parser = Parser::from(module).unwrap();

0 commit comments

Comments
 (0)