@@ -993,16 +993,13 @@ impl CodeGenerator for Type {
993993
994994 let rust_name = ctx. rust_ident ( & name) ;
995995
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+ }
10061003 } ) ;
10071004
10081005 let mut tokens = if let Some ( comment) = item. comment ( ctx) {
@@ -2477,28 +2474,23 @@ impl CodeGenerator for CompInfo {
24772474
24782475 let is_rust_union = is_union && struct_layout. is_rust_union ( ) ;
24792476
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+ } ,
25022494 } ) ;
25032495
25042496 // The custom derives callback may return a list of derive attributes;
@@ -2696,6 +2688,7 @@ impl CodeGenerator for CompInfo {
26962688 }
26972689
26982690 let mut method_names = Default :: default ( ) ;
2691+ let discovered_id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
26992692 if ctx. options ( ) . codegen_config . methods ( ) {
27002693 for method in self . methods ( ) {
27012694 assert_ne ! ( method. kind( ) , MethodKind :: Constructor ) ;
@@ -3017,7 +3010,6 @@ impl Method {
30173010
30183011 // First of all, output the actual function.
30193012 let function_item = ctx. resolve_item ( self . signature ( ) ) ;
3020- let id = DiscoveredItemId :: new ( function_item. id ( ) . as_usize ( ) ) ;
30213013 if !function_item. process_before_codegen ( ctx, result) {
30223014 return ;
30233015 }
@@ -3064,14 +3056,11 @@ impl Method {
30643056
30653057 method_names. insert ( name. clone ( ) ) ;
30663058
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+ }
30753064 } ) ;
30763065
30773066 let mut function_name = function_item. canonical_name ( ctx) ;
@@ -3801,13 +3790,10 @@ impl CodeGenerator for Enum {
38013790 let repr = repr. to_rust_ty_or_opaque ( ctx, item) ;
38023791 let has_typedef = ctx. is_enum_typedef_combo ( item. id ( ) ) ;
38033792
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+ }
38113797 } ) ;
38123798
38133799 let mut builder = EnumBuilder :: new (
@@ -4592,7 +4578,6 @@ impl CodeGenerator for Function {
45924578 ) -> Self :: Return {
45934579 debug ! ( "<Function as CodeGenerator>::codegen: item = {item:?}" ) ;
45944580 debug_assert ! ( item. is_enabled_for_codegen( ctx) ) ;
4595- let id = DiscoveredItemId :: new ( item. id ( ) . as_usize ( ) ) ;
45964581
45974582 let is_internal = matches ! ( self . linkage( ) , Linkage :: Internal ) ;
45984583
@@ -4709,13 +4694,10 @@ impl CodeGenerator for Function {
47094694 if times_seen > 0 {
47104695 write ! ( & mut canonical_name, "{times_seen}" ) . unwrap ( ) ;
47114696 }
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+ }
47194701 } ) ;
47204702
47214703 let link_name_attr = self . link_name ( ) . or_else ( || {
@@ -5256,6 +5238,7 @@ pub(crate) mod utils {
52565238 use super :: helpers:: BITFIELD_UNIT ;
52575239 use super :: serialize:: CSerialize ;
52585240 use super :: { error, CodegenError , CodegenResult , ToRustTyOrOpaque } ;
5241+ use crate :: callbacks:: DiscoveredItemId ;
52595242 use crate :: ir:: context:: BindgenContext ;
52605243 use crate :: ir:: context:: TypeId ;
52615244 use crate :: ir:: function:: { Abi , ClangAbi , FunctionSig } ;
@@ -5985,4 +5968,17 @@ pub(crate) mod utils {
59855968
59865969 true
59875970 }
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+ }
59885984}
0 commit comments