linera-witty 0.15.21

Generation of WIT compatible host code from Rust code
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Tests for the `WitType` derive macro.

#[path = "common/types.rs"]
mod types;

use std::{collections::BTreeMap, rc::Rc, sync::Arc};

use linera_witty::{HList, Layout, RegisterWitTypes, WitType};

use self::types::{
    Branch, Enum, Leaf, RecordWithDoublePadding, SimpleWrapper, SliceWrapper,
    SpecializedGenericEnum, SpecializedGenericStruct, StructWithHeapFields, StructWithLists,
    TupleWithPadding, TupleWithoutPadding,
};

/// Checks the memory size, layout and WIT type declaration derived for a wrapper type.
#[test]
fn test_simple_bool_wrapper() {
    test_wit_type_implementation::<SimpleWrapper>(ExpectedMetadata {
        size: 1,
        alignment: 1,
        flat_layout_length: 1,
        declaration: concat!(
            "    record simple-wrapper {\n",
            "        inner0: bool,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT type declaration derived for a type with multiple fields
/// ordered in a way that doesn't require any padding.
#[test]
fn test_tuple_struct_without_padding() {
    test_wit_type_implementation::<TupleWithoutPadding>(ExpectedMetadata {
        size: 16,
        alignment: 8,
        flat_layout_length: 3,
        declaration: concat!(
            "    record tuple-without-padding {\n",
            "        inner0: u64,\n",
            "        inner1: s32,\n",
            "        inner2: s16,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT type declaration derived for a type with multiple fields
/// ordered in a way that requires padding between all fields.
#[test]
fn test_tuple_struct_with_padding() {
    test_wit_type_implementation::<TupleWithPadding>(ExpectedMetadata {
        size: 16,
        alignment: 8,
        flat_layout_length: 3,
        declaration: concat!(
            "    record tuple-with-padding {\n",
            "        inner0: u16,\n",
            "        inner1: u32,\n",
            "        inner2: s64,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT type declaration derived for a type with multiple named
/// fields ordered in a way that requires padding before two fields.
#[test]
fn test_named_struct_with_double_padding() {
    test_wit_type_implementation::<RecordWithDoublePadding>(ExpectedMetadata {
        size: 24,
        alignment: 8,
        flat_layout_length: 4,
        declaration: concat!(
            "    record record-with-double-padding {\n",
            "        first: u16,\n",
            "        second: u32,\n",
            "        third: s8,\n",
            "        fourth: s64,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT type declarations derived for a type that contains a
/// field with a type that also has `WitType` derived for it.
#[test]
fn test_nested_types() {
    test_wit_type_implementation::<Leaf>(ExpectedMetadata {
        size: 24,
        alignment: 8,
        flat_layout_length: 3,
        declaration: concat!(
            "    record leaf {\n",
            "        first: bool,\n",
            "        second: u128,\n",
            "    }\n\n",
            "    type u128 = tuple<u64, u64>;\n"
        ),
    });

    test_wit_type_implementation::<Branch>(ExpectedMetadata {
        size: 56,
        alignment: 8,
        flat_layout_length: 7,
        declaration: concat!(
            "    record branch {\n",
            "        tag: u16,\n",
            "        first-leaf: leaf,\n",
            "        second-leaf: leaf,\n",
            "    }\n\n",
            "    record leaf {\n",
            "        first: bool,\n",
            "        second: u128,\n",
            "    }\n\n",
            "    type u128 = tuple<u64, u64>;\n"
        ),
    });
}

/// Checks the memory size, layout and WIT type declaration derived for an `enum` type.
#[test]
fn test_enum_type() {
    test_wit_type_implementation::<Enum>(ExpectedMetadata {
        size: 24,
        alignment: 8,
        flat_layout_length: 11,
        declaration: concat!(
            "    variant enum {\n",
            "        empty,\n",
            "        large-variant-with-loose-alignment(\
                        tuple<s8, s8, s8, s8, s8, s8, s8, s8, s8, s8>\
                     ),\n",
            "        smaller-variant-with-strict-alignment(u64),\n",
            "    }\n",
        ),
    });
}

/// Checks the memory size, layout and WIT type declaration derived for a specialized generic
/// `struct` type.
#[test]
fn test_specialized_generic_struct() {
    test_wit_type_implementation::<SpecializedGenericStruct<u8, i16>>(ExpectedMetadata {
        size: 12,
        alignment: 4,
        flat_layout_length: 4,
        declaration: concat!(
            "    record specialized-generic-struct {\n",
            "        first: u8,\n",
            "        second: s16,\n",
            "        both: list<tuple<u8, s16>>,\n",
            "    }\n",
        ),
    });
}

/// Checks the memory size, layout and WIT type declaration derived for a specialized generic `enum`
/// type.
#[test]
fn test_specialized_generic_enum_type() {
    test_wit_type_implementation::<SpecializedGenericEnum<Option<bool>, u32>>(ExpectedMetadata {
        size: 12,
        alignment: 4,
        flat_layout_length: 3,
        declaration: concat!(
            "    variant specialized-generic-enum {\n",
            "        none,\n",
            "        first(option<bool>),\n",
            "        maybe-second(option<u32>),\n",
            "    }\n",
        ),
    });
}

/// Checks the memory size, layout and WIT declaration derived for a complex type that has heap
/// allocated fields.
#[test]
fn test_heap_allocated_fields() {
    test_wit_type_implementation::<StructWithHeapFields>(ExpectedMetadata {
        size: 56,
        alignment: 8,
        flat_layout_length: 15,
        declaration: concat!(
            "    variant enum {\n",
            "        empty,\n",
            "        large-variant-with-loose-alignment(\
                        tuple<s8, s8, s8, s8, s8, s8, s8, s8, s8, s8>\
                     ),\n",
            "        smaller-variant-with-strict-alignment(u64),\n",
            "    }\n\n",
            "    record leaf {\n",
            "        first: bool,\n",
            "        second: u128,\n",
            "    }\n\n",
            "    record simple-wrapper {\n",
            "        inner0: bool,\n",
            "    }\n\n",
            "    record struct-with-heap-fields {\n",
            "        boxed: simple-wrapper,\n",
            "        rced: leaf,\n",
            "        arced: enum,\n",
            "    }\n\n",
            "    type u128 = tuple<u64, u64>;\n",
        ),
    });
}

/// Checks the memory size, layout and WIT declaration derived for a slice type.
#[test]
fn test_slice() {
    test_wit_type_implementation::<[SimpleWrapper]>(ExpectedMetadata {
        size: 8,
        alignment: 4,
        flat_layout_length: 2,
        declaration: concat!(
            "    record simple-wrapper {\n",
            "        inner0: bool,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT declaration derived for a [`Vec`] type.
#[test]
fn test_vec() {
    test_wit_type_implementation::<Vec<SimpleWrapper>>(ExpectedMetadata {
        size: 8,
        alignment: 4,
        flat_layout_length: 2,
        declaration: concat!(
            "    record simple-wrapper {\n",
            "        inner0: bool,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT declaration derived for a boxed slice type.
#[test]
fn test_boxed_slice() {
    test_wit_type_implementation::<Box<[TupleWithPadding]>>(ExpectedMetadata {
        size: 8,
        alignment: 4,
        flat_layout_length: 2,
        declaration: concat!(
            "    record tuple-with-padding {\n",
            "        inner0: u16,\n",
            "        inner1: u32,\n",
            "        inner2: s64,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT declaration derived for an rc-ed slice type.
#[test]
fn test_rced_slice() {
    test_wit_type_implementation::<Rc<[Leaf]>>(ExpectedMetadata {
        size: 8,
        alignment: 4,
        flat_layout_length: 2,
        declaration: concat!(
            "    record leaf {\n",
            "        first: bool,\n",
            "        second: u128,\n",
            "    }\n\n",
            "    type u128 = tuple<u64, u64>;\n"
        ),
    });
}

/// Checks the memory size, layout and WIT declaration derived for an arc-ed slice type.
#[test]
fn test_arced_slice() {
    test_wit_type_implementation::<Arc<[RecordWithDoublePadding]>>(ExpectedMetadata {
        size: 8,
        alignment: 4,
        flat_layout_length: 2,
        declaration: concat!(
            "    record record-with-double-padding {\n",
            "        first: u16,\n",
            "        second: u32,\n",
            "        third: s8,\n",
            "        fourth: s64,\n",
            "    }\n"
        ),
    });
}

/// Check the memory size, layout and WIT declaration derived for a type that has a slice
/// field.
#[test]
fn test_slice_field() {
    test_wit_type_implementation::<SliceWrapper>(ExpectedMetadata {
        size: 8,
        alignment: 4,
        flat_layout_length: 2,
        declaration: concat!(
            "    record slice-wrapper {\n",
            "        inner0: list<tuple-without-padding>,\n",
            "    }\n\n",
            "    record tuple-without-padding {\n",
            "        inner0: u64,\n",
            "        inner1: s32,\n",
            "        inner2: s16,\n",
            "    }\n"
        ),
    });
}

/// Checks the memory size, layout and WIT declaration derived for a type that has list
/// fields.
#[test]
fn test_list_fields() {
    test_wit_type_implementation::<StructWithLists>(ExpectedMetadata {
        size: 32,
        alignment: 4,
        flat_layout_length: 8,
        declaration: concat!(
            "    record leaf {\n",
            "        first: bool,\n",
            "        second: u128,\n",
            "    }\n\n",
            "    record record-with-double-padding {\n",
            "        first: u16,\n",
            "        second: u32,\n",
            "        third: s8,\n",
            "        fourth: s64,\n",
            "    }\n\n",
            "    record simple-wrapper {\n",
            "        inner0: bool,\n",
            "    }\n\n",
            "    record struct-with-lists {\n",
            "        vec: list<simple-wrapper>,\n",
            "        boxed-slice: list<tuple-with-padding>,\n",
            "        rced-slice: list<leaf>,\n",
            "        arced-slice: list<record-with-double-padding>,\n",
            "    }\n\n",
            "    record tuple-with-padding {\n",
            "        inner0: u16,\n",
            "        inner1: u32,\n",
            "        inner2: s64,\n",
            "    }\n\n",
            "    type u128 = tuple<u64, u64>;\n"
        ),
    });
}

/// Helper type to make visible what each metadata value is.
#[derive(Clone, Copy, Debug)]
struct ExpectedMetadata {
    size: u32,
    alignment: u32,
    flat_layout_length: usize,
    declaration: &'static str,
}

/// Tests that a type `T` and wrapped versions of it have the `expected` [`WitType`] metadata in
/// their implementations.
fn test_wit_type_implementation<T>(expected: ExpectedMetadata)
where
    T: WitType + ?Sized + 'static,
    Box<T>: WitType,
    Rc<T>: WitType,
    Arc<T>: WitType,
{
    test_single_wit_type_implementation::<&'static T>(expected);
    test_single_wit_type_implementation::<Box<T>>(expected);
    test_single_wit_type_implementation::<Rc<T>>(expected);
    test_single_wit_type_implementation::<Arc<T>>(expected);
}

/// Tests that a type `T` has the `expected` [`WitType`] metadata in its implementation.
fn test_single_wit_type_implementation<T>(expected: ExpectedMetadata)
where
    T: WitType,
{
    assert_eq!(T::SIZE, expected.size);
    assert_eq!(<T as WitType>::Layout::ALIGNMENT, expected.alignment);
    assert_eq!(
        <<T as WitType>::Layout as Layout>::Flat::LEN,
        expected.flat_layout_length
    );
    assert_eq!(wit_type_declaration_of::<T>(), expected.declaration);
}

/// Returns the WIT snippet with the type declarations for `T`.
fn wit_type_declaration_of<T>() -> String
where
    T: WitType,
{
    let mut wit_types = BTreeMap::new();

    <HList![T] as RegisterWitTypes>::register_wit_types(&mut wit_types);

    wit_types
        .into_values()
        .filter(|declaration| !declaration.is_empty())
        .collect::<Vec<_>>()
        .join("\n")
}