light-zero-copy-derive 0.7.0

Proc macro for zero-copy deserialization
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
};

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{
    Attribute, Data, DataEnum, DeriveInput, Field, Fields, FieldsNamed, Ident, Type, TypePath,
};

// Global cache for storing whether a struct implements Copy
lazy_static::lazy_static! {
    pub(crate) static ref COPY_IMPL_CACHE: Arc<Mutex<HashMap<String, bool>>> = Arc::new(Mutex::new(HashMap::new()));
}

/// Creates a unique cache key for a type using span information to avoid collisions
/// between types with the same name from different modules/locations
fn create_unique_type_key(ident: &Ident) -> String {
    format!("{}:{:?}", ident, ident.span())
}

/// Represents the type of input data (struct or enum)
pub enum InputType<'a> {
    Struct(&'a FieldsNamed),
    UnitStruct, // Unit struct with no fields (e.g., `struct Foo;`)
    Enum(&'a DataEnum),
}

/// Process the derive input to extract the struct information
pub fn process_input(
    input: &DeriveInput,
) -> syn::Result<(
    &Ident,               // Original struct name
    proc_macro2::Ident,   // Z-struct name
    proc_macro2::Ident,   // Z-struct meta name
    Option<&FieldsNamed>, // Struct fields (None for unit structs)
)> {
    let name = &input.ident;
    let z_struct_name = format_ident!("Z{}", name);
    let z_struct_meta_name = format_ident!("Z{}Meta", name);

    // Populate the cache by checking if this struct implements Copy
    let _ = struct_implements_copy(input);

    let fields = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => Some(fields),
            Fields::Unit => None, // Support unit structs (e.g., `struct Foo;`)
            _ => {
                return Err(syn::Error::new_spanned(
                    &data.fields,
                    "ZeroCopy only supports structs with named fields or unit structs",
                ))
            }
        },
        _ => {
            return Err(syn::Error::new_spanned(
                input,
                "ZeroCopy only supports structs",
            ))
        }
    };

    Ok((name, z_struct_name, z_struct_meta_name, fields))
}

/// Process the derive input to extract information for both structs and enums
pub fn process_input_generic(
    input: &DeriveInput,
) -> syn::Result<(
    &Ident,             // Original name
    proc_macro2::Ident, // Z-name
    InputType<'_>,      // Input type (struct or enum)
)> {
    let name = &input.ident;
    let z_name = format_ident!("Z{}", name);

    // Populate the cache by checking if this struct implements Copy
    let _ = struct_implements_copy(input);

    let input_type = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => InputType::Struct(fields),
            Fields::Unit => InputType::UnitStruct, // Support unit structs
            _ => {
                return Err(syn::Error::new_spanned(
                    &data.fields,
                    "ZeroCopy only supports structs with named fields or unit structs",
                ))
            }
        },
        Data::Enum(data) => InputType::Enum(data),
        _ => {
            return Err(syn::Error::new_spanned(
                input,
                "ZeroCopy only supports structs and enums",
            ))
        }
    };

    Ok((name, z_name, input_type))
}

pub fn process_fields(fields: &FieldsNamed) -> (Vec<&Field>, Vec<&Field>) {
    let mut meta_fields = Vec::new();
    let mut struct_fields = Vec::new();
    let mut reached_vec_or_option = false;

    for field in fields.named.iter() {
        if !reached_vec_or_option {
            if is_vec_or_option(&field.ty) || !is_copy_type(&field.ty) {
                reached_vec_or_option = true;
                struct_fields.push(field);
            } else {
                meta_fields.push(field);
            }
        } else {
            struct_fields.push(field);
        }
    }

    (meta_fields, struct_fields)
}

pub fn is_vec_or_option(ty: &Type) -> bool {
    is_vec_type(ty) || is_option_type(ty)
}

pub fn is_vec_type(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            return segment.ident == "Vec";
        }
    }
    false
}

pub fn is_option_type(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            return segment.ident == "Option";
        }
    }
    false
}

pub fn get_vec_inner_type(ty: &Type) -> Option<&Type> {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            if segment.ident == "Vec" {
                if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                    if let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first() {
                        return Some(inner_ty);
                    }
                }
            }
        }
    }
    None
}

pub fn get_option_inner_type(ty: &Type) -> Option<&Type> {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            if segment.ident == "Option" {
                if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                    if let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first() {
                        return Some(inner_ty);
                    }
                }
            }
        }
    }
    None
}

pub fn is_primitive_integer(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            let ident = &segment.ident;
            return ident == "u16"
                || ident == "u32"
                || ident == "u64"
                || ident == "i16"
                || ident == "i32"
                || ident == "i64"
                || ident == "u8"
                || ident == "i8";
        }
    }
    false
}

pub fn is_bool_type(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            return segment.ident == "bool";
        }
    }
    false
}

/// Check if a type is a specific primitive type (u8, u16, u32, u64, bool, etc.)
pub fn is_specific_primitive_type(ty: &Type, type_name: &str) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            return segment.ident == type_name;
        }
    }
    false
}

