Skip to content

Commit 5b1652c

Browse files
committed
add BindgenOptions::require_syn method
1 parent 3842b11 commit 5b1652c

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

src/lib.rs

+36-35
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,11 @@ struct BindgenOptions {
20292029
impl ::std::panic::UnwindSafe for BindgenOptions {}
20302030

20312031
impl BindgenOptions {
2032+
/// Whether any of the enabled options requires `syn`.
2033+
fn require_syn(&self) -> bool {
2034+
self.sort_semantically
2035+
}
2036+
20322037
fn build(&mut self) {
20332038
let mut regex_sets = [
20342039
&mut self.allowlisted_vars,
@@ -2455,13 +2460,13 @@ impl Bindings {
24552460

24562461
let (items, options, warnings) = codegen::codegen(context);
24572462

2458-
if options.sort_semantically {
2463+
let module = if options.require_syn() {
24592464
let module_wrapped_tokens =
24602465
quote!(mod wrapper_for_sorting_hack { #( #items )* });
24612466

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.
24652470
// One caveat is that some of the items coming from `quote`d output might have
24662471
// multiple items within them. Hence, we have to wrap the incoming in a `mod`.
24672472
// The two `unwrap`s here are deliberate because
@@ -2475,46 +2480,42 @@ impl Bindings {
24752480
.unwrap()
24762481
.1;
24772482

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+
}
24982505

24992506
let synful_items = syn_parsed_items
25002507
.into_iter()
25012508
.map(|item| item.into_token_stream());
25022509

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+
};
25112514

25122515
Ok(Bindings {
25132516
options,
25142517
warnings,
2515-
module: quote! {
2516-
#( #items )*
2517-
},
2518+
module,
25182519
})
25192520
}
25202521

0 commit comments

Comments
 (0)