@@ -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) {
@@ -2480,28 +2477,23 @@ impl CodeGenerator for CompInfo {
2480
2477
2481
2478
let is_rust_union = is_union && struct_layout. is_rust_union ( ) ;
2482
2479
2483
- let discovered_id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
2484
- ctx. options ( ) . for_each_callback ( |cb| {
2485
- let discovered_item = match self . kind ( ) {
2486
- CompKind :: Struct => DiscoveredItem :: Struct {
2487
- original_name : item
2488
- . kind ( )
2489
- . expect_type ( )
2490
- . name ( )
2491
- . map ( String :: from) ,
2492
- final_name : canonical_ident. to_string ( ) ,
2493
- } ,
2494
- CompKind :: Union => DiscoveredItem :: Union {
2495
- original_name : item
2496
- . kind ( )
2497
- . expect_type ( )
2498
- . name ( )
2499
- . map ( String :: from) ,
2500
- final_name : canonical_ident. to_string ( ) ,
2501
- } ,
2502
- } ;
2503
-
2504
- cb. new_item_found ( discovered_id, discovered_item) ;
2480
+ utils:: call_discovered_item_callback ( ctx, item, || match self . kind ( ) {
2481
+ CompKind :: Struct => DiscoveredItem :: Struct {
2482
+ original_name : item
2483
+ . kind ( )
2484
+ . expect_type ( )
2485
+ . name ( )
2486
+ . map ( String :: from) ,
2487
+ final_name : canonical_ident. to_string ( ) ,
2488
+ } ,
2489
+ CompKind :: Union => DiscoveredItem :: Union {
2490
+ original_name : item
2491
+ . kind ( )
2492
+ . expect_type ( )
2493
+ . name ( )
2494
+ . map ( String :: from) ,
2495
+ final_name : canonical_ident. to_string ( ) ,
2496
+ } ,
2505
2497
} ) ;
2506
2498
2507
2499
// The custom derives callback may return a list of derive attributes;
@@ -2699,6 +2691,7 @@ impl CodeGenerator for CompInfo {
2699
2691
}
2700
2692
2701
2693
let mut method_names = Default :: default ( ) ;
2694
+ let discovered_id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
2702
2695
if ctx. options ( ) . codegen_config . methods ( ) {
2703
2696
for method in self . methods ( ) {
2704
2697
assert_ne ! ( method. kind( ) , MethodKind :: Constructor ) ;
@@ -3020,7 +3013,6 @@ impl Method {
3020
3013
3021
3014
// First of all, output the actual function.
3022
3015
let function_item = ctx. resolve_item ( self . signature ( ) ) ;
3023
- let id = DiscoveredItemId :: new ( function_item. id ( ) . as_usize ( ) ) ;
3024
3016
if !function_item. process_before_codegen ( ctx, result) {
3025
3017
return ;
3026
3018
}
@@ -3067,14 +3059,11 @@ impl Method {
3067
3059
3068
3060
method_names. insert ( name. clone ( ) ) ;
3069
3061
3070
- ctx. options ( ) . for_each_callback ( |cb| {
3071
- cb. new_item_found (
3072
- id,
3073
- DiscoveredItem :: Method {
3074
- parent : parent_id,
3075
- final_name : name. clone ( ) ,
3076
- } ,
3077
- ) ;
3062
+ utils:: call_discovered_item_callback ( ctx, function_item, || {
3063
+ DiscoveredItem :: Method {
3064
+ parent : parent_id,
3065
+ final_name : name. clone ( ) ,
3066
+ }
3078
3067
} ) ;
3079
3068
3080
3069
let mut function_name = function_item. canonical_name ( ctx) ;
@@ -3804,13 +3793,10 @@ impl CodeGenerator for Enum {
3804
3793
let repr = repr. to_rust_ty_or_opaque ( ctx, item) ;
3805
3794
let has_typedef = ctx. is_enum_typedef_combo ( item. id ( ) ) ;
3806
3795
3807
- ctx. options ( ) . for_each_callback ( |cb| {
3808
- cb. new_item_found (
3809
- DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
3810
- DiscoveredItem :: Enum {
3811
- final_name : name. to_string ( ) ,
3812
- } ,
3813
- ) ;
3796
+ utils:: call_discovered_item_callback ( ctx, item, || {
3797
+ DiscoveredItem :: Enum {
3798
+ final_name : name. to_string ( ) ,
3799
+ }
3814
3800
} ) ;
3815
3801
3816
3802
let mut builder = EnumBuilder :: new (
@@ -4595,7 +4581,6 @@ impl CodeGenerator for Function {
4595
4581
) -> Self :: Return {
4596
4582
debug ! ( "<Function as CodeGenerator>::codegen: item = {item:?}" ) ;
4597
4583
debug_assert ! ( item. is_enabled_for_codegen( ctx) ) ;
4598
- let id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
4599
4584
4600
4585
let is_internal = matches ! ( self . linkage( ) , Linkage :: Internal ) ;
4601
4586
@@ -4712,13 +4697,10 @@ impl CodeGenerator for Function {
4712
4697
if times_seen > 0 {
4713
4698
write ! ( & mut canonical_name, "{times_seen}" ) . unwrap ( ) ;
4714
4699
}
4715
- ctx. options ( ) . for_each_callback ( |cb| {
4716
- cb. new_item_found (
4717
- id,
4718
- DiscoveredItem :: Function {
4719
- final_name : canonical_name. to_string ( ) ,
4720
- } ,
4721
- ) ;
4700
+ utils:: call_discovered_item_callback ( ctx, item, || {
4701
+ DiscoveredItem :: Function {
4702
+ final_name : canonical_name. to_string ( ) ,
4703
+ }
4722
4704
} ) ;
4723
4705
4724
4706
let link_name_attr = self . link_name ( ) . or_else ( || {
@@ -5259,6 +5241,7 @@ pub(crate) mod utils {
5259
5241
use super :: helpers:: BITFIELD_UNIT ;
5260
5242
use super :: serialize:: CSerialize ;
5261
5243
use super :: { error, CodegenError , CodegenResult , ToRustTyOrOpaque } ;
5244
+ use crate :: callbacks:: DiscoveredItemId ;
5262
5245
use crate :: ir:: context:: BindgenContext ;
5263
5246
use crate :: ir:: context:: TypeId ;
5264
5247
use crate :: ir:: function:: { Abi , ClangAbi , FunctionSig } ;
@@ -5988,4 +5971,17 @@ pub(crate) mod utils {
5988
5971
5989
5972
true
5990
5973
}
5974
+
5975
+ pub ( super ) fn call_discovered_item_callback (
5976
+ ctx : & BindgenContext ,
5977
+ item : & Item ,
5978
+ discovered_item_creator : impl Fn ( ) -> crate :: callbacks:: DiscoveredItem ,
5979
+ ) {
5980
+ ctx. options ( ) . for_each_callback ( |cb| {
5981
+ cb. new_item_found (
5982
+ DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
5983
+ discovered_item_creator ( ) ,
5984
+ ) ;
5985
+ } ) ;
5986
+ }
5991
5987
}
0 commit comments