Skip to content

Commit

Permalink
lang: Reduce cloning and code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto committed Nov 20, 2024
1 parent f3c3ea3 commit 93ea7ef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lang/syn/src/codegen/program/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn gen_discriminator(namespace: &str, name: impl ToString) -> proc_macro2::T
format!("&{:?}", discriminator).parse().unwrap()
}

pub fn generate_ix_variant(name: String, args: &[IxArg]) -> proc_macro2::TokenStream {
pub fn generate_ix_variant(name: &str, args: &[IxArg]) -> proc_macro2::TokenStream {
let ix_arg_names: Vec<&syn::Ident> = args.iter().map(|arg| &arg.name).collect();
let ix_name_camel = generate_ix_variant_name(name);

Expand All @@ -40,7 +40,7 @@ pub fn generate_ix_variant(name: String, args: &[IxArg]) -> proc_macro2::TokenSt
}
}

pub fn generate_ix_variant_name(name: String) -> proc_macro2::TokenStream {
pub fn generate_ix_variant_name(name: &str) -> proc_macro2::TokenStream {
let n = name.to_camel_case();
n.parse().unwrap()
}
5 changes: 3 additions & 2 deletions lang/syn/src/codegen/program/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let accounts_ident: proc_macro2::TokenStream = format!("crate::cpi::accounts::{}", &ix.anchor_ident.to_string()).parse().unwrap();
let cpi_method = {
let name = &ix.raw_method.sig.ident;
let ix_variant = generate_ix_variant(&name, &ix.args);
let name_str = name.to_string();
let ix_variant = generate_ix_variant(&name_str, &ix.args);
let method_name = &ix.ident;
let args: Vec<&syn::PatType> = ix.args.iter().map(|arg| &arg.raw_arg).collect();
let discriminator = {
let name = generate_ix_variant_name(name.to_string());
let name = generate_ix_variant_name(&name_str);
quote! { <instruction::#name as anchor_lang::Discriminator>::DISCRIMINATOR }
};
let ret_type = &ix.returns.ty.to_token_stream();
Expand Down
7 changes: 4 additions & 3 deletions lang/syn/src/codegen/program/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
.iter()
.map(|ix| {
let ix_arg_names: Vec<&syn::Ident> = ix.args.iter().map(|arg| &arg.name).collect();
let ix_name = generate_ix_variant_name(ix.raw_method.sig.ident.to_string());
let ix_method_name = &ix.raw_method.sig.ident;
let anchor = &ix.anchor_ident;
let variant_arm = generate_ix_variant(ix.raw_method.sig.ident.to_string(), &ix.args);
let ix_method_name_str = ix_method_name.to_string();
let ix_name = generate_ix_variant_name(&ix_method_name_str);
let variant_arm = generate_ix_variant(&ix_method_name_str, &ix.args);
let ix_name_log = format!("Instruction: {ix_name}");
let anchor = &ix.anchor_ident;
let ret_type = &ix.returns.ty.to_token_stream();
let cfgs = &ix.cfgs;
let maybe_set_return_data = match ret_type.to_string().as_str() {
Expand Down

0 comments on commit 93ea7ef

Please sign in to comment.