pub fn is_pubkey_type(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            return segment.ident == "Pubkey";
        }
    }
    false
}

pub fn convert_to_zerocopy_type(ty: &Type) -> TokenStream {
    match ty {
        Type::Path(TypePath { path, .. }) => {
            if let Some(segment) = path.segments.last() {
                let ident = &segment.ident;

                // Handle primitive types first
                match ident.to_string().as_str() {
                    "u16" => quote! { ::light_zero_copy::little_endian::U16 },
                    "u32" => quote! { ::light_zero_copy::little_endian::U32 },
                    "u64" => quote! { ::light_zero_copy::little_endian::U64 },
                    "i16" => quote! { ::light_zero_copy::little_endian::I16 },
                    "i32" => quote! { ::light_zero_copy::little_endian::I32 },
                    "i64" => quote! { ::light_zero_copy::little_endian::I64 },
                    "bool" => quote! { u8 },
                    _ => {
                        // Handle container types recursively
                        if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                            let transformed_args: Vec<TokenStream> = args
                                .args
                                .iter()
                                .map(|arg| {
                                    if let syn::GenericArgument::Type(inner_type) = arg {
                                        convert_to_zerocopy_type(inner_type)
                                    } else {
                                        quote! { #arg }
                                    }
                                })
                                .collect();

                            quote! { #ident<#(#transformed_args),*> }
                        } else {
                            quote! { #ty }
                        }
                    }
                }
            } else {
                quote! { #ty }
            }
        }
        Type::Array(array) => {
            // Recursively convert the element type
            let elem = convert_to_zerocopy_type(&array.elem);
            let len = &array.len;
            quote! { [#elem; #len] }
        }
        _ => {
            quote! { #ty }
        }
    }
}

/// Checks if a struct has a derive(Copy) attribute
fn struct_has_copy_derive(attrs: &[Attribute]) -> bool {
    attrs.iter().any(|attr| {
        attr.path().is_ident("derive") && {
            let mut found_copy = false;
            // Use parse_nested_meta as the primary and only approach - it's the syn 2.0 standard
            // for parsing comma-separated derive items like #[derive(Copy, Clone, Debug)]
            attr.parse_nested_meta(|meta| {
                if meta.path.is_ident("Copy") {
                    found_copy = true;
                }
                Ok(()) // Continue parsing other derive items
            })
            .is_ok()
                && found_copy
        }
    })
}

/// Checks if a struct has a #[light_hasher] attribute
pub fn struct_has_light_hasher_attribute(attrs: &[Attribute]) -> bool {
    attrs
        .iter()
        .any(|attr| attr.path().is_ident("light_hasher"))
}

/// Determines whether a struct implements Copy by checking for the #[derive(Copy)] attribute.
/// Results are cached for performance.
///
/// In Rust, a struct can only implement Copy if:
/// 1. It explicitly has a #[derive(Copy)] attribute, AND
/// 2. All of its fields implement Copy
///
/// The Rust compiler will enforce the second condition at compile time, so we only need to check
/// for the derive attribute here.
pub fn struct_implements_copy(input: &DeriveInput) -> bool {
    let cache_key = create_unique_type_key(&input.ident);

    // Check the cache first
    if let Ok(cache) = COPY_IMPL_CACHE.lock() {
        if let Some(implements_copy) = cache.get(&cache_key) {
            return *implements_copy;
        }
    }
    // If mutex is poisoned, we can still continue without cache

    // Check if the struct has a derive(Copy) attribute
    let implements_copy = struct_has_copy_derive(&input.attrs);

    // Cache the result (ignore if mutex is poisoned)
    if let Ok(mut cache) = COPY_IMPL_CACHE.lock() {
        cache.insert(cache_key, implements_copy);
    }

    implements_copy
}

/// Determines whether a type implements Copy
/// 1. check whether type is a primitive type that implements Copy
/// 2. check whether type is an array type (which is always Copy if the element type is Copy)
/// 3. check whether type is struct -> check in the COPY_IMPL_CACHE if we know whether it has a #[derive(Copy)] attribute
///
/// For struct types, this relies on the cache populated by struct_implements_copy. If we don't have cached
/// information, it assumes the type does not implement Copy. This is a limitation of our approach, but it
/// works well in practice because process_input will call struct_implements_copy for all structs before
/// they might be referenced by other structs.
pub fn is_copy_type(ty: &Type) -> bool {
    match ty {
        Type::Path(TypePath { path, .. }) => {
            if let Some(segment) = path.segments.last() {
                let ident = &segment.ident;

                // Check if it's a primitive type that implements Copy
                if ident == "u8"
                    || ident == "u16"
                    || ident == "u32"
                    || ident == "u64"
                    || ident == "i8"
                    || ident == "i16"
                    || ident == "i32"
                    || ident == "i64"
                    || ident == "bool" // bool is a Copy type
                    || ident == "char"
                    || ident == "Pubkey"
                // Pubkey is hardcoded as copy type for now.
                {
                    return true;
                }

                // Check if we have cached information about this type
                let cache_key = create_unique_type_key(ident);
                if let Ok(cache) = COPY_IMPL_CACHE.lock() {
                    if let Some(implements_copy) = cache.get(&cache_key) {
                        return *implements_copy;
                    }
                }
                // If mutex is poisoned, continue without cache
            }
        }
        // Handle array types (which are always Copy if the element type is Copy)
        Type::Array(array) => {
            // Arrays are Copy if their element type is Copy
            return is_copy_type(&array.elem);
        }
        // For struct types not in cache, we'd need the derive input to check attributes
        _ => {}
    }
    false
}

/// Check if a type needs to use the ZeroCopyStructInner trait.
/// Arrays and primitive types can be used directly after type conversion,
/// while custom structs need to go through the trait's associated type.
pub fn needs_struct_inner_trait(ty: &Type) -> bool {
    // Arrays don't implement ZeroCopyStructInner - use directly
    if matches!(ty, Type::Array(_)) {
        return false;
    }

    // Primitive types and bool are used directly after conversion
    if is_primitive_integer(ty) || is_bool_type(ty) || is_pubkey_type(ty) {
        return false;
    }

    // All other types (custom structs) need the trait
    true
}

/// Check if a struct has #[repr(C)] attribute
pub fn has_repr_c_attribute(attrs: &[syn::Attribute]) -> bool {
    attrs.iter().any(|attr| {
        if attr.path().is_ident("repr") {
            // Parse the repr attribute arguments
            // Convert tokens to string and check if it contains "C"
            // This handles both #[repr(C)] and #[repr(C, packed)] etc.
            let tokens = attr.meta.clone();
            if let syn::Meta::List(list) = tokens {
                // Convert tokens to string and check for "C"
                let tokens_str = list.tokens.to_string();
                // Split by comma and check each part
                for part in tokens_str.split(',') {
                    let trimmed = part.trim();
                    // Check if this part is exactly "C" (not part of another word)
                    if trimmed == "C" {
                        return true;
                    }
                }
            } else if let syn::Meta::Path(path) = tokens {
                // Handle #[repr(C)] without parentheses (though unlikely)
                return path.is_ident("C");
            }
            false
        } else {
            false
        }
    })
}

/// Validate that the input has #[repr(C)] attribute for memory layout safety
pub fn validate_repr_c_required(attrs: &[syn::Attribute], item_type: &str) -> syn::Result<()> {
    if !has_repr_c_attribute(attrs) {
        return Err(syn::Error::new_spanned(
            attrs.first().unwrap_or(&syn::parse_quote!(#[dummy])),
            format!(
                "{} requires #[repr(C)] attribute for memory layout safety. Add #[repr(C)] above the {} declaration.",
                item_type, item_type.to_lowercase()
            )
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use quote::quote;

    use super::has_repr_c_attribute;

    #[test]
    fn test_repr_c_detection() {
        // Test single #[repr(C)]
        let input = quote! {
            #[repr(C)]
            struct Test {}
        };
        let parsed: syn::DeriveInput = syn::parse2(input).unwrap();
        assert!(
            has_repr_c_attribute(&parsed.attrs),
            "Should detect #[repr(C)]"
        );

        // Test #[repr(C, packed)]
        let input = quote! {
            #[repr(C, packed)]
            struct Test {}
        };
        let parsed: syn::DeriveInput = syn::parse2(input).unwrap();
        assert!(
            has_repr_c_attribute(&parsed.attrs),
            "Should detect C in #[repr(C, packed)]"
        );

        // Test #[repr(C, align(8))]
        let input = quote! {
            #[repr(C, align(8))]
            struct Test {}
        };
        let parsed: syn::DeriveInput = syn::parse2(input).unwrap();
        assert!(
            has_repr_c_attribute(&parsed.attrs),
            "Should detect C in #[repr(C, align(8))]"
        );

        // Test #[repr(packed, C)]
        let input = quote! {
            #[repr(packed, C)]
            struct Test {}
        };
        let parsed: syn::DeriveInput = syn::parse2(input).unwrap();
        assert!(
            has_repr_c_attribute(&parsed.attrs),
            "Should detect C in #[repr(packed, C)]"
        );

        // Test #[repr(packed)] without C
        let input = quote! {
            #[repr(packed)]
            struct Test {}
        };
        let parsed: syn::DeriveInput = syn::parse2(input).unwrap();
        assert!(
            !has_repr_c_attribute(&parsed.attrs),
            "Should not detect C in #[repr(packed)]"
        );

        // Test no repr attribute
        let input = quote! {
            struct Test {}
        };
        let parsed: syn::DeriveInput = syn::parse2(input).unwrap();
        assert!(
            !has_repr_c_attribute(&parsed.attrs),
            "Should not detect C without repr"
        );

        // Test #[repr(Rust)]
        let input = quote! {
            #[repr(Rust)]
            struct Test {}
        };
        let parsed: syn::DeriveInput = syn::parse2(input).unwrap();
        assert!(
            !has_repr_c_attribute(&parsed.attrs),
            "Should not detect C in #[repr(Rust)]"
        );
    }
}