Skip to content

Commit e639599

Browse files
committed
feat: add zero copy new, enum support
chore: move tests into separate dir refactor: rename Deserialize & deserializeMut -> ZeroCopyAt, ZeroCopyAtMut refacator: use fully qualified import paths test: add further functional tests and fix macro issues test: new_zero_copy
1 parent 507cce4 commit e639599

File tree

156 files changed

+7896
-2222
lines changed

Some content is hidden

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

156 files changed

+7896
-2222
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ jobs:
5454
cargo test -p light-verifier --all-features
5555
cargo test -p light-merkle-tree-metadata --all-features
5656
cargo test -p light-zero-copy --features "std, mut, derive"
57-
cargo test -p light-zero-copy-derive --features "mut"
57+
cargo test -p light-zero-copy --no-default-features # Test no_std compatibility
58+
cargo build -p light-zero-copy --no-default-features # Ensure no_std builds
59+
cargo test -p light-zero-copy-derive --all-features
5860
cargo test -p light-hash-set --all-features
5961
- name: program-libs-slow
6062
packages: light-bloom-filter light-indexed-merkle-tree light-batched-merkle-tree

Cargo.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

anchor-programs/system/src/cpi_context_account.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anchor_lang::prelude::*;
55
use light_compressed_account::instruction_data::{
66
invoke_cpi::InstructionDataInvokeCpi, zero_copy::ZInstructionDataInvokeCpi,
77
};
8-
use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError};
8+
use light_zero_copy::{errors::ZeroCopyError, traits::ZeroCopyAt};
99
use zerocopy::{little_endian::U32, Ref};
1010

1111
/// Collects instruction data without executing a compressed transaction.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl ZCompressedAccount<'_> {
389389
#[cfg(test)]
390390
mod tests {
391391
use light_hasher::Poseidon;
392-
use light_zero_copy::borsh::Deserialize;
392+
use light_zero_copy::traits::ZeroCopyAt;
393393
use num_bigint::BigUint;
394394
use rand::Rng;
395395

program-libs/compressed-account/src/indexer_event/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use borsh::BorshDeserialize;
2-
use light_zero_copy::borsh::Deserialize;
2+
use light_zero_copy::traits::ZeroCopyAt;
33

44
use super::{
55
error::ParseIndexerEventError,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError, ZeroCopyMut};
1+
use light_zero_copy::{errors::ZeroCopyError, traits::ZeroCopyAt,ZeroCopyMut};
22
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned};
33

44
use crate::{AnchorDeserialize, AnchorSerialize};
@@ -35,9 +35,9 @@ impl Default for CompressedProof {
3535
}
3636
}
3737

38-
impl<'a> Deserialize<'a> for CompressedProof {
39-
type Output = Ref<&'a [u8], Self>;
40-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
38+
impl<'a> ZeroCopyAt<'a> for CompressedProof {
39+
type ZeroCopyAt = Ref<&'a [u8], Self>;
40+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
4141
Ok(Ref::<&[u8], CompressedProof>::from_prefix(bytes)?)
4242
}
4343
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use light_zero_copy::{
7-
borsh::Deserialize, errors::ZeroCopyError, slice::ZeroCopySlice, slice_mut::ZeroCopySliceMut,
7+
errors::ZeroCopyError, slice::ZeroCopySlice, slice_mut::ZeroCopySliceMut, traits::ZeroCopyAt,
88
};
99
use zerocopy::{
1010
little_endian::{U32, U64},
@@ -87,8 +87,8 @@ impl Deref for InsertIntoQueuesInstructionData<'_> {
8787
}
8888
}
8989

90-
impl<'a> Deserialize<'a> for InsertIntoQueuesInstructionData<'a> {
91-
type Output = Self;
90+
impl<'a> ZeroCopyAt<'a> for InsertIntoQueuesInstructionData<'a> {
91+
type ZeroCopyAt = Self;
9292
fn zero_copy_at(bytes: &'a [u8]) -> std::result::Result<(Self, &'a [u8]), ZeroCopyError> {
9393
let (meta, bytes) = Ref::<&[u8], InsertIntoQueuesInstructionDataMeta>::from_prefix(bytes)?;
9494

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ops::{Deref, DerefMut};
22

3-
use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError, slice::ZeroCopySliceBorsh};
3+
use light_zero_copy::{errors::ZeroCopyError, slice::ZeroCopySliceBorsh, traits::ZeroCopyAt};
44
use zerocopy::{
55
little_endian::{U16, U32, U64},
66
FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned,
@@ -198,10 +198,10 @@ pub struct ZOutAccountInfo<'a> {
198198
pub data: &'a [u8],
199199
}
200200

201-
impl<'a> Deserialize<'a> for ZOutAccountInfo<'a> {
202-
type Output = ZOutAccountInfo<'a>;
201+
impl<'a> ZeroCopyAt<'a> for ZOutAccountInfo<'a> {
202+
type ZeroCopyAt = ZOutAccountInfo<'a>;
203203

