@@ -993,16 +993,13 @@ impl CodeGenerator for Type {
993
993
994
994
let rust_name = ctx. rust_ident ( & name) ;
995
995
996
- ctx. options ( ) . for_each_callback ( |cb| {
997
- cb. new_item_found (
998
- DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
999
- DiscoveredItem :: Alias {
1000
- alias_name : rust_name. to_string ( ) ,
1001
- alias_for : DiscoveredItemId :: new (
1002
- inner_item. id ( ) . as_usize ( ) ,
1003
- ) ,
1004
- } ,
1005
- ) ;
996
+ utils:: call_discovered_item_callback ( ctx, item, || {
997
+ DiscoveredItem :: Alias {
998
+ alias_name : rust_name. to_string ( ) ,
999
+ alias_for : DiscoveredItemId :: new (
1000
+ inner_item. id ( ) . as_usize ( ) ,
1001
+ ) ,
1002
+ }
1006
1003
} ) ;
1007
1004
1008
1005
let mut tokens = if let Some ( comment) = item. comment ( ctx) {
@@ -2477,28 +2474,23 @@ impl CodeGenerator for CompInfo {
2477
2474
2478
2475
let is_rust_union = is_union && struct_layout. is_rust_union ( ) ;
2479
2476
2480
- let discovered_id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
2481
- ctx. options ( ) . for_each_callback ( |cb| {
2482
- let discovered_item = match self . kind ( ) {
2483
- CompKind :: Struct => DiscoveredItem :: Struct {
2484
- original_name : item
2485
- . kind ( )
2486
- . expect_type ( )
2487
- . name ( )
2488
- . map ( String :: from) ,
2489
- final_name : canonical_ident. to_string ( ) ,
2490
- } ,
2491
- CompKind :: Union => DiscoveredItem :: Union {
2492
- original_name : item
2493
- . kind ( )
2494
- . expect_type ( )
2495
- . name ( )
2496
- . map ( String :: from) ,
2497
- final_name : canonical_ident. to_string ( ) ,
2498
- } ,
2499
- } ;
2500
-
2501
- cb. new_item_found ( discovered_id, discovered_item) ;
2477
+ utils:: call_discovered_item_callback ( ctx, item, || match self . kind ( ) {
2478
+ CompKind :: Struct => DiscoveredItem :: Struct {
2479
+ original_name : item
2480
+ . kind ( )
2481
+ . expect_type ( )
2482
+ . name ( )
2483
+ . map ( String :: from) ,
2484
+ final_name : canonical_ident. to_string ( ) ,
2485
+ } ,
2486
+ CompKind :: Union => DiscoveredItem :: Union {
2487
+ original_name : item
2488
+ . kind ( )
2489
+ . expect_type ( )
2490
+ . name ( )
2491
+ . map ( String :: from) ,
2492
+ final_name : canonical_ident. to_string ( ) ,
2493
+ } ,
2502
2494
} ) ;
2503
2495
2504
2496
// The custom derives callback may return a list of derive attributes;
@@ -2696,6 +2688,7 @@ impl CodeGenerator for CompInfo {
2696
2688
}
2697
2689
2698
2690
let mut method_names = Default :: default ( ) ;
2691
+ let discovered_id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
2699
2692
if ctx. options ( ) . codegen_config . methods ( ) {
2700
2693
for method in self . methods ( ) {
2701
2694
assert_ne ! ( method. kind( ) , MethodKind :: Constructor ) ;
@@ -3017,7 +3010,6 @@ impl Method {
3017
3010
3018
3011
// First of all, output the actual function.
3019
3012
let function_item = ctx. resolve_item ( self . signature ( ) ) ;
3020
- let id = DiscoveredItemId :: new ( function_item. id ( ) . as_usize ( ) ) ;
3021
3013
if !function_item. process_before_codegen ( ctx, result) {
3022
3014
return ;
3023
3015
}
@@ -3064,14 +3056,11 @@ impl Method {
3064
3056
3065
3057
method_names. insert ( name. clone ( ) ) ;
3066
3058
3067
- ctx. options ( ) . for_each_callback ( |cb| {
3068
- cb. new_item_found (
3069
- id,
3070
- DiscoveredItem :: Method {
3071
- parent : parent_id,
3072
- final_name : name. clone ( ) ,
3073
- } ,
3074
- ) ;
3059
+ utils:: call_discovered_item_callback ( ctx, function_item, || {
3060
+ DiscoveredItem :: Method {
3061
+ parent : parent_id,
3062
+ final_name : name. clone ( ) ,
3063
+ }
3075
3064
} ) ;
3076
3065
3077
3066
let mut function_name = function_item. canonical_name ( ctx) ;
@@ -3801,13 +3790,10 @@ impl CodeGenerator for Enum {
3801
3790
let repr = repr. to_rust_ty_or_opaque ( ctx, item) ;
3802
3791
let has_typedef = ctx. is_enum_typedef_combo ( item. id ( ) ) ;
3803
3792
3804
- ctx. options ( ) . for_each_callback ( |cb| {
3805
- cb. new_item_found (
3806
- DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
3807
- DiscoveredItem :: Enum {
3808
- final_name : name. to_string ( ) ,
3809
- } ,
3810
- ) ;
3793
+ utils:: call_discovered_item_callback ( ctx, item, || {
3794
+ DiscoveredItem :: Enum {
3795
+ final_name : name. to_string ( ) ,
3796
+ }
3811
3797
} ) ;
3812
3798
3813
3799
let mut builder = EnumBuilder :: new (
@@ -4592,7 +4578,6 @@ impl CodeGenerator for Function {
4592
4578
) -> Self :: Return {
4593
4579
debug ! ( "<Function as CodeGenerator>::codegen: item = {item:?}" ) ;
4594
4580
debug_assert ! ( item. is_enabled_for_codegen( ctx) ) ;
4595
- let id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
4596
4581
4597
4582
let is_internal = matches ! ( self . linkage( ) , Linkage :: Internal ) ;
4598
4583
@@ -4709,13 +4694,10 @@ impl CodeGenerator for Function {
4709
4694
if times_seen > 0 {
4710
4695
write ! ( & mut canonical_name, "{times_seen}" ) . unwrap ( ) ;
4711
4696
}
4712
- ctx. options ( ) . for_each_callback ( |cb| {
4713
- cb. new_item_found (
4714
- id,
4715
- DiscoveredItem :: Function {
4716
- final_name : canonical_name. to_string ( ) ,
4717
- } ,
4718
- ) ;
4697
+ utils:: call_discovered_item_callback ( ctx, item, || {
4698
+ DiscoveredItem :: Function {
4699
+ final_name : canonical_name. to_string ( ) ,
4700
+ }
4719
4701
} ) ;
4720
4702
4721
4703
let link_name_attr = self . link_name ( ) . or_else ( || {
@@ -5256,6 +5238,7 @@ pub(crate) mod utils {
5256
5238
use super :: helpers:: BITFIELD_UNIT ;
5257
5239
use super :: serialize:: CSerialize ;
5258
5240
use super :: { error, CodegenError , CodegenResult , ToRustTyOrOpaque } ;
5241
+ use crate :: callbacks:: DiscoveredItemId ;
5259
5242
use crate :: ir:: context:: BindgenContext ;
5260
5243
use crate :: ir:: context:: TypeId ;
5261
5244
use crate :: ir:: function:: { Abi , ClangAbi , FunctionSig } ;
@@ -5985,4 +5968,17 @@ pub(crate) mod utils {
5985
5968
5986
5969
true
5987
5970
}
5971
+
5972
+ pub ( super ) fn call_discovered_item_callback (
5973
+ ctx : & BindgenContext ,
5974
+ item : & Item ,
5975
+ discovered_item_creator : impl Fn ( ) -> crate :: callbacks:: DiscoveredItem ,
5976
+ ) {
5977
+ ctx. options ( ) . for_each_callback ( |cb| {
5978
+ cb. new_item_found (
5979
+ DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
5980
+ discovered_item_creator ( ) ,
5981
+ ) ;
5982
+ } ) ;
5983
+ }
5988
5984
}
0 commit comments