vercode-macros 0.5.0

Low overhead versionable serialization of Rust data structures
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// Copyright (c) Microsoft Corporation. All rights reserved.
use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Fields, Meta, parse_macro_input};

/// Helper struct for field information during code generation
#[derive(Clone)]
struct FieldInfo<'a> {
    index: usize,
    version: u32,
    ty: &'a syn::Type,
    ident: Option<&'a syn::Ident>,
}

impl<'a> FieldInfo<'a> {
    fn temp_var(&self) -> syn::Ident {
        syn::Ident::new(
            &format!("__field{}", self.index),
            proc_macro2::Span::call_site(),
        )
    }
}

/// Batches of fields grouped by version number
struct VersionBatch<'a> {
    version: u32,
    fields: Vec<FieldInfo<'a>>,
}

fn parse_version_attribute(attrs: &[syn::Attribute]) -> u32 {
    for attr in attrs {
        if let Meta::List(list) = &attr.meta
            && list.path.is_ident("version")
        {
            let ts = list.tokens.to_string();
            let digits: String = ts.chars().filter(|c| c.is_ascii_digit()).collect();
            if let Ok(v) = digits.parse::<u32>() {
                return v;
            }
        }
    }
    0
}

fn field_version(field: &syn::Field) -> u32 {
    parse_version_attribute(&field.attrs)
}

fn variant_version(variant: &syn::Variant) -> u32 {
    parse_version_attribute(&variant.attrs)
}

/// Extract and organize field information from Fields
fn extract_field_info(fields: &Fields) -> Vec<FieldInfo<'_>> {
    match fields {
        Fields::Named(named) => named
            .named
            .iter()
            .enumerate()
            .map(|(i, f)| FieldInfo {
                index: i,
                version: field_version(f),
                ty: &f.ty,
                ident: f.ident.as_ref(),
            })
            .collect(),
        Fields::Unnamed(unnamed) => unnamed
            .unnamed
            .iter()
            .enumerate()
            .map(|(i, f)| FieldInfo {
                index: i,
                version: field_version(f),
                ty: &f.ty,
                ident: None,
            })
            .collect(),
        Fields::Unit => vec![],
    }
}

/// Sort fields by version, then by original index, and batch by version
fn create_version_batches(mut field_infos: Vec<FieldInfo>) -> Vec<VersionBatch> {
    // Sort by version first, then by original index
    field_infos.sort_by_key(|f| (f.version, f.index));

    // Group into batches by version
    let mut batches: Vec<VersionBatch> = Vec::new();
    for field in field_infos {
        if let Some(last_batch) = batches.last_mut()
            && last_batch.version == field.version
        {
            last_batch.fields.push(field);
            continue;
        }
        batches.push(VersionBatch {
            version: field.version,
            fields: vec![field],
        });
    }
    batches
}

