@@ -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,8 +3059,8 @@ 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 (
3062
+ utils :: call_discovered_item_callback ( ctx , function_item , | | {
3063
+ DiscoveredItem :: Method {
3072
3064
id,
3073
3065
DiscoveredItem : : Method {
3074
3066
parent : parent_id,
@@ -3804,13 +3796,10 @@ impl CodeGenerator for Enum {
3804
3796
let repr = repr. to_rust_ty_or_opaque ( ctx, item) ;
3805
3797
let has_typedef = ctx. is_enum_typedef_combo ( item. id ( ) ) ;
3806
3798
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
- ) ;
3799
+ utils:: call_discovered_item_callback ( ctx, item, || {
3800
+ DiscoveredItem :: Enum {
3801
+ final_name : name. to_string ( ) ,
3802
+ }
3814
3803
} ) ;
3815
3804
3816
3805
let mut builder = EnumBuilder :: new (
@@ -4595,7 +4584,6 @@ impl CodeGenerator for Function {
4595
4584
) -> Self :: Return {
4596
4585
debug ! ( "<Function as CodeGenerator>::codegen: item = {item:?}" ) ;
4597
4586
debug_assert ! ( item. is_enabled_for_codegen( ctx) ) ;
4598
- let id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
4599
4587
4600
4588
let is_internal = matches ! ( self . linkage( ) , Linkage :: Internal ) ;
4601
4589
@@ -4712,13 +4700,10 @@ impl CodeGenerator for Function {
4712
4700
if times_seen > 0 {
4713
4701
write ! ( & mut canonical_name, "{times_seen}" ) . unwrap ( ) ;
4714
4702
}
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
- ) ;
4703
+ utils:: call_discovered_item_callback ( ctx, item, || {
4704
+ DiscoveredItem :: Function {
4705
+ final_name : canonical_name. to_string ( ) ,
4706
+ }
4722
4707
} ) ;
4723
4708
4724
4709
let link_name_attr = self . link_name ( ) . or_else ( || {
@@ -5259,6 +5244,7 @@ pub(crate) mod utils {
5259
5244
use super :: helpers:: BITFIELD_UNIT ;
5260
5245
use super :: serialize:: CSerialize ;
5261
5246
use super :: { error, CodegenError , CodegenResult , ToRustTyOrOpaque } ;
5247
+ use crate :: callbacks:: DiscoveredItemId ;
5262
5248
use crate :: ir:: context:: BindgenContext ;
5263
5249
use crate :: ir:: context:: TypeId ;
5264
5250
use crate :: ir:: function:: { Abi , ClangAbi , FunctionSig } ;
@@ -5988,4 +5974,17 @@ pub(crate) mod utils {
5988
5974
5989
5975
true
5990
5976
}
5977
+
5978
+ pub ( super ) fn call_discovered_item_callback (
5979
+ ctx : & BindgenContext ,
5980
+ item : & Item ,
5981
+ discovered_item_creator : impl Fn ( ) -> crate :: callbacks:: DiscoveredItem ,
5982
+ ) {
5983
+ ctx. options ( ) . for_each_callback ( |cb| {
5984
+ cb. new_item_found (
5985
+ DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ,
5986
+ discovered_item_creator ( ) ,
5987
+ ) ;
5988
+ } ) ;
5989
+ }
5991
5990
}
0 commit comments