storage_layout.rs 11.2 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
// 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 crate::storage_layout_derive;

#[test]
fn unit_struct_works() {
    synstructure::test_derive! {
        storage_layout_derive {
            struct UnitStruct;
        }
        expands to {
24
            const _: () = {
25
                impl ::ink_core::storage2::traits::StorageLayout for UnitStruct {
26
27
28
                    fn layout(__key_ptr: &mut ::ink_core::storage2::traits::KeyPtr) -> ::ink_metadata::layout2::Layout {
                        ::ink_metadata::layout2::Layout::Struct(
                            ::ink_metadata::layout2::StructLayout::new(vec![])
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
                        )
                    }
                }
            };
        }
    }
}

#[test]
fn tuple_struct_works() {
    synstructure::test_derive! {
        storage_layout_derive {
            struct TupleStruct(bool, u32, i64);
        }
        expands to {
44
            const _: () = {
45
                impl ::ink_core::storage2::traits::StorageLayout for TupleStruct {
46
47
48
49
                    fn layout(__key_ptr: &mut ::ink_core::storage2::traits::KeyPtr) -> ::ink_metadata::layout2::Layout {
                        ::ink_metadata::layout2::Layout::Struct(
                            ::ink_metadata::layout2::StructLayout::new(vec![
                                ::ink_metadata::layout2::FieldLayout::new(
50
51
52
                                    None,
                                    <bool as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                ),
53
                                ::ink_metadata::layout2::FieldLayout::new(
54
55
56
                                    None,
                                    <u32 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                ),
57
                                ::ink_metadata::layout2::FieldLayout::new(
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
                                    None,
                                    <i64 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                ),
                            ])
                        )
                    }
                }
            };
        }
    }
}

#[test]
fn named_fields_struct_works() {
    synstructure::test_derive! {
        storage_layout_derive {
            struct NamedFieldsStruct {
                a: bool,
                b: u32,
                c: i64,
            }
        }
        expands to {
81
            const _: () = {
82
                impl ::ink_core::storage2::traits::StorageLayout for NamedFieldsStruct {
83
84
85
86
                    fn layout(__key_ptr: &mut ::ink_core::storage2::traits::KeyPtr) -> ::ink_metadata::layout2::Layout {
                        ::ink_metadata::layout2::Layout::Struct(
                            ::ink_metadata::layout2::StructLayout::new(vec![
                                ::ink_metadata::layout2::FieldLayout::new(
87
88
89
                                    Some("a"),
                                    <bool as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                ),
90
                                ::ink_metadata::layout2::FieldLayout::new(
91
92
93
                                    Some("b"),
                                    <u32 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                ),
94
                                ::ink_metadata::layout2::FieldLayout::new(
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                                    Some("c"),
                                    <i64 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                ),
                            ])
                        )
                    }
                }
            };
        }
    }
}

#[test]
fn clike_enum_works() {
    synstructure::test_derive! {
        storage_layout_derive {
            enum ClikeEnum { A, B, C }
        }
        expands to {
114
            const _: () = {
115
                impl ::ink_core::storage2::traits::StorageLayout for ClikeEnum {
116
                    fn layout(__key_ptr: &mut ::ink_core::storage2::traits::KeyPtr) -> ::ink_metadata::layout2::Layout {
117
                        let dispatch_key = __key_ptr.advance_by(1);
118
119
120
                        ::ink_metadata::layout2::Layout::Enum(
                            ::ink_metadata::layout2::EnumLayout::new(
                                ::ink_metadata::layout2::LayoutKey::from(dispatch_key),
121
122
123
124
125
                                vec![
                                    {
                                        let mut __variant_key_ptr = __key_ptr.clone();
                                        let mut __key_ptr = &mut __variant_key_ptr;
                                        (
126
127
                                            ::ink_metadata::layout2::Discriminant::from(0usize),
                                            ::ink_metadata::layout2::StructLayout::new(vec![]),
128
129
130
131
132
133
                                        )
                                    },
                                    {
                                        let mut __variant_key_ptr = __key_ptr.clone();
                                        let mut __key_ptr = &mut __variant_key_ptr;
                                        (
134
135
                                            ::ink_metadata::layout2::Discriminant::from(1usize),
                                            ::ink_metadata::layout2::StructLayout::new(vec![]),
136
137
138
139
140
141
                                        )
                                    },
                                    {
                                        let mut __variant_key_ptr = __key_ptr.clone();
                                        let mut __key_ptr = &mut __variant_key_ptr;
                                        (
142
143
                                            ::ink_metadata::layout2::Discriminant::from(2usize),
                                            ::ink_metadata::layout2::StructLayout::new(vec![]),
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
                                        )
                                    },
                                ]
                            )
                        )
                    }
                }
            };
        }
    }
}

