@@ -25,19 +25,16 @@ use syn::{
25
25
} ;
26
26
27
27
pub struct ParsedCxxQtData {
28
- /// Map of the QObjects defined in the module that will be used for code generation
29
- //
30
- // We have to use a BTreeMap here, instead of a HashMap, to keep the order of QObjects stable.
31
- // Otherwise, the output order would be different, depending on the environment, which makes it hard to test/debug.
32
- pub qobjects : Vec < ParsedQObject > ,
28
+ /// List of QObjects defined in the module, separated by block
29
+ pub qobjects : Vec < Vec < ParsedQObject > > ,
33
30
/// List of QEnums defined in the module, that aren't associated with a QObject
34
31
pub qenums : Vec < ParsedQEnum > ,
35
- /// List of methods and Q_INVOKABLES found
36
- pub methods : Vec < ParsedMethod > ,
37
- /// List of the Q_SIGNALS found
38
- pub signals : Vec < ParsedSignal > ,
39
- /// List of the inherited methods found
40
- pub inherited_methods : Vec < ParsedInheritedMethod > ,
32
+ /// List of methods and Q_INVOKABLES found, separated by block
33
+ pub methods : Vec < Vec < ParsedMethod > > ,
34
+ /// List of the Q_SIGNALS found, separated by block
35
+ pub signals : Vec < Vec < ParsedSignal > > ,
36
+ /// List of the inherited methods found, separated by block
37
+ pub inherited_methods : Vec < Vec < ParsedInheritedMethod > > ,
41
38
/// List of QNamespace declarations
42
39
pub qnamespaces : Vec < ParsedQNamespace > ,
43
40
/// Blocks of extern "C++Qt"
@@ -50,6 +47,11 @@ pub struct ParsedCxxQtData {
50
47
pub module_ident : Ident ,
51
48
}
52
49
50
+ /// Used to get a flat iterator view into a 2D Vector
51
+ fn flat_view < T > ( v : & [ Vec < T > ] ) -> impl Iterator < Item = & T > {
52
+ v. iter ( ) . flat_map ( |v| v. iter ( ) )
53
+ }
54
+
53
55
impl ParsedCxxQtData {
54
56
/// Create a ParsedCxxQtData from a given module and namespace
55
57
pub fn new ( module_ident : Ident , namespace : Option < String > ) -> Self {
@@ -67,6 +69,26 @@ impl ParsedCxxQtData {
67
69
}
68
70
}
69
71
72
+ /// Iterator wrapper for methods
73
+ pub fn methods ( & self ) -> impl Iterator < Item = & ParsedMethod > {
74
+ flat_view ( & self . methods )
75
+ }
76
+
77
+ /// Iterator wrapper for signals
78
+ pub fn signals ( & self ) -> impl Iterator < Item = & ParsedSignal > {
79
+ flat_view ( & self . signals )
80
+ }
81
+
82
+ /// Iterator wrapper for inherited methods
83
+ pub fn inherited_methods ( & self ) -> impl Iterator < Item = & ParsedInheritedMethod > {
84
+ flat_view ( & self . inherited_methods )
85
+ }
86
+
87
+ /// Iterator wrapper for QObjects
88
+ pub fn qobjects ( & self ) -> impl Iterator < Item = & ParsedQObject > {
89
+ flat_view ( & self . qobjects )
90
+ }
91
+
70
92
/// Determine if the given [syn::Item] is a CXX-Qt related item
71
93
/// If it is then add the [syn::Item] into qobjects BTreeMap
72
94
/// Otherwise return the [syn::Item] to pass through to CXX
@@ -224,10 +246,10 @@ impl ParsedCxxQtData {
224
246
try_inline_self_invokables ( inline_self, & inline_ident, & mut signals) ?;
225
247
try_inline_self_invokables ( inline_self, & inline_ident, & mut inherited) ?;
226
248
227
- self . qobjects . extend ( qobjects) ;
228
- self . methods . extend ( methods) ;
229
- self . signals . extend ( signals) ;
230
- self . inherited_methods . extend ( inherited) ;
249
+ self . qobjects . push ( qobjects) ;
250
+ self . methods . push ( methods) ;
251
+ self . signals . push ( signals) ;
252
+ self . inherited_methods . push ( inherited) ;
231
253
232
254
Ok ( ( ) )
233
255
}
@@ -247,8 +269,7 @@ impl ParsedCxxQtData {
247
269
248
270
#[ cfg( test) ]
249
271
fn find_object ( & self , id : & Ident ) -> Option < & ParsedQObject > {
250
- self . qobjects
251
- . iter ( )
272
+ self . qobjects ( )
252
273
. find ( |obj| obj. name . rust_unqualified ( ) == id)
253
274
}
254
275
}
@@ -265,8 +286,9 @@ mod tests {
265
286
/// Creates a ParsedCxxQtData with a QObject definition already found
266
287
pub fn create_parsed_cxx_qt_data ( ) -> ParsedCxxQtData {
267
288
let mut cxx_qt_data = ParsedCxxQtData :: new ( format_ident ! ( "ffi" ) , None ) ;
268
- cxx_qt_data. qobjects . push ( create_parsed_qobject ( ) ) ;
269
- cxx_qt_data. qobjects . push ( create_parsed_qobject ( ) ) ;
289
+ cxx_qt_data
290
+ . qobjects
291
+ . push ( vec ! [ create_parsed_qobject( ) , create_parsed_qobject( ) ] ) ;
270
292
cxx_qt_data
271
293
}
272
294
@@ -376,9 +398,9 @@ mod tests {
376
398
let result = cxx_qt_data. parse_cxx_qt_item ( item) . unwrap ( ) ;
377
399
assert ! ( result. is_none( ) ) ;
378
400
379
- assert_eq ! ( cxx_qt_data. methods. len( ) , 2 ) ;
380
- assert ! ( cxx_qt_data. methods[ 0 ] . is_qinvokable) ;
381
- assert ! ( !cxx_qt_data. methods[ 1 ] . is_qinvokable)
401
+ assert_eq ! ( cxx_qt_data. methods( ) . collect :: < Vec <_>> ( ) . len( ) , 2 ) ;
402
+ assert ! ( cxx_qt_data. methods[ 0 ] [ 0 ] . is_qinvokable) ;
403
+ assert ! ( !cxx_qt_data. methods[ 0 ] [ 1 ] . is_qinvokable)
382
404
}
383
405
384
406
#[ test]
@@ -406,7 +428,7 @@ mod tests {
406
428
} ;
407
429
cxx_qt_data. parse_cxx_qt_item ( item) . unwrap ( ) ;
408
430
assert_eq ! ( cxx_qt_data. methods. len( ) , 1 ) ;
409
- assert_eq ! ( cxx_qt_data. methods[ 0 ] . name. cxx_unqualified( ) , "fooBar" ) ;
431
+ assert_eq ! ( cxx_qt_data. methods[ 0 ] [ 0 ] . name. cxx_unqualified( ) , "fooBar" ) ;
410
432
}
411
433
412
434
#[ test]
@@ -421,9 +443,10 @@ mod tests {
421
443
}
422
444
} ;
423
445
cxx_qt_data. parse_cxx_qt_item ( item) . unwrap ( ) ;
424
- assert_eq ! ( cxx_qt_data. methods. len( ) , 1 ) ;
425
- assert_eq ! ( cxx_qt_data. methods[ 0 ] . name. cxx_unqualified( ) , "foo_bar" ) ;
426
- assert_eq ! ( cxx_qt_data. methods[ 0 ] . name. rust_unqualified( ) , "foo_bar" ) ;
446
+ let methods = & cxx_qt_data. methods [ 0 ] ;
447
+ assert_eq ! ( methods. len( ) , 1 ) ;
448
+ assert_eq ! ( methods[ 0 ] . name. cxx_unqualified( ) , "foo_bar" ) ;
449
+ assert_eq ! ( methods[ 0 ] . name. rust_unqualified( ) , "foo_bar" ) ;
427
450
}
428
451
429
452
#[ test]
@@ -438,8 +461,8 @@ mod tests {
438
461
}
439
462
} ;
440
463
cxx_qt_data. parse_cxx_qt_item ( item) . unwrap ( ) ;
441
- assert_eq ! ( cxx_qt_data. methods. len( ) , 1 ) ;
442
- assert_eq ! ( cxx_qt_data. methods[ 0 ] . name. cxx_unqualified( ) , "renamed" ) ;
464
+ assert_eq ! ( cxx_qt_data. methods[ 0 ] . len( ) , 1 ) ;
465
+ assert_eq ! ( cxx_qt_data. methods[ 0 ] [ 0 ] . name. cxx_unqualified( ) , "renamed" ) ;
443
466
}
444
467
445
468
#[ test]
@@ -611,7 +634,7 @@ mod tests {
611
634
}
612
635
} ;
613
636
cxxqtdata. parse_cxx_qt_item ( block) . unwrap ( ) ;
614
- let signals = & cxxqtdata. signals ;
637
+ let signals = & cxxqtdata. signals ( ) . collect :: < Vec < _ > > ( ) ;
615
638
assert_eq ! ( signals. len( ) , 2 ) ;
616
639
assert ! ( signals[ 0 ] . mutable) ;
617
640
assert ! ( signals[ 1 ] . mutable) ;
@@ -641,7 +664,7 @@ mod tests {
641
664
} ;
642
665
cxxqtdata. parse_cxx_qt_item ( block) . unwrap ( ) ;
643
666
644
- let signals = & cxxqtdata. signals ;
667
+ let signals = & cxxqtdata. signals ( ) . collect :: < Vec < _ > > ( ) ;
645
668
assert_eq ! ( signals. len( ) , 1 ) ;
646
669
assert ! ( signals[ 0 ] . mutable) ;
647
670
assert ! ( !signals[ 0 ] . safe) ;
@@ -716,7 +739,7 @@ mod tests {
716
739
} ;
717
740
718
741
parsed_cxxqtdata. parse_cxx_qt_item ( extern_rust_qt) . unwrap ( ) ;
719
- assert_eq ! ( parsed_cxxqtdata. qobjects. len( ) , 2 ) ;
742
+ assert_eq ! ( parsed_cxxqtdata. qobjects( ) . collect :: < Vec <_>> ( ) . len( ) , 2 ) ;
720
743
721
744
assert ! ( parsed_cxxqtdata
722
745
. find_object( & format_ident!( "MyObject" ) )
@@ -741,7 +764,7 @@ mod tests {
741
764
} ;
742
765
743
766
parsed_cxxqtdata. parse_cxx_qt_item ( extern_rust_qt) . unwrap ( ) ;
744
- assert_eq ! ( parsed_cxxqtdata. qobjects. len( ) , 2 ) ;
767
+ assert_eq ! ( parsed_cxxqtdata. qobjects( ) . collect :: < Vec <_>> ( ) . len( ) , 2 ) ;
745
768
assert_eq ! (
746
769
parsed_cxxqtdata
747
770
. find_object( & format_ident!( "MyObject" ) )
0 commit comments