204-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
204+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
205205
let (meta, bytes) = Ref::<&[u8], ZOutAccountInfoMeta>::from_prefix(bytes)?;
206206
let (len, bytes) = Ref::<&'a [u8], U32>::from_prefix(bytes)?;
207207
let (data, bytes) = bytes.split_at(u64::from(*len) as usize);
@@ -394,9 +394,9 @@ impl<'a> Deref for ZInstructionDataInvokeCpiWithAccountInfo<'a> {
394394
}
395395
}
396396

397-
impl<'a> Deserialize<'a> for InstructionDataInvokeCpiWithAccountInfo {
398-
type Output = ZInstructionDataInvokeCpiWithAccountInfo<'a>;
399-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
397+
impl<'a> ZeroCopyAt<'a> for InstructionDataInvokeCpiWithAccountInfo {
398+
type ZeroCopyAt = ZInstructionDataInvokeCpiWithAccountInfo<'a>;
399+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
400400
let (meta, bytes) =
401401
Ref::<&[u8], ZInstructionDataInvokeCpiWithReadOnlyMeta>::from_prefix(bytes)?;
402402
let (proof, bytes) = Option::<Ref<&[u8], CompressedProof>>::zero_copy_at(bytes)?;

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

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

3-
use light_zero_copy::{
4-
borsh::Deserialize, errors::ZeroCopyError, slice::ZeroCopySliceBorsh, ZeroCopyMut,
5-
};
3+
use light_zero_copy::{errors::ZeroCopyError, slice::ZeroCopySliceBorsh, traits::ZeroCopyAt, ZeroCopyMut};
64
use zerocopy::{
75
little_endian::{U16, U32, U64},
86
FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned,
@@ -346,9 +344,9 @@ impl<'a> Deref for ZInstructionDataInvokeCpiWithReadOnly<'a> {
346344
}
347345
}
348346

349-
impl<'a> Deserialize<'a> for InstructionDataInvokeCpiWithReadOnly {
350-
type Output = ZInstructionDataInvokeCpiWithReadOnly<'a>;
351-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
347+
impl<'a> ZeroCopyAt<'a> for InstructionDataInvokeCpiWithReadOnly {
348+
type ZeroCopyAt = ZInstructionDataInvokeCpiWithReadOnly<'a>;
349+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
352350
let (meta, bytes) =
353351
Ref::<&[u8], ZInstructionDataInvokeCpiWithReadOnlyMeta>::from_prefix(bytes)?;
354352
let (proof, bytes) = Option::<Ref<&[u8], CompressedProof>>::zero_copy_at(bytes)?;
@@ -375,10 +373,9 @@ impl<'a> Deserialize<'a> for InstructionDataInvokeCpiWithReadOnly {
375373
(slices, bytes)
376374
};
377375

378-
let (output_compressed_accounts, bytes) =
379-
<Vec<ZOutputCompressedAccountWithPackedContext<'a>> as Deserialize<'a>>::zero_copy_at(
380-
bytes,
381-
)?;
376+
let (output_compressed_accounts, bytes) = <Vec<
377+
ZOutputCompressedAccountWithPackedContext<'a>,
378+
> as ZeroCopyAt<'a>>::zero_copy_at(bytes)?;
382379

383380
let (read_only_addresses, bytes) =
384381
ZeroCopySliceBorsh::<'a, ZPackedReadOnlyAddress>::from_bytes_at(bytes)?;

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{mem::size_of, ops::Deref};
22

3-
use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError, slice::ZeroCopySliceBorsh};
3+
use light_zero_copy::{errors::ZeroCopyError, slice::ZeroCopySliceBorsh, traits::ZeroCopyAt};
44
use zerocopy::{
55
little_endian::{U16, U32, U64},
66
FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned,
@@ -32,8 +32,8 @@ pub struct ZPackedReadOnlyAddress {
3232
pub address_merkle_tree_account_index: u8,
3333
}
3434

35-
impl<'a> Deserialize<'a> for ZPackedReadOnlyAddress {
36-
type Output = Self;
35+
impl<'a> ZeroCopyAt<'a> for ZPackedReadOnlyAddress {
36+
type ZeroCopyAt = Self;
3737
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), ZeroCopyError> {
3838
let (address, bytes) = bytes.split_at(size_of::<[u8; 32]>());
3939
let (address_merkle_tree_root_index, bytes) = U16::ref_from_prefix(bytes)?;
@@ -99,9 +99,9 @@ impl ZPackedMerkleContext {
9999
}
100100
}
101101

102-
impl<'a> Deserialize<'a> for ZPackedMerkleContext {
103-
type Output = Ref<&'a [u8], Self>;
104-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
102+
impl<'a> ZeroCopyAt<'a> for ZPackedMerkleContext {
103+
type ZeroCopyAt = Ref<&'a [u8], Self>;
104+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
105105
let (ref_value, bytes) = Ref::<&[u8], Self>::from_prefix(bytes)?;
106106
Ok((ref_value, bytes))
107107
}
@@ -171,8 +171,8 @@ impl<'a> From<&ZOutputCompressedAccountWithPackedContext<'a>>
171171
}
172172
}
173173

174-
impl<'a> Deserialize<'a> for ZOutputCompressedAccountWithPackedContext<'a> {
175-
type Output = Self;
174+
impl<'a> ZeroCopyAt<'a> for ZOutputCompressedAccountWithPackedContext<'a> {
175+
type ZeroCopyAt = Self;
176176

177177
#[inline]
178178
fn zero_copy_at(vec: &'a [u8]) -> Result<(Self, &'a [u8]), ZeroCopyError> {
@@ -215,8 +215,8 @@ impl From<&ZCompressedAccountData<'_>> for CompressedAccountData {
215215
}
216216
}
217217

218-
impl<'a> Deserialize<'a> for ZCompressedAccountData<'a> {
219-
type Output = Self;
218+
impl<'a> ZeroCopyAt<'a> for ZCompressedAccountData<'a> {
219+
type ZeroCopyAt = Self;
220220

221221
#[inline]
222222
fn zero_copy_at(
@@ -281,8 +281,8 @@ impl From<&ZCompressedAccount<'_>> for CompressedAccount {
281281
}
282282
}
283283

284-
impl<'a> Deserialize<'a> for ZCompressedAccount<'a> {
285-
type Output = Self;
284+
impl<'a> ZeroCopyAt<'a> for ZCompressedAccount<'a> {
285+
type ZeroCopyAt = Self;
286286

287287
#[inline]
288288
fn zero_copy_at(bytes: &'a [u8]) -> Result<(ZCompressedAccount<'a>, &'a [u8]), ZeroCopyError> {
@@ -399,8 +399,8 @@ impl Deref for ZPackedCompressedAccountWithMerkleContext<'_> {
399399
}
400400
}
401401

402-
impl<'a> Deserialize<'a> for ZPackedCompressedAccountWithMerkleContext<'a> {
403-
type Output = Self;
402+
impl<'a> ZeroCopyAt<'a> for ZPackedCompressedAccountWithMerkleContext<'a> {
403+
type ZeroCopyAt = Self;
404404
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), ZeroCopyError> {
405405
let (compressed_account, bytes) = ZCompressedAccount::zero_copy_at(bytes)?;
406406
let (meta, bytes) =
@@ -491,8 +491,8 @@ impl<'a> InstructionData<'a> for ZInstructionDataInvoke<'a> {
491491
unimplemented!()
492492
}
493493
}
494-
impl<'a> Deserialize<'a> for ZInstructionDataInvoke<'a> {
495-
type Output = Self;
494+
impl<'a> ZeroCopyAt<'a> for ZInstructionDataInvoke<'a> {
495+
type ZeroCopyAt = Self;
496496
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), ZeroCopyError> {
497497
let (proof, bytes) = Option::<CompressedProof>::zero_copy_at(bytes)?;
498498
let (input_compressed_accounts_with_merkle_context, bytes) =
@@ -682,8 +682,8 @@ impl<'a> From<ZInstructionDataInvokeCpi<'a>> for ZInstructionDataInvoke<'a> {
682682
}
683683
}
684684

