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
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
// 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! {
::ink_abi::layout2::FieldLayout::new(
#ident,
<#ty as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
)
}
})
}
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! {
gen impl ::ink_core::storage2::traits::StorageLayout for @Self {
fn layout(__key_ptr: &mut ::ink_core::storage2::traits::KeyPtr) -> ::ink_abi::layout2::Layout {
::ink_abi::layout2::Layout::Struct(
::ink_abi::layout2::StructLayout::new(vec![
#(#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;
(
::ink_abi::layout2::Discriminant::from(#discriminant),
::ink_abi::layout2::StructLayout::new(vec![
#(#field_layouts ,)*
]),
)
}
}
});
s.gen_impl(quote! {
gen impl ::ink_core::storage2::traits::StorageLayout for @Self {
fn layout(__key_ptr: &mut ::ink_core::storage2::traits::KeyPtr) -> ::ink_abi::layout2::Layout {
let dispatch_key = __key_ptr.advance_by(1);
::ink_abi::layout2::Layout::Enum(
::ink_abi::layout2::EnumLayout::new(
::ink_abi::layout2::LayoutKey::from(dispatch_key),
vec![
#(#variant_layouts ,)*
]
)
)
}
}
})
}
pub fn storage_layout_derive(mut s: synstructure::Structure) -> TokenStream2 {
s.bind_with(|_| synstructure::BindStyle::Move);
s.add_bounds(synstructure::AddBounds::Generics);
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"),
}
}