@@ -995,16 +995,13 @@ impl CodeGenerator for Type {
995
995
996
996
let rust_name = ctx. rust_ident ( & name) ;
997
997
998
- ctx. options ( ) . for_each_callback ( |cb| {
999
- cb. new_item_found (
1000
- DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
1001
- DiscoveredItem :: Alias {
1002
- alias_name : rust_name. to_string ( ) ,
1003
- alias_for : DiscoveredItemId :: new (
1004
- inner_item. id ( ) . as_usize ( ) ,
1005
- ) ,
1006
- } ,
1007
- ) ;
998
+ utils:: call_discovered_item_callback ( ctx, item, || {
999
+ DiscoveredItem :: Alias {
1000
+ alias_name : rust_name. to_string ( ) ,
1001
+ alias_for : DiscoveredItemId :: new (
1002
+ inner_item. id ( ) . as_usize ( ) ,
1003
+ ) ,
1004
+ }
1008
1005
} ) ;
1009
1006
1010
1007
let mut tokens = if let Some ( comment) = item. comment ( ctx) {
@@ -2481,28 +2478,23 @@ impl CodeGenerator for CompInfo {
2481
2478
2482
2479
let is_rust_union = is_union && struct_layout. is_rust_union ( ) ;
2483
2480
2484
- let discovered_id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
2485
- ctx. options ( ) . for_each_callback ( |cb| {
2486
- let discovered_item = match self . kind ( ) {
2487
- CompKind :: Struct => DiscoveredItem :: Struct {
2488
- original_name : item
2489
- . kind ( )
2490
- . expect_type ( )
2491
- . name ( )
2492
- . map ( String :: from) ,
2493
- final_name : canonical_ident. to_string ( ) ,
2494
- } ,
2495
- CompKind :: Union => DiscoveredItem :: Union {
2496
- original_name : item
2497
- . kind ( )
2498
- . expect_type ( )
2499
- . name ( )
2500
- . map ( String :: from) ,
2501
- final_name : canonical_ident. to_string ( ) ,
2502
- } ,
2503
- } ;
2504
-
2505
- cb. new_item_found ( discovered_id, discovered_item) ;
2481
+ utils:: call_discovered_item_callback ( ctx, item, || match self . kind ( ) {
2482
+ CompKind :: Struct => DiscoveredItem :: Struct {
2483
+ original_name : item
2484
+ . kind ( )
2485
+ . expect_type ( )
2486
+ . name ( )
2487
+ . map ( String :: from) ,
2488
+ final_name : canonical_ident. to_string ( ) ,
2489
+ } ,
2490
+ CompKind :: Union => DiscoveredItem :: Union {
2491
+ original_name : item
2492
+ . kind ( )
2493
+ . expect_type ( )
2494
+ . name ( )
2495
+ . map ( String :: from) ,
2496
+ final_name : canonical_ident. to_string ( ) ,
2497
+ } ,
2506
2498
} ) ;
2507
2499
2508
2500
// The custom derives callback may return a list of derive attributes;
@@ -2700,6 +2692,7 @@ impl CodeGenerator for CompInfo {
2700
2692
}
2701
2693
2702
2694
let mut method_names = Default :: default ( ) ;
2695
+ let discovered_id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
2703
2696
if ctx. options ( ) . codegen_config . methods ( ) {
2704
2697
for method in self . methods ( ) {
2705
2698
assert_ne ! ( method. kind( ) , MethodKind :: Constructor ) ;
@@ -3021,7 +3014,6 @@ impl Method {
3021
3014
3022
3015
// First of all, output the actual function.
3023
3016
let function_item = ctx. resolve_item ( self . signature ( ) ) ;
3024
- let id = DiscoveredItemId :: new ( function_item. id ( ) . as_usize ( ) ) ;
3025
3017
if !function_item. process_before_codegen ( ctx, result) {
3026
3018
return ;
3027
3019
}
@@ -3068,14 +3060,11 @@ impl Method {
3068
3060
3069
3061
method_names. insert ( name. clone ( ) ) ;
3070
3062
3071
- ctx. options ( ) . for_each_callback ( |cb| {
3072
- cb. new_item_found (
3073
- id,
3074
- DiscoveredItem :: Method {
3075
- parent : parent_id,
3076
- final_name : name. clone ( ) ,
3077
- } ,
3078
- )
3063
+ utils:: call_discovered_item_callback ( ctx, function_item, || {
3064
+ DiscoveredItem :: Method {
3065
+ parent : parent_id,
3066
+ final_name : name. clone ( ) ,
3067
+ }
3079
3068
} ) ;
3080
3069
3081
3070
let mut function_name = function_item. canonical_name ( ctx) ;
@@ -3783,13 +3772,10 @@ impl CodeGenerator for Enum {
3783
3772
let repr = repr. to_rust_ty_or_opaque ( ctx, item) ;
3784
3773
let has_typedef = ctx. is_enum_typedef_combo ( item. id ( ) ) ;
3785
3774
3786
- ctx. options ( ) . for_each_callback ( |cb| {
3787
- cb. new_item_found (
3788
- DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
3789
- DiscoveredItem :: Enum {
3790
- final_name : name. to_string ( ) ,
3791
- } ,
3792
- ) ;
3775
+ utils:: call_discovered_item_callback ( ctx, item, || {
3776
+ DiscoveredItem :: Enum {
3777
+ final_name : name. to_string ( ) ,
3778
+ }
3793
3779
} ) ;
3794
3780
3795
3781
let mut builder =
@@ -4553,7 +4539,6 @@ impl CodeGenerator for Function {
4553
4539
) -> Self :: Return {
4554
4540
debug ! ( "<Function as CodeGenerator>::codegen: item = {item:?}" ) ;
4555
4541
debug_assert ! ( item. is_enabled_for_codegen( ctx) ) ;
4556
- let id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
4557
4542
4558
4543
let is_internal = matches ! ( self . linkage( ) , Linkage :: Internal ) ;
4559
4544
@@ -4664,13 +4649,10 @@ impl CodeGenerator for Function {
4664
4649
if times_seen > 0 {
4665
4650
write ! ( & mut canonical_name, "{times_seen}" ) . unwrap ( ) ;
4666
4651
}
4667
- ctx. options ( ) . for_each_callback ( |cb| {
4668
- cb. new_item_found (
4669
- id,
4670
- DiscoveredItem :: Function {
4671
- final_name : canonical_name. to_string ( ) ,
4672
- } ,
4673
- ) ;
4652
+ utils:: call_discovered_item_callback ( ctx, item, || {
4653
+ DiscoveredItem :: Function {
4654
+ final_name : canonical_name. to_string ( ) ,
4655
+ }
4674
4656
} ) ;
4675
4657
4676
4658
let link_name_attr = self . link_name ( ) . or_else ( || {
@@ -5211,6 +5193,7 @@ pub(crate) mod utils {
5211
5193
use super :: helpers:: BITFIELD_UNIT ;
5212
5194
use super :: serialize:: CSerialize ;
5213
5195
use super :: { error, CodegenError , CodegenResult , ToRustTyOrOpaque } ;
5196
+ use crate :: callbacks:: DiscoveredItemId ;
5214
5197
use crate :: ir:: context:: BindgenContext ;
5215
5198
use crate :: ir:: context:: TypeId ;
5216
5199
use crate :: ir:: function:: { Abi , ClangAbi , FunctionSig } ;
@@ -5940,4 +5923,17 @@ pub(crate) mod utils {
5940
5923
5941
5924
true
5942
5925
}
5926
+
5927
+ pub ( super ) fn call_discovered_item_callback (
5928
+ ctx : & BindgenContext ,
5929
+ item : & Item ,
5930
+ discovered_item_creator : impl Fn ( ) -> crate :: callbacks:: DiscoveredItem ,
5931
+ ) {
5932
+ ctx. options ( ) . for_each_callback ( |cb| {
5933
+ cb. new_item_found (
5934
+ DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
5935
+ discovered_item_creator ( ) ,
5936
+ ) ;
5937
+ } ) ;
5938
+ }
5943
5939
}
0 commit comments