@@ -10,7 +10,7 @@ use crate::{
10
10
syntax:: path:: path_compare_str,
11
11
} ;
12
12
use quote:: quote;
13
- use syn:: { parse_quote, Attribute , Result } ;
13
+ use syn:: { parse_quote, Attribute , Item , Result } ;
14
14
15
15
impl GeneratedRustFragment {
16
16
pub fn from_extern_cxx_qt (
@@ -22,76 +22,88 @@ impl GeneratedRustFragment {
22
22
} else {
23
23
quote ! { }
24
24
} ;
25
+ let extern_block_docs = & extern_cxxqt_block. docs ;
25
26
26
27
// Add the pass through blocks
27
28
let unsafety = & extern_cxxqt_block. unsafety ;
28
29
let items = & extern_cxxqt_block. passthrough_items ;
29
- let mut generated = extern_cxxqt_block
30
- . qobjects
31
- . iter ( )
32
- . map ( |ty| -> Result < GeneratedRustFragment > {
33
- let mut generated = vec ! [ ] ;
34
- let qobject_names = QObjectNames :: from_extern_qobject ( ty, type_names) ?;
35
30
36
- generated. push ( GeneratedRustFragment :: generate_casting_impl (
37
- & qobject_names,
38
- type_names,
39
- & ty. name ,
40
- & ty. base_class ,
41
- ) ?) ;
31
+ let mut qobject_items: Vec < Item > = vec ! [ ] ;
32
+ let mut cxx_qt_mod = vec ! [ ] ;
33
+ let mut cxx_mod = vec ! [ ] ;
34
+ for obj in & extern_cxxqt_block. qobjects {
35
+ let qobject_names = QObjectNames :: from_extern_qobject ( obj, type_names) ?;
42
36
43
- let namespace = if let Some ( namespace) = & ty. name . namespace ( ) {
44
- quote ! { #[ namespace = #namespace ] }
45
- } else {
46
- quote ! { }
47
- } ;
48
- let cpp_name = & ty. name . cxx_unqualified ( ) ;
49
- let rust_name = & ty. name . rust_unqualified ( ) ;
50
- let vis = & ty. declaration . vis ;
51
- let ident = & ty. name . rust_unqualified ( ) ;
52
- let cxx_name = if & rust_name. to_string ( ) == cpp_name {
53
- quote ! { }
54
- } else {
55
- let cxx_name = cpp_name. to_string ( ) ;
56
- quote ! {
57
- #[ cxx_name = #cxx_name]
58
- }
59
- } ;
60
- let cfgs: Vec < & Attribute > = ty
61
- . declaration
62
- . attrs
63
- . iter ( )
64
- . filter ( |attr| path_compare_str ( attr. meta . path ( ) , & [ "cfg" ] ) )
65
- . collect ( ) ;
66
- let docs: Vec < & Attribute > = ty
67
- . declaration
68
- . attrs
69
- . iter ( )
70
- . filter ( |attr| path_compare_str ( attr. meta . path ( ) , & [ "doc" ] ) )
71
- . collect ( ) ;
72
- generated. push ( GeneratedRustFragment :: from_cxx_item ( parse_quote ! {
73
- #extern_block_namespace
74
- #unsafety extern "C++" {
75
- #namespace
76
- #cxx_name
77
- #( #cfgs) *
78
- #( #docs) *
79
- #vis type #ident;
80
- }
81
- } ) ) ;
82
- Ok ( GeneratedRustFragment :: flatten ( generated) )
83
- } )
84
- . collect :: < Result < Vec < _ > > > ( ) ?;
37
+ let casting = GeneratedRustFragment :: generate_casting_impl (
38
+ & qobject_names,
39
+ type_names,
40
+ & obj. name ,
41
+ & obj. base_class ,
42
+ ) ?;
43
+ cxx_mod. extend ( casting. cxx_mod_contents ) ;
44
+ cxx_qt_mod. extend ( casting. cxx_qt_mod_contents ) ;
85
45
86
- if !items. is_empty ( ) {
87
- generated. push ( GeneratedRustFragment :: from_cxx_item ( parse_quote ! {
88
- #extern_block_namespace
89
- #unsafety extern "C++" {
90
- #( #items) *
46
+ let namespace = if let Some ( namespace) = & obj. name . namespace ( ) {
47
+ quote ! { #[ namespace = #namespace ] }
48
+ } else {
49
+ quote ! { }
50
+ } ;
51
+ let cpp_name = & obj. name . cxx_unqualified ( ) ;
52
+ let rust_name = & obj. name . rust_unqualified ( ) ;
53
+ let vis = & obj. declaration . vis ;
54
+ let ident = & obj. name . rust_unqualified ( ) ;
55
+ let cxx_name = if & rust_name. to_string ( ) == cpp_name {
56
+ quote ! { }
57
+ } else {
58
+ let cxx_name = cpp_name. to_string ( ) ;
59
+ quote ! {
60
+ #[ cxx_name = #cxx_name]
91
61
}
92
- } ) ) ;
62
+ } ;
63
+ let cfgs: Vec < & Attribute > = obj
64
+ . declaration
65
+ . attrs
66
+ . iter ( )
67
+ . filter ( |attr| path_compare_str ( attr. meta . path ( ) , & [ "cfg" ] ) )
68
+ . collect ( ) ;
69
+ let docs: Vec < & Attribute > = obj
70
+ . declaration
71
+ . attrs
72
+ . iter ( )
73
+ . filter ( |attr| path_compare_str ( attr. meta . path ( ) , & [ "doc" ] ) )
74
+ . collect ( ) ;
75
+ qobject_items. push ( parse_quote ! {
76
+ #namespace
77
+ #cxx_name
78
+ #( #cfgs) *
79
+ #( #docs) *
80
+ #vis type #ident;
81
+ } ) ;
93
82
}
94
83
84
+ let passthrough_items = if !items. is_empty ( ) {
85
+ quote ! {
86
+ #( #items) *
87
+ }
88
+ } else {
89
+ quote ! { }
90
+ } ;
91
+
92
+ cxx_mod. push ( parse_quote ! {
93
+ #extern_block_namespace
94
+ #( #extern_block_docs) *
95
+ #unsafety extern "C++" {
96
+ #( #qobject_items) *
97
+
98
+ #passthrough_items
99
+ }
100
+ } ) ;
101
+
102
+ let mut generated = vec ! [ GeneratedRustFragment {
103
+ cxx_mod_contents: cxx_mod,
104
+ cxx_qt_mod_contents: cxx_qt_mod,
105
+ } ] ;
106
+
95
107
// Build the signals
96
108
for signal in & extern_cxxqt_block. signals {
97
109
let qobject_name = type_names. lookup ( & signal. qobject_ident ) ?;
0 commit comments