/// Generate write code for a batch of fields
fn generate_field_writes(
    batches: &[VersionBatch],
    is_named: bool,
) -> Vec<proc_macro2::TokenStream> {
    let mut writes = Vec::new();
    let mut last_version = 0u32;

    for batch in batches {
        if batch.version != last_version {
            last_version = batch.version;
            let v = batch.version;
            writes.push(quote! { if version < #v { return offset; } });
        }

        for field in &batch.fields {
            let write_stmt = if is_named {
                let ident = field.ident.unwrap();
                quote! { offset += ::vercode::VerCodable::write_version(&self.#ident, version, &mut buf[offset..]); }
            } else {
                let idx = syn::Index::from(field.index);
                quote! { offset += ::vercode::VerCodable::write_version(&self.#idx, version, &mut buf[offset..]); }
            };
            writes.push(write_stmt);
        }
    }
    writes
}

/// Generate size calculation code for a batch of fields
fn generate_field_sizes(batches: &[VersionBatch], is_named: bool) -> Vec<proc_macro2::TokenStream> {
    let mut sizes = Vec::new();
    let mut last_version = 0u32;

    for batch in batches {
        if batch.version != last_version {
            last_version = batch.version;
            let v = batch.version;
            sizes.push(quote! { if version < #v { return total; } });
        }

        for field in &batch.fields {
            let size_stmt = if is_named {
                let ident = field.ident.unwrap();
                quote! { total += ::vercode::VerCodable::size_version(&self.#ident, version); }
            } else {
                let idx = syn::Index::from(field.index);
                quote! { total += ::vercode::VerCodable::size_version(&self.#idx, version); }
            };
            sizes.push(size_stmt);
        }
    }
    sizes
}

/// Generate read code for version batches
fn generate_field_reads(batches: &[VersionBatch]) -> Vec<proc_macro2::TokenStream> {
    let mut reads = Vec::new();

    for batch in batches {
        let temp_vars: Vec<_> = batch.fields.iter().map(|f| f.temp_var()).collect();
        let mut read_stmts = Vec::new();
        let mut default_stmts = Vec::new();

        for field in &batch.fields {
            let temp_var = field.temp_var();
            let ty = field.ty;
            read_stmts.push(quote! {
                (#temp_var, __temp_size) = <#ty as ::vercode::VerCodable>::read_version(version, &buf[offset..])?;
                offset += __temp_size;
            });
            default_stmts.push(quote! {
                #temp_var = <#ty as ::std::default::Default>::default();
            });
        }

        if batch.version == 0 {
            // Version 0 fields always read
            reads.push(quote! {
                #(let mut #temp_vars;)*
                let mut __temp_size;
                #(#read_stmts)*
            });
        } else {
            let v = batch.version;
            reads.push(quote! {
                #(let mut #temp_vars;)*
                let mut __temp_size;
                if offset < length && version >= #v {
                    #(#read_stmts)*
                } else {
                    #(#default_stmts)*
                }
            });
        }
    }
    reads
}

/// Generate struct construction from field info
fn generate_struct_construction(
    name: &syn::Ident,
    fields: &Fields,
    field_infos: &[FieldInfo],
) -> proc_macro2::TokenStream {
    match fields {
        Fields::Named(_) => {
            let field_inits: Vec<_> = field_infos
                .iter()
                .map(|f| {
                    let ident = f.ident.unwrap();
                    let temp_var = f.temp_var();
                    quote! { #ident: #temp_var }
                })
                .collect();
            quote! { #name { #(#field_inits),* } }
        }
        Fields::Unnamed(_) => {
            let field_values: Vec<_> = field_infos.iter().map(|f| f.temp_var()).collect();
            quote! { #name ( #(#field_values),* ) }
        }
        Fields::Unit => quote! { #name },
    }
}

/// Calculate maximum version expression from field infos
fn calculate_max_version_expr(field_infos: &[FieldInfo]) -> proc_macro2::TokenStream {
    // Calculate max from field version attributes
    let field_attr_max = field_infos.iter().map(|f| f.version).max().unwrap_or(0);

    // Generate expressions for each field type's MAX_VERSION
    let field_type_exprs: Vec<_> = field_infos
        .iter()
        .map(|f| {
            let ty = f.ty;
            quote! { <#ty as ::vercode::VerCodable>::MAX_VERSION }
        })
        .collect();

    // Generate a const expression that computes the max of all versions
    if field_type_exprs.is_empty() {
        quote! { #field_attr_max }
    } else {
        quote! {
            {
                let mut max = #field_attr_max;
                #(
                    if #field_type_exprs > max {
                        max = #field_type_exprs;
                    }
                )*
                max
            }
        }
    }
}

/// Variant information for enum processing
struct VariantInfo<'a> {
    index: usize,
    variant: &'a syn::Variant,
    field_infos: Vec<FieldInfo<'a>>,
    batches: Vec<VersionBatch<'a>>,
}

impl<'a> VariantInfo<'a> {
    fn new(index: usize, variant: &'a syn::Variant) -> Self {
        let field_infos = extract_field_info(&variant.fields);
        let batches = create_version_batches(field_infos.clone());
        VariantInfo {
            index,
            variant,
            field_infos,
            batches,
        }
    }

    fn max_version_expr(&self) -> proc_macro2::TokenStream {
        let variant_ver = variant_version(self.variant);

        // Get field attribute versions
        let field_attr_max = self
            .field_infos
            .iter()
            .map(|f| f.version)
            .max()
            .unwrap_or(0);

        // Generate expressions for each field type's MAX_VERSION
        let field_type_exprs: Vec<_> = self
            .field_infos
            .iter()
            .map(|f| {
                let ty = f.ty;
                quote! { <#ty as ::vercode::VerCodable>::MAX_VERSION }
            })
            .collect();

        // Generate a const expression that computes the max of all versions
        if field_type_exprs.is_empty() {
            let max = variant_ver.max(field_attr_max);
            quote! { #max }
        } else {
            quote! {
                {
                    let mut max = #variant_ver;
                    if #field_attr_max > max {
                        max = #field_attr_max;
                    }
                    #(
                        if #field_type_exprs > max {
                            max = #field_type_exprs;
                        }
                    )*
                    max
                }
            }
        }
    }

    /// Generate pattern match binding for this variant
    fn match_pattern(&self, enum_name: &syn::Ident) -> proc_macro2::TokenStream {
        let var_name = &self.variant.ident;
        match &self.variant.fields {
            Fields::Named(_) => {
                let actual_names: Vec<_> =
                    self.field_infos.iter().map(|f| f.ident.unwrap()).collect();
                let temp_vars: Vec<_> = self.field_infos.iter().map(|f| f.temp_var()).collect();
                quote! { #enum_name::#var_name { #(#actual_names: #temp_vars),* } }
            }
            Fields::Unnamed(_) => {
                let temp_vars: Vec<_> = self.field_infos.iter().map(|f| f.temp_var()).collect();
                quote! { #enum_name::#var_name(#(#temp_vars),*) }
            }
            Fields::Unit => quote! { #enum_name::#var_name },
        }
    }

    /// Generate variant construction from temp variables
    fn construct_variant(&self, enum_name: &syn::Ident) -> proc_macro2::TokenStream {
        let var_name = &self.variant.ident;
        match &self.variant.fields {
            Fields::Named(_) => {
                let actual_names: Vec<_> =
                    self.field_infos.iter().map(|f| f.ident.unwrap()).collect();
                let temp_vars: Vec<_> = self.field_infos.iter().map(|f| f.temp_var()).collect();
                quote! { #enum_name::#var_name { #(#actual_names: #temp_vars),* } }
            }
            Fields::Unnamed(_) => {
                let temp_vars: Vec<_> = self.field_infos.iter().map(|f| f.temp_var()).collect();
                quote! { #enum_name::#var_name(#(#temp_vars),*) }
            }
            Fields::Unit => quote! { #enum_name::#var_name },
        }
    }

    /// Generate write arm for this variant
    fn write_arm(&self, enum_name: &syn::Ident) -> proc_macro2::TokenStream {
        let idx_u32 = self.index as u32;
        let pattern = self.match_pattern(enum_name);
        let field_writes = generate_variant_field_writes(&self.batches);

        quote! {
            #pattern => {
                buf[offset..offset+2].copy_from_slice(&(#idx_u32 as u16).to_le_bytes());
                offset += 2;
                #(#field_writes)*
            }
        }
    }

    /// Generate size arm for this variant
    fn size_arm(&self, enum_name: &syn::Ident) -> proc_macro2::TokenStream {
        let pattern = self.match_pattern(enum_name);
        let field_sizes = generate_variant_field_sizes(&self.batches);

        quote! {
            #pattern => {
                #(#field_sizes)*
            }
        }
    }

    /// Generate read arm for this variant
    fn read_arm(&self, enum_name: &syn::Ident) -> proc_macro2::TokenStream {
        let idx_u32 = self.index as u32;
        let reads = generate_field_reads(&self.batches);
        let construction = self.construct_variant(enum_name);

        quote! {
            #idx_u32 => {
                #(#reads)*
                #construction
            }
        }
    }
}

/// Generate write statements for variant fields (using temp vars)
fn generate_variant_field_writes(batches: &[VersionBatch]) -> Vec<proc_macro2::TokenStream> {
    let mut writes = Vec::new();
    let mut last_version = 0u32;

    for batch in batches {
        if batch.version != last_version {
            last_version = batch.version;
            let v = batch.version;
            writes.push(quote! { if version < #v { return offset; } });
        }

        for field in &batch.fields {
            let temp_var = field.temp_var();
            writes.push(quote! {
                offset += ::vercode::VerCodable::write_version(#temp_var, version, &mut buf[offset..]);
            });
        }
    }
    writes
}

/// Generate size statements for variant fields (using temp vars)
fn generate_variant_field_sizes(batches: &[VersionBatch]) -> Vec<proc_macro2::TokenStream> {
    let mut sizes = Vec::new();
    let mut last_version = 0u32;

    for batch in batches {
        if batch.version != last_version {
            last_version = batch.version;
            let v = batch.version;
            sizes.push(quote! { if version < #v { return total; } });
        }

        for field in &batch.fields {
            let temp_var = field.temp_var();
            sizes.push(quote! {
                total += ::vercode::VerCodable::size_version(#temp_var, version);
            });
        }
    }
    sizes
}

#[proc_macro_derive(VercodeTransparent)]
pub fn derive_vercode_transparent(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let generics = &input.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    // Verify it's a newtype struct and get field accessor
    let (inner_type, field_accessor, construction) = match &input.data {
        Data::Struct(s) => match &s.fields {
            Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
                let ty = &fields.unnamed.first().unwrap().ty;
                (ty, quote! { 0 }, quote! { Self(inner) })
            }
            Fields::Named(fields) if fields.named.len() == 1 => {
                let field = fields.named.first().unwrap();
                let ty = &field.ty;
                let field_name = field.ident.as_ref().unwrap();
                (
                    ty,
                    quote! { #field_name },
                    quote! { Self { #field_name: inner } },
                )
            }
            _ => panic!(
                "VercodeTransparent can only be used on newtype structs with exactly one field"
            ),
        },
        _ => panic!("VercodeTransparent can only be used on structs"),
    };

    let expanded = quote! {
        impl #impl_generics ::vercode::VerCodable for #name #ty_generics #where_clause {
            const MAX_VERSION: u32 = <#inner_type as ::vercode::VerCodable>::MAX_VERSION;

            #[inline(always)]
            fn write_version(&self, version: u32, buf: &mut [u8]) -> usize {
                ::vercode::VerCodable::write_version(&self.#field_accessor, version, buf)
            }

            #[inline(always)]
            fn read_version(version: u32, buf: &[u8]) -> ::std::result::Result<(Self, usize), ::vercode::InvalidEncoding> {
                let (inner, size) = <#inner_type as ::vercode::VerCodable>::read_version(version, buf)?;
                Ok((#construction, size))
            }

            #[inline(always)]
            fn size_version(&self, version: u32) -> usize {
                ::vercode::VerCodable::size_version(&self.#field_accessor, version)
            }

            #[inline(always)]
            fn write_option(this: Option<&Self>, version: u32, buf: &mut [u8]) -> usize {
                ::vercode::VerCodable::write_option(
                    this.map(|this| &this.#field_accessor),
                    version,
                    buf,
                )
            }

            #[inline(always)]
            fn read_option(version: u32, buf: &[u8]) -> Result<(Option<Self>, usize), ::vercode::InvalidEncoding> {
                let (inner_option, size) = ::vercode::VerCodable::read_option(version, buf)?;
                let result_option = inner_option.map(|inner| #construction);
                Ok((result_option, size))
            }

            #[inline(always)]
            fn size_option_version(this: &Option<Self>, version: u32) -> usize {
                let inner_option = this.as_ref().map(|this| this.#field_accessor);
                <#inner_type as ::vercode::VerCodable>::size_option_version(&inner_option, version)
            }
        }
    };

    TokenStream::from(expanded)
}

#[proc_macro_derive(Vercode, attributes(version))]
pub fn derive_vercode(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let generics = &input.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    match &input.data {
        Data::Struct(s) => {
            derive_struct(name, &impl_generics, &ty_generics, &where_clause, &s.fields)
        }
        Data::Enum(e) => derive_enum(
            name,
            &impl_generics,
            &ty_generics,
            &where_clause,
            &e.variants,
        ),
        _ => panic!("Vercode only supports structs and enums"),
    }
}

fn derive_struct(
    name: &syn::Ident,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: &Option<&syn::WhereClause>,
    fields: &Fields,
) -> TokenStream {
    let field_infos = extract_field_info(fields);
    let batches = create_version_batches(field_infos.clone());

    let max_version_expr = calculate_max_version_expr(&field_infos);

    let is_named = matches!(fields, Fields::Named(_));
    let writes = generate_field_writes(&batches, is_named);
    let sizes = generate_field_sizes(&batches, is_named);
    let reads = generate_field_reads(&batches);
    let construction = generate_struct_construction(name, fields, &field_infos);

    let expanded = quote! {
        impl #impl_generics ::vercode::VerCodable for #name #ty_generics #where_clause {
            const MAX_VERSION: u32 = #max_version_expr;

            #[inline(always)]
            fn write_version(&self, version: u32, buf: &mut [u8]) -> usize {
                let total_data = self.size_version(version);
                buf[..4].copy_from_slice(&(total_data as u32).to_le_bytes());
                let mut offset = 4usize;
                #(#writes)*
                offset
            }

            #[inline(always)]
            fn read_version(version: u32, buf: &[u8]) -> ::std::result::Result<(Self, usize), ::vercode::InvalidEncoding> {
                if buf.len() < 4 { return Err(::vercode::InvalidEncoding); }
                let length = u32::from_le_bytes(buf[..4].try_into().unwrap()) as usize;
                let mut offset = 4usize;
                #(#reads)*
                let result = #construction;
                Ok((result, offset))
            }

            #[inline(always)]
            fn size_version(&self, version: u32) -> usize {
                let mut total = 4usize;
                #(#sizes)*
                total
            }
        }
    };
    TokenStream::from(expanded)
}

fn derive_enum(
    name: &syn::Ident,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: &Option<&syn::WhereClause>,
    variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>,
) -> TokenStream {
    // Process all variants once
    let variant_infos: Vec<VariantInfo> = variants
        .iter()
        .enumerate()
        .map(|(idx, variant)| VariantInfo::new(idx, variant))
        .collect();

    // Calculate max version expression
    // Generate const expression that computes max across all variants
    let variant_max_exprs: Vec<_> = variant_infos.iter().map(|v| v.max_version_expr()).collect();

    let max_version_expr = if variant_max_exprs.is_empty() {
        quote! { 0 }
    } else {
        quote! {
            {
                let mut max = 0;
                #(
                    {
                        let variant_max = #variant_max_exprs;
                        if variant_max > max {
                            max = variant_max;
                        }
                    }
                )*
                max
            }
        }
    };

    // Generate match arms using the processed variant info
    let write_arms: Vec<_> = variant_infos.iter().map(|v| v.write_arm(name)).collect();
    let size_arms: Vec<_> = variant_infos.iter().map(|v| v.size_arm(name)).collect();
    let read_arms: Vec<_> = variant_infos.iter().map(|v| v.read_arm(name)).collect();

    let expanded = quote! {
        impl #impl_generics ::vercode::VerCodable for #name #ty_generics #where_clause {
            const MAX_VERSION: u32 = #max_version_expr;

            #[inline(always)]
            fn write_version(&self, version: u32, buf: &mut [u8]) -> usize {
                let total_data = self.size_version(version);
                buf[..4].copy_from_slice(&(total_data as u32).to_le_bytes());
                let mut offset = 4usize;
                match self {
                    #(#write_arms)*
                }
                offset
            }

            #[inline(always)]
            fn read_version(version: u32, buf: &[u8]) -> ::std::result::Result<(Self, usize), ::vercode::InvalidEncoding> {
                if buf.len() < 6 { return Err(::vercode::InvalidEncoding); }
                let length = u32::from_le_bytes(buf[..4].try_into().unwrap()) as usize;
                let discriminant = u16::from_le_bytes(buf[4..6].try_into().unwrap()) as u32;
                let mut offset = 6usize;

                let result = match discriminant {
                    #(#read_arms,)*
                    _ => return Err(::vercode::InvalidEncoding),
                };
                Ok((result, offset))
            }

            #[inline(always)]
            fn size_version(&self, version: u32) -> usize {
                let mut total = 6usize; // length prefix (4 bytes) + discriminant (2 bytes)
                match self {
                    #(#size_arms)*
                }
                total
            }
        }
    };

    TokenStream::from(expanded)
}