Skip to content

Commit 1eb02d9

Browse files
authored
feat: zero copy derive enum (#1909)
* chore: remove this error for true no-std, add futher input validation * feat: enum support * feat: add zero copy new, enum support * review fixes * refactor: impl feedback * chore: move tests into separate dir * stash * refactor: zero copy derive * refactor: rename Deserialize & deserializeMut -> ZeroCopyAt, ZeroCopyAtMut * fixes and fully qualified import paths * fix: enum lifetime error * test: add further functional tests and fix macro issues * fix: feedback and tests * remove result.txt * remove 1 * test: new_zero_copy
1 parent dc3e5e7 commit 1eb02d9

File tree

158 files changed

+8161
-2423
lines changed

Some content is hidden

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

158 files changed

+8161
-2423
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 & 1 deletion
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
@@ -379,7 +379,7 @@ impl ZCompressedAccount<'_> {
379379
#[cfg(test)]
380380
mod tests {
381381
use light_hasher::Poseidon;
382-
use light_zero_copy::borsh::Deserialize;
382+
use light_zero_copy::traits::ZeroCopyAt;
383383
use num_bigint::BigUint;
384384
use rand::Rng;
385385

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};
1+
use light_zero_copy::{errors::ZeroCopyError, traits::ZeroCopyAt};
22
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned};
33

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

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

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);
@@ -388,9 +388,9 @@ impl<'a> Deref for ZInstructionDataInvokeCpiWithAccountInfo<'a> {
388388
}
389389
}
390390

391-
impl<'a> Deserialize<'a> for InstructionDataInvokeCpiWithAccountInfo {
392-
type Output = ZInstructionDataInvokeCpiWithAccountInfo<'a>;
393-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
391+
impl<'a> ZeroCopyAt<'a> for InstructionDataInvokeCpiWithAccountInfo {
392+
type ZeroCopyAt = ZInstructionDataInvokeCpiWithAccountInfo<'a>;
393+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
394394
let (meta, bytes) =
395395
Ref::<&[u8], ZInstructionDataInvokeCpiWithReadOnlyMeta>::from_prefix(bytes)?;
396396
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::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,
@@ -336,9 +336,9 @@ impl<'a> Deref for ZInstructionDataInvokeCpiWithReadOnly<'a> {
336336
}
337337
}
338338

339-
impl<'a> Deserialize<'a> for InstructionDataInvokeCpiWithReadOnly {
340-
type Output = ZInstructionDataInvokeCpiWithReadOnly<'a>;
341-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
339+
impl<'a> ZeroCopyAt<'a> for InstructionDataInvokeCpiWithReadOnly {
340+
type ZeroCopyAt = ZInstructionDataInvokeCpiWithReadOnly<'a>;
341+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
342342
let (meta, bytes) =
343343
Ref::<&[u8], ZInstructionDataInvokeCpiWithReadOnlyMeta>::from_prefix(bytes)?;
344344
let (proof, bytes) = Option::<Ref<&[u8], CompressedProof>>::zero_copy_at(bytes)?;
@@ -365,10 +365,9 @@ impl<'a> Deserialize<'a> for InstructionDataInvokeCpiWithReadOnly {
365365
(slices, bytes)
366366
};
367367

368-
let (output_compressed_accounts, bytes) =
369-
<Vec<ZOutputCompressedAccountWithPackedContext<'a>> as Deserialize<'a>>::zero_copy_at(
370-
bytes,
371-
)?;
368+
let (output_compressed_accounts, bytes) = <Vec<
369+
ZOutputCompressedAccountWithPackedContext<'a>,
370+
> as ZeroCopyAt<'a>>::zero_copy_at(bytes)?;
372371

373372
let (read_only_addresses, bytes) =
374373
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) =
@@ -487,8 +487,8 @@ impl<'a> InstructionData<'a> for ZInstructionDataInvoke<'a> {
487487
unimplemented!()
488488
}
489489
}
490-
impl<'a> Deserialize<'a> for ZInstructionDataInvoke<'a> {
491-
type Output = Self;
490+
impl<'a> ZeroCopyAt<'a> for ZInstructionDataInvoke<'a> {
491+
type ZeroCopyAt = Self;
492492
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), ZeroCopyError> {
493493
let (proof, bytes) = Option::<CompressedProof>::zero_copy_at(bytes)?;
494494
let (input_compressed_accounts_with_merkle_context, bytes) =
@@ -673,8 +673,8 @@ impl<'a> From<ZInstructionDataInvokeCpi<'a>> for ZInstructionDataInvoke<'a> {
673673
}
674674
}
675675

676-
impl<'a> Deserialize<'a> for ZInstructionDataInvokeCpi<'a> {
677-
type Output = Self;
676+
impl<'a> ZeroCopyAt<'a> for ZInstructionDataInvokeCpi<'a> {
677+
type ZeroCopyAt = Self;
678678
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), ZeroCopyError> {
679679
let (proof, bytes) = Option::<CompressedProof>::zero_copy_at(bytes)?;
680680
let (new_address_params, bytes) = ZeroCopySliceBorsh::from_bytes_at(bytes)?;
@@ -708,8 +708,8 @@ impl<'a> Deserialize<'a> for ZInstructionDataInvokeCpi<'a> {
708708
}
709709
}
710710

711-
impl Deserialize<'_> for CompressedCpiContext {
712-
type Output = Self;
711+
impl ZeroCopyAt<'_> for CompressedCpiContext {
712+
type ZeroCopyAt = Self;
713713
fn zero_copy_at(bytes: &[u8]) -> Result<(Self, &[u8]), ZeroCopyError> {
714714
let (first_set_context, bytes) = u8::zero_copy_at(bytes)?;
715715
let (set_context, bytes) = u8::zero_copy_at(bytes)?;
@@ -736,9 +736,9 @@ pub struct ZPackedReadOnlyCompressedAccount {
736736
pub root_index: U16,
737737
}
738738

739-
impl<'a> Deserialize<'a> for ZPackedReadOnlyCompressedAccount {
740-
type Output = Ref<&'a [u8], Self>;
741-
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> {
739+
impl<'a> ZeroCopyAt<'a> for ZPackedReadOnlyCompressedAccount {
740+
type ZeroCopyAt = Ref<&'a [u8], Self>;
741+
fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::ZeroCopyAt, &'a [u8]), ZeroCopyError> {
742742
Ok(Ref::<&[u8], Self>::from_prefix(bytes)?)
743743
}
744744
}

0 commit comments

Comments
 (0)