packed_layout.rs 15.7 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::packed_layout_derive;

#[test]
fn unit_struct_works() {
    synstructure::test_derive! {
        packed_layout_derive {
            struct UnitStruct;
        }
        expands to {
24
            const _: () = {
25
                impl ::ink_core::storage::traits::PackedLayout for UnitStruct {
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
                    fn pull_packed(&mut self, __key: &::ink_primitives::Key) {
                        match self {
                            UnitStruct => {}
                        }
                    }

                    fn push_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            UnitStruct => {}
                        }
                    }

                    fn clear_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            UnitStruct => {}
                        }
                    }
                }
            };
        }
        no_build
    }
}

#[test]
fn struct_works() {
    synstructure::test_derive! {
        packed_layout_derive {
            struct NamedFields {
                a: i32,
                b: [u8; 32],
                d: Box<i32>,
            }
        }
        expands to {
61
            const _: () = {
62
                impl ::ink_core::storage::traits::PackedLayout for NamedFields {
63
64
65
66
67
68
69
70
                    fn pull_packed(&mut self, __key: &::ink_primitives::Key) {
                        match self {
                            NamedFields {
                                a: __binding_0,
                                b: __binding_1,
                                d: __binding_2,
                            } => {
                                {
71
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_0, __key);
72
73
                                }
                                {
74
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_1, __key);
75
76
                                }
                                {
77
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_2, __key);
78
79
80
81
82
83
84
85
86
87
88
89
                                }
                            }
                        }
                    }
                    fn push_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            NamedFields {
                                a: __binding_0,
                                b: __binding_1,
                                d: __binding_2,
                            } => {
                                {
90
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_0, __key);
91
92
                                }
                                {
93
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_1, __key);
94
95
                                }
                                {
96
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_2, __key);
97
98
99
100
101
102
103
104
105
106
107
108
                                }
                            }
                        }
                    }
                    fn clear_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            NamedFields {
                                a: __binding_0,
                                b: __binding_1,
                                d: __binding_2,
                            } => {
                                {
109
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_0, __key);
110
111
                                }
                                {
112
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_1, __key);
113
114
                                }
                                {
115
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_2, __key);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
                                }
                            }
                        }
                    }
                }
            };
        }
        no_build
    }
}

#[test]
fn enum_works() {
    synstructure::test_derive! {
        packed_layout_derive {
            enum MixedEnum {
                A,
                B(i32, [u8; 32]),
                C { a: i32, b: (bool, i32) },
            }
        }
        expands to {
138
            const _: () = {
139
                impl ::ink_core::storage::traits::PackedLayout for MixedEnum {
140
141
142
143
144
                    fn pull_packed(&mut self, __key: &::ink_primitives::Key) {
                        match self {
                            MixedEnum::A => {}
                            MixedEnum::B(__binding_0, __binding_1,) => {
                                {
145
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_0, __key);
146
147
                                }
                                {
148
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_1, __key);
149
150
151
152
153
154
155
                                }
                            }
                            MixedEnum::C {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
156
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_0, __key);
157
158
                                }
                                {
159
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_1, __key);
160
161
162
163
164
165
166
167
168
                                }
                            }
                        }
                    }
                    fn push_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            MixedEnum::A => {}
                            MixedEnum::B(__binding_0, __binding_1,) => {
                                {
169
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_0, __key);
170
171
                                }
                                {
172
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_1, __key);
173
174
175
176
177
178
179
                                }
                            }
                            MixedEnum::C {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
180
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_0, __key);
181
182
                                }
                                {
183
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_1, __key);
184
185
186
187
188
189
190
191
192
                                }
                            }
                        }
                    }
                    fn clear_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            MixedEnum::A => {}
                            MixedEnum::B(__binding_0, __binding_1,) => {
                                {
193
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_0, __key);
194
195
                                }
                                {
196
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_1, __key);
197
198
199
200
201
202
203
                                }
                            }
                            MixedEnum::C {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
204
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_0, __key);
205
206
                                }
                                {
207
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_1, __key);
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                                }
                            }
                        }
                    }
                }
            };
        }
        no_build
    }
}

