add `BindgenOptions::require_syn` method
@@ -2029,6 +2029,11 @@ struct BindgenOptions {
impl ::std::panic::UnwindSafe for BindgenOptions {}
impl BindgenOptions {
+ /// Whether any of the enabled options requires `syn`.
+ fn require_syn(&self) -> bool {
+ self.sort_semantically
+ }
+
fn build(&mut self) {
let mut regex_sets = [
&mut self.allowlisted_vars,
@@ -2455,13 +2460,13 @@ impl Bindings {
let (items, options, warnings) = codegen::codegen(context);
- if options.sort_semantically {
+ let module = if options.require_syn() {
let module_wrapped_tokens =
quote!(mod wrapper_for_sorting_hack { #( #items )* });
- // This semantically sorting business is a hack, for now. This means that we are
- // re-parsing already generated code using `syn` (as opposed to `quote`) because
- // `syn` provides us more control over the elements.
+ // This syn business is a hack, for now. This means that we are re-parsing already
+ // generated code using `syn` (as opposed to `quote`) because `syn` provides us more
+ // control over the elements.
// One caveat is that some of the items coming from `quote`d output might have
// multiple items within them. Hence, we have to wrap the incoming in a `mod`.
// The two `unwrap`s here are deliberate because
@@ -2475,6 +2480,7 @@ impl Bindings {
.unwrap()
.1;
+ if options.sort_semantically {
syn_parsed_items.sort_by_key(|item| match item {
syn::Item::Type(_) => 0,
syn::Item::Struct(_) => 1,
@@ -2495,26 +2501,21 @@ impl Bindings {
syn::Item::Macro2(_) => 16,
_ => 18,
});
+ }
let synful_items = syn_parsed_items
.into_iter()
.map(|item| item.into_token_stream());
- return Ok(Bindings {
- options,
- warnings,
- module: quote! {
- #( #synful_items )*
- },
- });
- }
+ quote! { #( #synful_items )* }
+ } else {
+ quote! { #( #items )* }
+ };
Ok(Bindings {
options,
warnings,
- module: quote! {
- #( #items )*
- },
+ module,
})
}
