storage_layout.rs 4.08 KB
Newer Older
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
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use proc_macro2::TokenStream as TokenStream2;
use quote::quote;

fn field_layout<'a>(
    variant: &'a synstructure::VariantInfo,
) -> impl Iterator<Item = TokenStream2> + 'a {
    variant.ast().fields.iter().map(|field| {
        let ident = match field.ident.as_ref() {
            Some(ident) => {
                let ident_str = ident.to_string();
                quote! { Some(#ident_str) }
            }
            None => quote! { None },
        };
        let ty = &field.ty;
        quote! {
31
            ::ink_metadata::layout::FieldLayout::new(
32
                #ident,
33
                <#ty as ::ink_core::storage::traits::StorageLayout>::layout(__key_ptr),
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
            )
        }
    })
}

fn storage_layout_struct(s: &synstructure::Structure) -> TokenStream2 {
    assert!(
        matches!(s.ast().data, syn::Data::Struct(_)),
        "s must be a struct item"
    );
    assert!(
        s.variants().len() == 1,
        "structs must have at most one variant"
    );
    let variant: &synstructure::VariantInfo = &s.variants()[0];
    let field_layouts = field_layout(variant);
    s.gen_impl(quote! {
51
52
53
54
        gen impl ::ink_core::storage::traits::StorageLayout for @Self {
            fn layout(__key_ptr: &mut ::ink_core::storage::traits::KeyPtr) -> ::ink_metadata::layout::Layout {
                ::ink_metadata::layout::Layout::Struct(
                    ::ink_metadata::layout::StructLayout::new(vec![
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
                        #(#field_layouts ,)*
                    ])
                )
            }
        }
    })
}

fn storage_layout_enum(s: &synstructure::Structure) -> TokenStream2 {
    assert!(
        matches!(s.ast().data, syn::Data::Enum(_)),
        "s must be an enum item"
    );
    let variant_layouts = s.variants().iter().enumerate().map(|(n, variant)| {
        let discriminant = variant
            .ast()
            .discriminant
            .as_ref()
            .map(|(_, expr)| quote! { #expr })
            .unwrap_or_else(|| quote! { #n });
        let field_layouts = field_layout(variant);
        quote! {
            {
                let mut __variant_key_ptr = __key_ptr.clone();
                let mut __key_ptr = &mut __variant_key_ptr;
                (
81
82
                    ::ink_metadata::layout::Discriminant::from(#discriminant),
                    ::ink_metadata::layout::StructLayout::new(vec![
83
84
85
86
87
88
89
                        #(#field_layouts ,)*
                    ]),
                )
            }
        }
    });
    s.gen_impl(quote! {
90
91
        gen impl ::ink_core::storage::traits::StorageLayout for @Self {
            fn layout(__key_ptr: &mut ::ink_core::storage::traits::KeyPtr) -> ::ink_metadata::layout::Layout {
92
                let dispatch_key = __key_ptr.advance_by(1);
93
94
95
                ::ink_metadata::layout::Layout::Enum(
                    ::ink_metadata::layout::EnumLayout::new(
                        ::ink_metadata::layout::LayoutKey::from(dispatch_key),
96
97
98
99
100
101
102
103
104
105
106
                        vec![
                            #(#variant_layouts ,)*
                        ]
                    )
                )
            }
        }
    })
}

pub fn storage_layout_derive(mut s: synstructure::Structure) -> TokenStream2 {
107
108
109
    s.bind_with(|_| synstructure::BindStyle::Move)
        .add_bounds(synstructure::AddBounds::Generics)
        .underscore_const(true);
110
111
112
113
114
115
    match s.ast().data {
        syn::Data::Struct(_) => storage_layout_struct(&s),
        syn::Data::Enum(_) => storage_layout_enum(&s),
        _ => panic!("cannot derive `StorageLayout` for Rust `union` items"),
    }
}