685-
impl<'a> Deserialize<'a> for ZInstructionDataInvokeCpi<'a> {
686-
type Output = Self;
685+
impl<'a> ZeroCopyAt<'a> for ZInstructionDataInvokeCpi<'a> {
686+
type ZeroCopyAt = Self;
687687
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), ZeroCopyError> {
688688
let (proof, bytes) = Option::<CompressedProof>::zero_copy_at(bytes)?;
689689
let (new_address_params, bytes) = ZeroCopySliceBorsh::from_bytes_at(bytes)?;
@@ -717,8 +717,8 @@ impl<'a> Deserialize<'a> for ZInstructionDataInvokeCpi<'a> {
717717
}
718718
}
719719

720-
impl Deserialize<'_> for CompressedCpiContext {
721-
type Output = Self;
720+
impl ZeroCopyAt<'_> for CompressedCpiContext {
721+
type ZeroCopyAt = Self;
722722
fn zero_copy_at(bytes: &[u8]) -> Result<(Self, &[u8]), ZeroCopyError> {
723723
let (set_context, bytes) = u8::zero_copy_at(bytes)?;
724724
let (first_set_context, bytes) = u8::zero_copy_at(bytes)?;
@@ -745,9 +745,9 @@ pub struct ZPackedReadOnlyCompressedAccount {
745745
pub root_index: U16,
746746
}
747747

748-
impl<'a> Deserialize<'a> for ZPackedReadOnlyCompressedAccount {
749-
type Output = Ref<&'a [u8], Self>;
750-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
748+
impl<'a> ZeroCopyAt<'a> for ZPackedReadOnlyCompressedAccount {
749+
type ZeroCopyAt = Ref<&'a [u8], Self>;
750+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
751751
Ok(Ref::<&[u8], Self>::from_prefix(bytes)?)
752752
}
753753
}

0 commit comments

Comments
 (0)