alloy_rlp_derive/
en.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
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
use crate::utils::{
    attributes_include, field_ident, is_optional, make_generics, parse_struct, EMPTY_STRING_CODE,
};
use proc_macro2::TokenStream;
use quote::quote;
use std::iter::Peekable;
use syn::{Error, Result};

pub(crate) fn impl_encodable(ast: &syn::DeriveInput) -> Result<TokenStream> {
    let body = parse_struct(ast, "RlpEncodable")?;

    let mut fields = body
        .fields
        .iter()
        .enumerate()
        .filter(|(_, field)| !attributes_include(&field.attrs, "skip"))
        .peekable();

    let supports_trailing_opt = attributes_include(&ast.attrs, "trailing");

    let mut encountered_opt_item = false;
    let mut length_exprs = Vec::with_capacity(body.fields.len());
    let mut encode_exprs = Vec::with_capacity(body.fields.len());

    while let Some((i, field)) = fields.next() {
        let is_opt = is_optional(field);
        if is_opt {
            if !supports_trailing_opt {
                let msg = "optional fields are disabled.\nAdd the `#[rlp(trailing)]` attribute to the struct in order to enable optional fields";
                return Err(Error::new_spanned(field, msg));
            }
            encountered_opt_item = true;
        } else if encountered_opt_item {
            let msg = "all the fields after the first optional field must be optional";
            return Err(Error::new_spanned(field, msg));
        }

        length_exprs.push(encodable_length(i, field, is_opt, fields.clone()));
        encode_exprs.push(encodable_field(i, field, is_opt, fields.clone()));
    }

    let name = &ast.ident;
    let generics = make_generics(&ast.generics, quote!(alloy_rlp::Encodable));
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    Ok(quote! {
        const _: () = {
            extern crate alloy_rlp;

            impl #impl_generics alloy_rlp::Encodable for #name #ty_generics #where_clause {
                #[inline]
                fn length(&self) -> usize {
                    let payload_length = self._alloy_rlp_payload_length();
                    payload_length + alloy_rlp::length_of_length(payload_length)
                }

                #[inline]
                fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
                    alloy_rlp::Header {
                        list: true,
                        payload_length: self._alloy_rlp_payload_length(),
                    }
                    .encode(out);
                    #(#encode_exprs)*
                }
            }

            impl #impl_generics #name #ty_generics #where_clause {
                #[allow(unused_parens)]
                #[inline]
                fn _alloy_rlp_payload_length(&self) -> usize {
                    0usize #( + #length_exprs)*
                }
            }
        };
    })
}

pub(crate) fn impl_encodable_wrapper(ast: &syn::DeriveInput) -> Result<TokenStream> {
    let body = parse_struct(ast, "RlpEncodableWrapper")?;

    let name = &ast.ident;
    let generics = make_generics(&ast.generics, quote!(alloy_rlp::Encodable));
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let ident = {
        let fields: Vec<_> = body.fields.iter().collect();
        if let [field] = fields[..] {
            field_ident(0, field)
        } else {
            let msg = "`RlpEncodableWrapper` is only derivable for structs with one field";
            return Err(Error::new(name.span(), msg));
        }
    };

    Ok(quote! {
        const _: () = {
            extern crate alloy_rlp;

            impl #impl_generics alloy_rlp::Encodable for #name #ty_generics #where_clause {
                #[inline]
                fn length(&self) -> usize {
                    alloy_rlp::Encodable::length(&self.#ident)
                }

                #[inline]
                fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
                    alloy_rlp::Encodable::encode(&self.#ident, out)
                }
            }
        };
    })
}

pub(crate) fn impl_max_encoded_len(ast: &syn::DeriveInput) -> Result<TokenStream> {
    let body = parse_struct(ast, "RlpMaxEncodedLen")?;

    let tys = body
        .fields
        .iter()
        .filter(|field| !attributes_include(&field.attrs, "skip"))
        .map(|field| &field.ty);

    let name = &ast.ident;

    let generics = make_generics(&ast.generics, quote!(alloy_rlp::MaxEncodedLenAssoc));
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let imp = quote! {{
        let _sz = 0usize #( + <#tys as alloy_rlp::MaxEncodedLenAssoc>::LEN )*;
        _sz + alloy_rlp::length_of_length(_sz)
    }};

    // can't do operations with const generic params / associated consts in the
    // non-associated impl
    let can_derive_non_assoc = ast
        .generics
        .params
        .iter()
        .all(|g| !matches!(g, syn::GenericParam::Type(_) | syn::GenericParam::Const(_)));
    let non_assoc_impl =  can_derive_non_assoc.then(|| {
        quote! {
            unsafe impl #impl_generics alloy_rlp::MaxEncodedLen<#imp> for #name #ty_generics #where_clause {}
        }
    });

    Ok(quote! {
        #[allow(unsafe_code)]
        const _: () = {
            extern crate alloy_rlp;

            #non_assoc_impl

            unsafe impl #impl_generics alloy_rlp::MaxEncodedLenAssoc for #name #ty_generics #where_clause {
                const LEN: usize = #imp;
            }
        };
    })
}

fn encodable_length<'a>(
    index: usize,
    field: &syn::Field,
    is_opt: bool,
    mut remaining: Peekable<impl Iterator<Item = (usize, &'a syn::Field)>>,
) -> TokenStream {
    let ident = field_ident(index, field);

    if is_opt {
        let default = if remaining.peek().is_some() {
            let condition = remaining_opt_fields_some_condition(remaining);
            quote! { (#condition) as usize }
        } else {
            quote! { 0 }
        };

        quote! { self.#ident.as_ref().map(|val| alloy_rlp::Encodable::length(val)).unwrap_or(#default) }
    } else {
        quote! { alloy_rlp::Encodable::length(&self.#ident) }
    }
}

fn encodable_field<'a>(
    index: usize,
    field: &syn::Field,
    is_opt: bool,
    mut remaining: Peekable<impl Iterator<Item = (usize, &'a syn::Field)>>,
) -> TokenStream {
    let ident = field_ident(index, field);

    if is_opt {
        let if_some_encode = quote! {
            if let Some(val) = self.#ident.as_ref() {
                alloy_rlp::Encodable::encode(val, out)
            }
        };

        if remaining.peek().is_some() {
            let condition = remaining_opt_fields_some_condition(remaining);
            quote! {
                #if_some_encode
                else if #condition {
                    out.put_u8(#EMPTY_STRING_CODE);
                }
            }
        } else {
            quote! { #if_some_encode }
        }
    } else {
        quote! { alloy_rlp::Encodable::encode(&self.#ident, out); }
    }
}

fn remaining_opt_fields_some_condition<'a>(
    remaining: impl Iterator<Item = (usize, &'a syn::Field)>,
) -> TokenStream {
    let conditions = remaining.map(|(index, field)| {
        let ident = field_ident(index, field);
        quote! { self.#ident.is_some() }
    });
    quote! { #(#conditions)||* }
}