#[test]
fn mixed_enum_works() {
    synstructure::test_derive! {
        storage_layout_derive {
            enum MixedEnum {
                A,
                B(bool, u32, i64),
                C{
                    a: bool,
                    b: u32,
                    c: i64,
                }
            }
        }
        expands to {
171
            const _: () = {
172
                impl ::ink_core::storage2::traits::StorageLayout for MixedEnum {
173
                    fn layout(__key_ptr: &mut ::ink_core::storage2::traits::KeyPtr) -> ::ink_metadata::layout2::Layout {
174
                        let dispatch_key = __key_ptr.advance_by(1);
175
176
177
                        ::ink_metadata::layout2::Layout::Enum(
                            ::ink_metadata::layout2::EnumLayout::new(
                                ::ink_metadata::layout2::LayoutKey::from(dispatch_key),
178
179
180
181
182
                                vec![
                                    {
                                        let mut __variant_key_ptr = __key_ptr.clone();
                                        let mut __key_ptr = &mut __variant_key_ptr;
                                        (
183
184
                                            ::ink_metadata::layout2::Discriminant::from(0usize),
                                            ::ink_metadata::layout2::StructLayout::new(vec![]),
185
186
187
188
189
190
                                        )
                                    },
                                    {
                                        let mut __variant_key_ptr = __key_ptr.clone();
                                        let mut __key_ptr = &mut __variant_key_ptr;
                                        (
191
192
193
                                            ::ink_metadata::layout2::Discriminant::from(1usize),
                                            ::ink_metadata::layout2::StructLayout::new(vec![
                                                ::ink_metadata::layout2::FieldLayout::new(
194
195
196
                                                    None,
                                                    <bool as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                                ),
197
                                                ::ink_metadata::layout2::FieldLayout::new(
198
199
200
                                                    None,
                                                    <u32 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                                ),
201
                                                ::ink_metadata::layout2::FieldLayout::new(
202
203
204
205
206
207
208
209
210
211
                                                    None,
                                                    <i64 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                                ),
                                            ]),
                                        )
                                    },
                                    {
                                        let mut __variant_key_ptr = __key_ptr.clone();
                                        let mut __key_ptr = &mut __variant_key_ptr;
                                        (
212
213
214
                                            ::ink_metadata::layout2::Discriminant::from(2usize),
                                            ::ink_metadata::layout2::StructLayout::new(vec![
                                                ::ink_metadata::layout2::FieldLayout::new(
215
216
217
                                                    Some("a"),
                                                    <bool as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                                ),
218
                                                ::ink_metadata::layout2::FieldLayout::new(
219
220
221
                                                    Some("b"),
                                                    <u32 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                                ),
222
                                                ::ink_metadata::layout2::FieldLayout::new(
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
                                                    Some("c"),
                                                    <i64 as ::ink_core::storage2::traits::StorageLayout>::layout(__key_ptr),
                                                ),
                                            ]),
                                        )
                                    },
                                ]
                            )
                        )
                    }
                }
            };
        }
    }
}