diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 49deb3e206..a2e0e5508a 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -3803,8 +3803,9 @@ fn serialize_idl(idl: &Idl) -> Result> { } fn serialize_idl_ix(ix_inner: anchor_lang::idl::IdlInstruction) -> Result> { - let mut data = anchor_lang::idl::IDL_IX_TAG.to_le_bytes().to_vec(); - data.append(&mut ix_inner.try_to_vec()?); + let mut data = Vec::with_capacity(1024); + data.extend_from_slice(&anchor_lang::idl::IDL_IX_TAG.to_le_bytes()); + ix_inner.serialize(&mut data)?; Ok(data) } diff --git a/lang/attribute/event/src/lib.rs b/lang/attribute/event/src/lib.rs index cbe7705d89..652648e2cb 100644 --- a/lang/attribute/event/src/lib.rs +++ b/lang/attribute/event/src/lib.rs @@ -21,12 +21,9 @@ pub fn event( let event_name = &event_strct.ident; let discriminator: proc_macro2::TokenStream = { - let discriminator_preimage = format!("event:{event_name}"); - let mut discriminator = [0u8; 8]; - discriminator.copy_from_slice( - &anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8], - ); - format!("{discriminator:?}").parse().unwrap() + let discriminator_preimage = format!("event:{event_name}").into_bytes(); + let discriminator = anchor_syn::hash::hash(&discriminator_preimage); + format!("{:?}", &discriminator.0[..8]).parse().unwrap() }; let ret = quote! { @@ -35,9 +32,10 @@ pub fn event( impl anchor_lang::Event for #event_name { fn data(&self) -> Vec { - let mut d = #discriminator.to_vec(); - d.append(&mut self.try_to_vec().unwrap()); - d + let mut data = Vec::with_capacity(1024); + data.extend_from_slice(&#discriminator); + self.serialize(&mut data).unwrap(); + data } } diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 867d776b91..ac3b7c08b2 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -266,9 +266,10 @@ pub trait ZeroCopy: Discriminator + Copy + Clone + Zeroable + Pod {} /// to an instruction. pub trait InstructionData: Discriminator + AnchorSerialize { fn data(&self) -> Vec { - let mut d = Self::discriminator().to_vec(); - d.append(&mut self.try_to_vec().expect("Should always serialize")); - d + let mut data = Vec::with_capacity(1024); + data.extend_from_slice(&Self::discriminator()); + self.serialize(&mut data).unwrap(); + data } } diff --git a/lang/syn/src/codegen/program/cpi.rs b/lang/syn/src/codegen/program/cpi.rs index 117fbf7b2d..31522192a4 100644 --- a/lang/syn/src/codegen/program/cpi.rs +++ b/lang/syn/src/codegen/program/cpi.rs @@ -34,10 +34,10 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { ) -> #method_ret { let ix = { let ix = instruction::#ix_variant; - let mut ix_data = AnchorSerialize::try_to_vec(&ix) + let mut data = Vec::with_capacity(1024); + data.extend_from_slice(&#sighash_tts); + AnchorSerialize::serialize(&ix, &mut data) .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotSerialize)?; - let mut data = #sighash_tts.to_vec(); - data.append(&mut ix_data); let accounts = ctx.to_account_metas(None); anchor_lang::solana_program::instruction::Instruction { program_id: ctx.program.key(),