You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At present, both in the client side and cpi context, using anchor's InstructionData::data() allocates a new `Vec on each call.
For a use case such as
for _ in0..ITERATIONS{// cpi or tx build using InstructionDataImplementor { ... }.data()}
its simply not necessary.
I propose a new trait method InstructionData::write_to, which would allow developers to write to an existing allocation.
pubtraitInstructionData:Discriminator + AnchorSerialize{fndata(&self) -> Vec<u8>{letmut d = Self::discriminator().to_vec();
d.append(&mutself.try_to_vec().expect("Should always serialize"));
d
}/// Clears `vec` and writes instruction data to it////// We use a Vec<u8> here because of the additional flexibility of re-allocation (only if necessary),/// and because the data field in `Instruction` expects a `Vec<u8>`.fnwrite_to(&self,mutvec:&mutVec<u8>){// Clear vector
vec.clear();// Write discriminator and then Self
borsh::to_writer(&mut vec,&Self::DISCRIMINATOR).expect("Discriminator is infallibly serializable");
borsh::to_writer(vec,&self).expect("InstructionData should be infallibly serializable");}}
Example usage
letmut instruction = Instruction{ ...};for _ in0..ITERATIONS{// update ix program_id, accounts if needed// update instruction dataInstructionDataImplementor{ .. }.write_to(&mut instruction.data);// cpi or tx build}
The text was updated successfully, but these errors were encountered:
At present, both in the client side and cpi context, using anchor's
InstructionData::data()
allocates a new `Vec on each call.For a use case such as
its simply not necessary.
I propose a new trait method
InstructionData::write_to
, which would allow developers to write to an existing allocation.Example usage
The text was updated successfully, but these errors were encountered: