Skip to content

Commit f1599b5

Browse files
committed
adapt to zero copy refactor & fix tests
1 parent e639599 commit f1599b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+946
-927
lines changed

program-libs/compressed-account/src/compressed_account.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
AnchorDeserialize, AnchorSerialize, CompressedAccountError, Pubkey, TreeType,
1313
};
1414

15+
#[repr(C)]
1516
#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)]
1617
pub struct PackedCompressedAccountWithMerkleContext {
1718
pub compressed_account: CompressedAccount,
@@ -134,6 +135,7 @@ pub struct ReadOnlyCompressedAccount {
134135
pub root_index: u16,
135136
}
136137

138+
#[repr(C)]
137139
#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)]
138140
pub struct PackedReadOnlyCompressedAccount {
139141
pub account_hash: [u8; 32],
@@ -150,6 +152,7 @@ pub struct MerkleContext {
150152
pub tree_type: TreeType,
151153
}
152154

155+
#[repr(C)]
153156
#[derive(
154157
Debug,
155158
Clone,
@@ -228,6 +231,7 @@ pub fn pack_merkle_context(
228231
.collect::<Vec<_>>()
229232
}
230233

234+
#[repr(C)]
231235
#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)]
232236
pub struct CompressedAccount {
233237
pub owner: Pubkey,
@@ -245,6 +249,7 @@ pub struct InCompressedAccount {
245249
pub address: Option<[u8; 32]>,
246250
}
247251

252+
#[repr(C)]
248253
#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)]
249254
pub struct CompressedAccountData {
250255
pub discriminator: [u8; 8],

program-libs/compressed-account/src/instruction_data/cpi_context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
AnchorDeserialize, AnchorSerialize,
88
};
99

10+
#[repr(C)]
1011
#[derive(
1112
AnchorSerialize, AnchorDeserialize, Debug, Clone, Copy, PartialEq, Eq, Default, ZeroCopyMut,
1213
)]

program-libs/compressed-account/src/instruction_data/data.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ pub struct OutputCompressedAccountWithContext {
2626
pub merkle_tree: Pubkey,
2727
}
2828

29+
#[repr(C)]
2930
#[derive(Debug, PartialEq, Default, Clone, AnchorDeserialize, AnchorSerialize, ZeroCopyMut)]
3031
pub struct OutputCompressedAccountWithPackedContext {
3132
pub compressed_account: CompressedAccount,
3233
pub merkle_tree_index: u8,
3334
}
3435

36+
#[repr(C)]
3537
#[derive(
3638
Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize, ZeroCopyMut,
3739
)]
@@ -42,6 +44,7 @@ pub struct NewAddressParamsPacked {
4244
pub address_merkle_tree_root_index: u16,
4345
}
4446

47+
#[repr(C)]
4548
#[derive(
4649
Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize, ZeroCopyMut,
4750
)]
@@ -92,6 +95,7 @@ pub struct NewAddressParamsAssigned {
9295
pub assigned_account_index: Option<u8>,
9396
}
9497

98+
#[repr(C)]
9599
#[derive(
96100
Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize, ZeroCopyMut,
97101
)]

program-libs/compressed-account/src/instruction_data/with_readonly.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::ops::Deref;
22

3-
use light_zero_copy::{errors::ZeroCopyError, slice::ZeroCopySliceBorsh, traits::ZeroCopyAt, ZeroCopyMut};
3+
use light_zero_copy::{
4+
errors::ZeroCopyError, slice::ZeroCopySliceBorsh, traits::ZeroCopyAt, ZeroCopyMut,
5+
};
46
use zerocopy::{
57
little_endian::{U16, U32, U64},
68
FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned,
@@ -30,6 +32,7 @@ use crate::{
3032
AnchorDeserialize, AnchorSerialize, CompressedAccountError,
3133
};
3234

35+
#[repr(C)]
3336
#[derive(Debug, Default, PartialEq, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)]
3437
pub struct InAccount {
3538
pub discriminator: [u8; 8],
@@ -193,6 +196,7 @@ impl<'a> Deref for ZInAccount<'a> {
193196
}
194197
}
195198

199+
#[repr(C)]
196200
#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)]
197201
pub struct InstructionDataInvokeCpiWithReadOnly {
198202
/// 0 With program ids

program-libs/compressed-account/src/instruction_data/zero_copy_set.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use light_zero_copy::borsh::Deserialize;
1+
use light_zero_copy::traits::ZeroCopyAt;
22
use zerocopy::little_endian::U16;
33

44
use crate::{
@@ -53,7 +53,7 @@ impl ZInAccountMut<'_> {
5353
&mut self,
5454
discriminator: [u8; 8],
5555
data_hash: [u8; 32],
56-
merkle_context: &<PackedMerkleContext as Deserialize>::Output,
56+
merkle_context: &<PackedMerkleContext as ZeroCopyAt>::ZeroCopyAt,
5757
root_index: U16,
5858
lamports: u64,
5959
address: Option<&[u8]>,
@@ -126,7 +126,7 @@ impl ZInstructionDataInvokeCpiWithReadOnlyMut<'_> {
126126
&mut self,
127127
bump: u8,
128128
invoking_program_id: &Pubkey,
129-
input_proof: Option<<CompressedProof as Deserialize>::Output>,
129+
input_proof: Option<<CompressedProof as ZeroCopyAt>::ZeroCopyAt>,
130130
cpi_context: &Option<impl CompressedCpiContextTrait>,
131131
) -> Result<(), CompressedAccountError> {
132132
self.mode = 1; // Small ix mode

program-libs/compressed-account/src/pubkey.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(feature = "bytemuck-des")]
22
use bytemuck::{Pod, Zeroable};
3-
use light_zero_copy::{errors::ZeroCopyError, traits::ZeroCopyAt};
3+
use light_zero_copy::{errors::ZeroCopyError, traits::{ZeroCopyAt, ZeroCopyAtMut, ZeroCopyStructInner, ZeroCopyStructInnerMut}, ZeroCopyNew};
44
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned};
55

66
use crate::{AnchorDeserialize, AnchorSerialize};
@@ -48,16 +48,15 @@ pub struct Pubkey(pub(crate) [u8; 32]);
4848

4949
impl<'a> ZeroCopyNew<'a> for Pubkey {
5050
type ZeroCopyConfig = ();
51-
type Output = zerocopy::Ref<&'a mut [u8], Pubkey>;
52-
fn byte_len(_config: &Self::ZeroCopyConfig) -> usize {
53-
32
51+
type Output = <Self as ZeroCopyAtMut<'a>>::ZeroCopyAtMut;
52+
fn byte_len(_config: &Self::ZeroCopyConfig) -> Result<usize, ZeroCopyError> {
53+
Ok(32)
5454
}
5555
fn new_zero_copy(
5656
bytes: &'a mut [u8],
5757
_config: Self::ZeroCopyConfig,
5858
) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> {
59-
let (key, rest) = zerocopy::Ref::from_prefix(bytes)?;
60-
Ok((key, rest))
59+
<Self as ZeroCopyAtMut<'a>>::zero_copy_at_mut(bytes)
6160
}
6261
}
6362

@@ -107,13 +106,13 @@ impl<'a> ZeroCopyAt<'a> for Pubkey {
107106
}
108107
}
109108

110-
impl<'a> DeserializeMut<'a> for Pubkey {
111-
type Output = Ref<&'a mut [u8], Pubkey>;
109+
impl<'a> ZeroCopyAtMut<'a> for Pubkey {
110+
type ZeroCopyAtMut = Ref<&'a mut [u8], Pubkey>;
112111

113112
#[inline]
114113
fn zero_copy_at_mut(
115114
bytes: &'a mut [u8],
116-
) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> {
115+
) -> Result<(Self::ZeroCopyAtMut, &'a mut [u8]), ZeroCopyError> {
117116
Ok(Ref::<&mut [u8], Pubkey>::from_prefix(bytes)?)
118117
}
119118
}

program-libs/ctoken-types/src/instructions/create_associated_token_account.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
AnchorDeserialize, AnchorSerialize,
77
};
88

9+
#[repr(C)]
910
#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)]
1011
pub struct CreateAssociatedTokenAccountInstructionData {
1112
/// The owner of the associated token account

program-libs/ctoken-types/src/instructions/create_compressed_mint.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
AnchorDeserialize, AnchorSerialize, CTokenError,
1313
};
1414

15+
#[repr(C)]
1516
#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)]
1617
pub struct CreateCompressedMintInstructionData {
1718
pub decimals: u8,
@@ -29,6 +30,7 @@ pub struct CreateCompressedMintInstructionData {
2930
pub proof: Option<CompressedProof>,
3031
}
3132

33+
#[repr(C)]
3234
#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)]
3335
pub struct CompressedMintWithContext {
3436
pub leaf_index: u32,
@@ -38,6 +40,7 @@ pub struct CompressedMintWithContext {
3840
pub mint: CompressedMintInstructionData,
3941
}
4042

43+
#[repr(C)]
4144
#[derive(Debug, PartialEq, Eq, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)]
4245
pub struct CompressedMintInstructionData {
4346
/// Version for upgradability
@@ -109,6 +112,7 @@ impl TryFrom<CompressedMint> for CompressedMintInstructionData {
109112
})
110113
}
111114
}
115+
#[repr(C)]
112116
#[derive(
113117
Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, ZeroCopy, ZeroCopyMut,
114118
)]

program-libs/ctoken-types/src/instructions/create_spl_mint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
AnchorSerialize,
77
};
88

9+
#[repr(C)]
910
#[derive(ZeroCopy, AnchorDeserialize, AnchorSerialize, Clone, Debug)]
1011
pub struct CreateSplMintInstructionData {
1112
pub mint_bump: u8,

program-libs/ctoken-types/src/instructions/extensions/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ impl ZExtensionInstructionData<'_> {
126126
}
127127

128128
// Manual implementation of zero-copy traits for ExtensionInstructionData
129-
impl<'a> light_zero_copy::borsh::Deserialize<'a> for ExtensionInstructionData {
130-
type Output = ZExtensionInstructionData<'a>;
129+
impl<'a> light_zero_copy::traits::ZeroCopyAt<'a> for ExtensionInstructionData {
130+
type ZeroCopyAt = ZExtensionInstructionData<'a>;
131131

132132
fn zero_copy_at(
133133
data: &'a [u8],
134-
) -> Result<(Self::Output, &'a [u8]), light_zero_copy::errors::ZeroCopyError> {
134+
) -> Result<(Self::ZeroCopyAt, &'a [u8]), light_zero_copy::errors::ZeroCopyError> {
135135
// Read discriminant (first 1 byte for borsh enum)
136136
if data.is_empty() {
137137
return Err(light_zero_copy::errors::ZeroCopyError::ArraySize(

0 commit comments

Comments
 (0)