Expand description
Procedural macros for borsh compatible zero copy serialization.
§Main Macros
ZeroCopy: Derives ZeroCopyAtZeroCopyMut: Derives ZeroCopyAtMut, ZeroCopyNewZeroCopyEq: Derives PartialEq for::ZeroCopy == StructName
§Macro Rules
- Create zero copy structs Z
for the struct 1.1. The first consecutive fixed-size fields are extracted into a meta struct Z Meta 1.2. Meta extraction stops at first Vec, non-optimized Option (i.e., non-u16/u32/u64 Options), or non-Copy type 1.3. Primitive types are converted to little-endian equivalents (u16→U16, u32→U32, u64→U64, bool→u8) 1.4. Fields after meta are included directly in the Z-struct and deserialized sequentially 1.5. Vec uses optimized slice operations, other Vec types use ZeroCopySlice 1.6. Option<u64/u32/u16> are optimized, other Option delegate to T’s ZeroCopyAt 1.7. Non-Copy types must implement ZeroCopyAt trait
§Supported Types
§Primitives
- Unsigned integers: u8, u16, u32, u64
- Signed integers: i8, i16, i32, i64
- Boolean: bool
§Collections
- Vec
where T is a supported type - Arrays [T; N] where T is a supported type
- Option
where T is a supported type (optimized for u16/u32/u64)
§Custom Types
- Any type that implements ZeroCopyAt trait
- Nested structs with #[derive(ZeroCopy)]
- Enums with unit variants or single unnamed field variants
§Limitations
§Type Support
- usize/isize: Platform-dependent size types are not supported for cross-platform consistency
- f32/f64: Floating point types are not supported
- char: Character type is not supported
§Structural Limitations
- Tuple structs: Not supported - only structs with named fields are allowed
- Empty structs: Not supported - structs must have at least one field for zero-copy serialization
- Enum support:
ZeroCopysupports enums with unit variants or single unnamed field variantsZeroCopyMutdoes NOT support enumsZeroCopyEqdoes NOT support enums
ZeroCopyEqdoes NOT support enums, vectors, arrays)
§Special Type Handling
- Arrays in Vec:
Vec<[T; N]>is supported. Arrays are Copy types that don’t implement theZeroCopyStructInnertrait, so they are handled directly after type conversion (e.g.,[u32; N]→[U32; N]) rather than through the trait’s associated type. - Primitive type conversion: Integer types are automatically converted to their
aligned equivalents for zero-copy safety (e.g.,
u32→U32,i64→I64)
§Requirements
- All structs and enums must have
#[repr(C)]attribute for memory layout safety - Fields must implement appropriate traits (Copy for meta fields, ZeroCopyAt for others)
Examples:
ⓘ
use light_zero_copy::slice::ZeroCopySliceBorsh; use light_zero_copy::slice_mut::ZeroCopySliceMutBorsh; pub struct Struct1 { a: Vec<u8>, } pub struct ZStruct1<'a> { a: &'a [u8] } pub struct ZStruct1Mut<'a> { a: &'a mut [u8] } pub struct Struct2 { a: Vec<u64>, } pub struct ZStruct2<'a> { a: ZeroCopySliceBorsh<'a, u64>, } pub struct ZStruct2Mut<'a> { a: ZeroCopySliceMutBorsh<'a, u64>, }
- Implement ZeroCopyAt trait which returns Z
- ZeroCopyMut (separate derive) adds:
3.1. Mutable variants with ‘Mut’ suffix (Z
Mut, Z MetaMut) 3.2. ZeroCopyAtMut trait implementation 3.3. ZeroCopyNew trait with configuration struct for dynamic field initialization
Derive Macros§
- Zero
Copy - ZeroCopy derivation macro for zero-copy deserialization
- Zero
Copy Eq - ZeroCopyEq implementation to add PartialEq for zero-copy structs.