@@ -2029,6 +2029,11 @@ struct BindgenOptions {
2029
2029
impl :: std:: panic:: UnwindSafe for BindgenOptions { }
2030
2030
2031
2031
impl BindgenOptions {
2032
+ /// Whether any of the enabled options requires `syn`.
2033
+ fn require_syn ( & self ) -> bool {
2034
+ self . sort_semantically
2035
+ }
2036
+
2032
2037
fn build ( & mut self ) {
2033
2038
let mut regex_sets = [
2034
2039
& mut self . allowlisted_vars ,
@@ -2455,13 +2460,13 @@ impl Bindings {
2455
2460
2456
2461
let ( items, options, warnings) = codegen:: codegen ( context) ;
2457
2462
2458
- if options. sort_semantically {
2463
+ let module = if options. require_syn ( ) {
2459
2464
let module_wrapped_tokens =
2460
2465
quote ! ( mod wrapper_for_sorting_hack { #( #items ) * } ) ;
2461
2466
2462
- // This semantically sorting business is a hack, for now. This means that we are
2463
- // re-parsing already generated code using `syn` (as opposed to `quote`) because
2464
- // `syn` provides us more control over the elements.
2467
+ // This syn business is a hack, for now. This means that we are re-parsing already
2468
+ // generated code using `syn` (as opposed to `quote`) because `syn` provides us more
2469
+ // control over the elements.
2465
2470
// One caveat is that some of the items coming from `quote`d output might have
2466
2471
// multiple items within them. Hence, we have to wrap the incoming in a `mod`.
2467
2472
// The two `unwrap`s here are deliberate because
@@ -2475,46 +2480,42 @@ impl Bindings {
2475
2480
. unwrap ( )
2476
2481
. 1 ;
2477
2482
2478
- syn_parsed_items. sort_by_key ( |item| match item {
2479
- syn:: Item :: Type ( _) => 0 ,
2480
- syn:: Item :: Struct ( _) => 1 ,
2481
- syn:: Item :: Const ( _) => 2 ,
2482
- syn:: Item :: Fn ( _) => 3 ,
2483
- syn:: Item :: Enum ( _) => 4 ,
2484
- syn:: Item :: Union ( _) => 5 ,
2485
- syn:: Item :: Static ( _) => 6 ,
2486
- syn:: Item :: Trait ( _) => 7 ,
2487
- syn:: Item :: TraitAlias ( _) => 8 ,
2488
- syn:: Item :: Impl ( _) => 9 ,
2489
- syn:: Item :: Mod ( _) => 10 ,
2490
- syn:: Item :: Use ( _) => 11 ,
2491
- syn:: Item :: Verbatim ( _) => 12 ,
2492
- syn:: Item :: ExternCrate ( _) => 13 ,
2493
- syn:: Item :: ForeignMod ( _) => 14 ,
2494
- syn:: Item :: Macro ( _) => 15 ,
2495
- syn:: Item :: Macro2 ( _) => 16 ,
2496
- _ => 18 ,
2497
- } ) ;
2483
+ if options. sort_semantically {
2484
+ syn_parsed_items. sort_by_key ( |item| match item {
2485
+ syn:: Item :: Type ( _) => 0 ,
2486
+ syn:: Item :: Struct ( _) => 1 ,
2487
+ syn:: Item :: Const ( _) => 2 ,
2488
+ syn:: Item :: Fn ( _) => 3 ,
2489
+ syn:: Item :: Enum ( _) => 4 ,
2490
+ syn:: Item :: Union ( _) => 5 ,
2491
+ syn:: Item :: Static ( _) => 6 ,
2492
+ syn:: Item :: Trait ( _) => 7 ,
2493
+ syn:: Item :: TraitAlias ( _) => 8 ,
2494
+ syn:: Item :: Impl ( _) => 9 ,
2495
+ syn:: Item :: Mod ( _) => 10 ,
2496
+ syn:: Item :: Use ( _) => 11 ,
2497
+ syn:: Item :: Verbatim ( _) => 12 ,
2498
+ syn:: Item :: ExternCrate ( _) => 13 ,
2499
+ syn:: Item :: ForeignMod ( _) => 14 ,
2500
+ syn:: Item :: Macro ( _) => 15 ,
2501
+ syn:: Item :: Macro2 ( _) => 16 ,
2502
+ _ => 18 ,
2503
+ } ) ;
2504
+ }
2498
2505
2499
2506
let synful_items = syn_parsed_items
2500
2507
. into_iter ( )
2501
2508
. map ( |item| item. into_token_stream ( ) ) ;
2502
2509
2503
- return Ok ( Bindings {
2504
- options,
2505
- warnings,
2506
- module : quote ! {
2507
- #( #synful_items ) *
2508
- } ,
2509
- } ) ;
2510
- }
2510
+ quote ! { #( #synful_items ) * }
2511
+ } else {
2512
+ quote ! { #( #items ) * }
2513
+ } ;
2511
2514
2512
2515
Ok ( Bindings {
2513
2516
options,
2514
2517
warnings,
2515
- module : quote ! {
2516
- #( #items ) *
2517
- } ,
2518
+ module,
2518
2519
} )
2519
2520
}
2520
2521
0 commit comments