Skip to content

Commit

Permalink
Scope helpers into impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jun 18, 2024
1 parent 3060fbe commit 7de2454
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ pub(crate) fn derive(input: &DeriveInput) -> Result<TokenStream> {
Data::Union(_) => Err(Error::new_spanned(input, "Unions are not supported")),
}?;

let helpers = specialization();
let dummy_const = format_ident!("_DERIVE_Display_FOR_{}", input.ident);
Ok(quote! {
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const #dummy_const: () = {
#helpers
const _: () = {
#impls
};
})
Expand Down Expand Up @@ -78,6 +75,7 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {

let helper = AttrsHelper::new(&input.attrs);

let helpers = specialization();
let display = helper.display(&input.attrs)?.map(|display| {
let pat = match &data.fields {
Fields::Named(fields) => {
Expand All @@ -93,6 +91,8 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
quote! {
impl #impl_generics core::fmt::Display for #ty #ty_generics #where_clause {
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
#helpers

// NB: This destructures the fields of `self` into named variables (for unnamed
// fields, it uses _0, _1, etc as above). The `#[allow(unused_variables)]`
// section means it doesn't have to parse the individual field references out of
Expand Down Expand Up @@ -373,6 +373,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
}
})
} else if displays.iter().any(Option::is_some) {
let helpers = specialization();
let arms = data
.variants
.iter()
Expand All @@ -397,6 +398,7 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
Ok(quote! {
impl #impl_generics core::fmt::Display for #ty #ty_generics #where_clause {
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
#helpers
#[allow(unused_variables)]
match self {
#(#arms,)*
Expand Down

0 comments on commit 7de2454

Please sign in to comment.