asn1_rs_derive/
tostatic.rs

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
use proc_macro2::Span;
use quote::quote;
use syn::{Ident, Lifetime};

pub fn derive_tostatic(s: synstructure::Structure) -> proc_macro2::TokenStream {
    let ast = s.ast();

    let debug_derive = ast.attrs.iter().any(|attr| {
        attr.meta
            .path()
            .is_ident(&Ident::new("debug_derive", Span::call_site()))
    });

    // if deriving a struct, there will be only one variant
    // for enums, this will iterate on each variant
    let body = s.each_variant(|vi| {
        // bindings can be empty for unit variants
        let instrs = vi
            .bindings()
            .iter()
            .enumerate()
            .fold(quote! {}, |acc, (idx, bi)| {
                let ident = Ident::new(&format!("_{idx}"), Span::call_site());
                quote! { #acc let #ident = #bi.to_static(); }
            });
        // use construct() to handle possible cases (unit/named/unnamed)
        let c = vi.construct(|_f, i| {
            let ident = Ident::new(&format!("_{i}"), Span::call_site());
            quote! { #ident }
        });
        quote! { #instrs #c }
    });

    let struct_ident = &ast.ident;

    // check if struct has lifetimes
    let static_token = match ast.generics.lifetimes().count() {
        0 => None,
        1 => {
            let lt = Lifetime::new("'static", Span::call_site());
            Some(quote! {<#lt>})
        }
        _ => {
            let lt_static = Lifetime::new("'static", Span::call_site());
            let lts = ast.generics.lifetimes().map(|_| lt_static.clone());
            Some(quote! {<#(#lts),*>})
        }
    };

    let ts = s.gen_impl(quote! {
        gen impl asn1_rs::ToStatic for @Self {
            type Owned = #struct_ident #static_token;

            fn to_static(&self) -> Self::Owned {
                match *self {
                    #body
                }
            }
        }
    });
    if debug_derive {
        eprintln!("TS: {ts}");
    }
    ts
}