#[test]
fn generic_struct_works() {
    synstructure::test_derive! {
        packed_layout_derive {
            struct GenericStruct<T1, T2> {
                a: T1,
                b: (T1, T2),
            }
        }
        expands to {
229
            const _: () = {
230
                impl<T1, T2> ::ink_core::storage::traits::PackedLayout for GenericStruct<T1, T2>
231
                where
232
233
                    T1: ::ink_core::storage::traits::PackedLayout,
                    T2: ::ink_core::storage::traits::PackedLayout
234
235
236
237
238
239
240
241
                {
                    fn pull_packed(&mut self, __key: &::ink_primitives::Key) {
                        match self {
                            GenericStruct {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
242
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_0, __key);
243
244
                                }
                                {
245
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_1, __key);
246
247
248
249
250
251
252
253
254
255
256
                                }
                            }
                        }
                    }
                    fn push_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            GenericStruct {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
257
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_0, __key);
258
259
                                }
                                {
260
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_1, __key);
261
262
263
264
265
266
267
268
269
270
271
                                }
                            }
                        }
                    }
                    fn clear_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            GenericStruct {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
272
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_0, __key);
273
274
                                }
                                {
275
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_1, __key);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
                                }
                            }
                        }
                    }
                }
            };
        }
        no_build
    }
}

#[test]
fn generic_enum_works() {
    synstructure::test_derive! {
        packed_layout_derive {
            enum GenericEnum<T1, T2> {
                Tuple(T1, T2),
                Named { a: T1, b: T2 },
            }
        }
        expands to {
297
            const _: () = {
298
                impl<T1, T2> ::ink_core::storage::traits::PackedLayout for GenericEnum<T1, T2>
299
                where
300
301
                    T1: ::ink_core::storage::traits::PackedLayout,
                    T2: ::ink_core::storage::traits::PackedLayout
302
303
304
305
306
                {
                    fn pull_packed(&mut self, __key: &::ink_primitives::Key) {
                        match self {
                            GenericEnum::Tuple(__binding_0, __binding_1,) => {
                                {
307
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_0, __key);
308
309
                                }
                                {
310
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_1, __key);
311
312
313
314
315
316
317
                                }
                            }
                            GenericEnum::Named {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
318
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_0, __key);
319
320
                                }
                                {
321
                                    ::ink_core::storage::traits::PackedLayout::pull_packed(__binding_1, __key);
322
323
324
325
326
327
328
329
                                }
                            }
                        }
                    }
                    fn push_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            GenericEnum::Tuple(__binding_0, __binding_1,) => {
                                {
330
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_0, __key);
331
332
                                }
                                {
333
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_1, __key);
334
335
336
337
338
339
340
                                }
                            }
                            GenericEnum::Named {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
341
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_0, __key);
342
343
                                }
                                {
344
                                    ::ink_core::storage::traits::PackedLayout::push_packed(__binding_1, __key);
345
346
347
348
349
350
351
352
                                }
                            }
                        }
                    }
                    fn clear_packed(&self, __key: &::ink_primitives::Key) {
                        match self {
                            GenericEnum::Tuple(__binding_0, __binding_1,) => {
                                {
353
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_0, __key);
354
355
                                }
                                {
356
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_1, __key);
357
358
359
360
361
362
363
                                }
                            }
                            GenericEnum::Named {
                                a: __binding_0,
                                b: __binding_1,
                            } => {
                                {
364
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_0, __key);
365
366
                                }
                                {
367
                                    ::ink_core::storage::traits::PackedLayout::clear_packed(__binding_1, __key);
368
369
370
371
372
373
374
375
376
377
                                }
                            }
                        }
                    }
                }
            };
        }
        no_build
    }
}