@@ -16,8 +16,8 @@ pub mod signals;
1616pub mod threading;
1717
1818use crate :: generator:: { rust:: fragment:: GeneratedRustFragment , structuring} ;
19- use crate :: parser:: externqobject :: ParsedExternQObject ;
20- use crate :: parser:: { parameter:: ParsedFunctionParameter , qobject :: ParsedQObject , Parser } ;
19+ use crate :: parser:: cxxqtdata :: ParsedCxxQtData ;
20+ use crate :: parser:: { parameter:: ParsedFunctionParameter , Parser } ;
2121use proc_macro2:: { Ident , TokenStream } ;
2222use quote:: quote;
2323use syn:: { parse_quote, Item , ItemMod , Result } ;
@@ -61,12 +61,7 @@ impl GeneratedRustBlocks {
6161 let namespace = parser. cxx_qt_data . namespace . clone ( ) . unwrap_or_default ( ) ;
6262 let passthrough_mod = & parser. passthrough_module ;
6363
64- let qobject_import = add_qobject_import ( & parser. cxx_qt_data . qobjects ) ;
65- if qobject_import. is_empty ( ) {
66- for block in & parser. cxx_qt_data . extern_cxxqt_blocks {
67- fragments. push ( add_extern_qobject_import ( & block. qobjects ) )
68- }
69- } else {
64+ if let Some ( qobject_import) = add_qobject_import ( & parser. cxx_qt_data ) {
7065 fragments. push ( qobject_import) ;
7166 }
7267
@@ -105,48 +100,23 @@ impl GeneratedRustBlocks {
105100 }
106101}
107102
108- fn add_qobject_import ( qobjects : & [ ParsedQObject ] ) -> GeneratedRustFragment {
109- let includes = qobjects
103+ // Generate a type declaration for `QObject` if necessary
104+ fn add_qobject_import ( cxx_qt_data : & ParsedCxxQtData ) -> Option < GeneratedRustFragment > {
105+ let includes = cxx_qt_data
106+ . qobjects
110107 . iter ( )
111108 . any ( |obj| obj. has_qobject_macro && obj. base_class . is_none ( ) ) ;
112- if includes {
113- GeneratedRustFragment {
114- cxx_mod_contents : vec ! [ parse_quote! {
115- extern "C++" {
116- #[ doc( hidden) ]
117- #[ namespace="" ]
118- type QObject = cxx_qt:: QObject ;
119- }
120- } ] ,
121- cxx_qt_mod_contents : vec ! [ ] ,
122- }
123- } else {
124- GeneratedRustFragment {
125- cxx_mod_contents : vec ! [ ] ,
126- cxx_qt_mod_contents : vec ! [ ] ,
127- }
128- }
129- }
130-
131- fn add_extern_qobject_import ( qobjects : & [ ParsedExternQObject ] ) -> GeneratedRustFragment {
132- // All extern C++Qt types must have QObject macro
133- let includes = qobjects. iter ( ) . any ( |obj| obj. base_class . is_none ( ) ) ;
134- if includes {
135- GeneratedRustFragment {
136- cxx_mod_contents : vec ! [ parse_quote! {
137- extern "C++" {
138- #[ doc( hidden) ]
139- #[ namespace="" ]
140- type QObject = cxx_qt:: QObject ;
141- }
142- } ] ,
143- cxx_qt_mod_contents : vec ! [ ] ,
144- }
109+ if includes
110+ || cxx_qt_data
111+ . extern_cxxqt_blocks
112+ . iter ( )
113+ . filter ( |block| !block. qobjects . is_empty ( ) )
114+ . count ( )
115+ > 0
116+ {
117+ Some ( GeneratedRustFragment :: qobject_import ( ) )
145118 } else {
146- GeneratedRustFragment {
147- cxx_mod_contents : vec ! [ ] ,
148- cxx_qt_mod_contents : vec ! [ ] ,
149- }
119+ None
150120 }
151121}
152122
0 commit comments