1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Procedural macros for borsh compatible zero copy serialization.
//!
//!
//!
//! ## Main Macros
//!
//! - `ZeroCopy`: Derives ZeroCopyAt
//! - `ZeroCopyMut`: Derives ZeroCopyAtMut, ZeroCopyNew
//! - `ZeroCopyEq`: Derives PartialEq for <StructName>::ZeroCopy == StructName
//!
//!
//! ## Macro Rules
//! 1. Create zero copy structs Z<StructName> for the struct
//! 1.1. The first consecutive fixed-size fields are extracted into a meta struct Z<StructName>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<u8> uses optimized slice operations, other Vec<T> types use ZeroCopySlice
//! 1.6. Option<u64/u32/u16> are optimized, other Option<T> 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<T> where T is a supported type
//! - Arrays [T; N] where T is a supported type
//! - Option<T> 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**:
//! - `ZeroCopy` supports enums with unit variants or single unnamed field variants
//! - `ZeroCopyMut` does NOT support enums
//! - `ZeroCopyEq` does NOT support enums
//! - `ZeroCopyEq` does NOT support enums, vectors, arrays)
//!
//! ### Special Type Handling
//! - **Arrays in Vec**: `Vec<[T; N]>` is supported. Arrays are Copy types that don't implement
//! the `ZeroCopyStructInner` trait, 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:
//! ```rust, ignore
//! 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>,
//! }
//! ```
//! 2. Implement ZeroCopyAt trait which returns Z<StructName>
//! 3. ZeroCopyMut (separate derive) adds:
//! 3.1. Mutable variants with 'Mut' suffix (Z<StructName>Mut, Z<StructName>MetaMut)
//! 3.2. ZeroCopyAtMut trait implementation
//! 3.3. ZeroCopyNew trait with configuration struct for dynamic field initialization
use TokenStream;
/// ZeroCopy derivation macro for zero-copy deserialization
///
/// # Usage
///
/// Basic usage:
/// ```rust, ignore
/// use light_zero_copy_derive::ZeroCopy;
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// pub struct MyStruct {
/// pub a: u8,
/// }
/// ```
///
/// To derive PartialEq as well, use ZeroCopyEq in addition to ZeroCopy:
/// ```rust, ignore
/// use light_zero_copy_derive::{ZeroCopy, ZeroCopyEq};
/// #[derive(ZeroCopy, ZeroCopyEq)]
/// #[repr(C)]
/// pub struct MyStruct {
/// pub a: u8,
/// }
/// ```
///
/// ZeroCopyEq implementation to add PartialEq for zero-copy structs.
///
/// Use this in addition to ZeroCopy when you want the generated struct to implement PartialEq:
///
/// ```rust, ignore
/// use light_zero_copy_derive::{ZeroCopy, ZeroCopyEq};
/// #[derive(ZeroCopy, ZeroCopyEq)]
/// #[repr(C)]
/// pub struct MyStruct {
/// pub a: u8,
/// }
/// ```
/// Note: Options are not supported in ZeroCopyEq
/// ZeroCopyMut derivation macro for mutable zero-copy deserialization
///
/// This macro generates mutable zero-copy implementations including:
/// - ZeroCopyAtMut trait implementation
/// - Mutable Z-struct with `Mut` suffix (Z<StructName>Mut)
/// - Mutable meta struct if there are fixed-size fields (Z<StructName>MetaMut)
/// - ZeroCopyNew trait implementation with configuration support
/// - Configuration struct for dynamic fields or unit type for fixed-size structs
///
/// # Usage
///
/// ```rust, ignore
/// use light_zero_copy_derive::ZeroCopyMut;
///
/// #[derive(ZeroCopyMut)]
/// #[repr(C)]
/// pub struct MyStruct {
/// pub a: u8,
/// pub vec: Vec<u8>,
/// }
/// ```
///
/// This will generate:
/// - `ZMyStructMut<'a>` type for mutable zero-copy access
/// - `MyStructConfig` struct with `vec: u32` field for Vec length
/// - `ZeroCopyAtMut` trait implementation for deserialization
/// - `ZeroCopyNew` trait implementation for initialization with config
///
/// For fixed-size structs, generates unit config:
/// ```rust, ignore
/// use light_zero_copy_derive::ZeroCopyMut;
/// #[derive(ZeroCopyMut)]
/// #[repr(C)]
/// pub struct FixedStruct {
/// pub a: u8,
/// pub b: u16,
/// }
/// // Generates: pub type FixedStructConfig = ();
/// ```
///
/// For both immutable and mutable functionality, use both derives:
/// ```rust, ignore
/// use light_zero_copy_derive::{ZeroCopy, ZeroCopyMut};
///
/// #[derive(ZeroCopy, ZeroCopyMut)]
/// #[repr(C)]
/// pub struct MyStruct {
/// pub a: u8,
/// }
/// ```