Skip to content

Commit

Permalink
refactor(ast_codegen): flatten code (#4743)
Browse files Browse the repository at this point in the history
A bit of code style simplification after #4731. Remove unnecessary nesting and rename some vars for consistency.
  • Loading branch information
overlookmotel committed Aug 7, 2024
1 parent aeed29f commit f36500a
Showing 1 changed file with 36 additions and 39 deletions.
75 changes: 36 additions & 39 deletions tasks/ast_codegen/src/generators/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,59 @@ impl Generator for DeriveCloneIn {

fn derive_enum(def: &EnumDef) -> TokenStream {
let ty_ident = def.ident();
let (alloc, body) = {
let mut used_alloc = false;
let matches = def
.all_variants()
.map(|var| {
let ident = var.ident();
if var.is_unit() {
quote!(Self :: #ident => #ty_ident :: #ident)
} else {
used_alloc = true;
quote!(Self :: #ident(it) => #ty_ident :: #ident(it.clone_in(alloc)))
}
})
.collect_vec();
let alloc_ident = if used_alloc { format_ident!("alloc") } else { format_ident!("_") };
(
alloc_ident,
quote! {
match self {
#(#matches),*
}
},
)

let mut used_alloc = false;
let matches = def
.all_variants()
.map(|var| {
let ident = var.ident();
if var.is_unit() {
quote!(Self :: #ident => #ty_ident :: #ident)
} else {
used_alloc = true;
quote!(Self :: #ident(it) => #ty_ident :: #ident(it.clone_in(alloc)))
}
})
.collect_vec();

let alloc_ident = if used_alloc { format_ident!("alloc") } else { format_ident!("_") };
let body = quote! {
match self {
#(#matches),*
}
};
impl_clone_in(&ty_ident, def.has_lifetime, &alloc, &body)

impl_clone_in(&ty_ident, def.has_lifetime, &alloc_ident, &body)
}

fn derive_struct(def: &StructDef) -> TokenStream {
let ty_ident = def.ident();
let (alloc, body) = {
let (alloc_ident, body) = if def.fields.is_empty() {
(format_ident!("_"), TokenStream::default())
} else {
let fields = def.fields.iter().map(|field| {
let ident = field.ident();
quote!(#ident: self.#ident.clone_in(alloc))
});
(format_ident!("alloc"), quote!({ #(#fields),* }))
};
(alloc_ident, quote!( #ty_ident #body ))

let (alloc_ident, body) = if def.fields.is_empty() {
(format_ident!("_"), quote!(#ty_ident))
} else {
let fields = def.fields.iter().map(|field| {
let ident = field.ident();
quote!(#ident: self.#ident.clone_in(alloc))
});
(format_ident!("alloc"), quote!(#ty_ident { #(#fields),* }))
};
impl_clone_in(&ty_ident, def.has_lifetime, &alloc, &body)

impl_clone_in(&ty_ident, def.has_lifetime, &alloc_ident, &body)
}

fn impl_clone_in(
ty_ident: &Ident,
has_lifetime: bool,
alloc: &Ident,
alloc_ident: &Ident,
body: &TokenStream,
) -> TokenStream {
if has_lifetime {
quote! {
endl!();
impl <'old_alloc, 'new_alloc> CloneIn<'new_alloc> for #ty_ident<'old_alloc> {
type Cloned = #ty_ident<'new_alloc>;
fn clone_in(&self, #alloc: &'new_alloc Allocator) -> Self::Cloned {
fn clone_in(&self, #alloc_ident: &'new_alloc Allocator) -> Self::Cloned {
#body
}
}
Expand All @@ -117,7 +114,7 @@ fn impl_clone_in(
endl!();
impl <'alloc> CloneIn<'alloc> for #ty_ident {
type Cloned = #ty_ident;
fn clone_in(&self, #alloc: &'alloc Allocator) -> Self::Cloned {
fn clone_in(&self, #alloc_ident: &'alloc Allocator) -> Self::Cloned {
#body
}
}
Expand Down

0 comments on commit f36500a

Please sign in to comment.