v2 / vlib / v / slow_tests / inout / comptime_ptr.vv
56 lines · 45 sloc · 1.23 KB · c63902baf02376c845f89d817c560a8e4def3285
Raw
1struct Encoder {}
2
3struct Writer {}
4
5struct StructTypePointer[T] {
6mut:
7 val &T
8}
9
10struct StructTypePointerPointer[T] {
11mut:
12 val &&T
13}
14
15pub fn (e &Encoder) encode_value[T](val T, mut wr Writer) ! {
16 e.encode_struct[T](val, 1, mut wr)!
17}
18
19pub fn (e &Encoder) encode_value_with_level[T](val T, mut wr Writer) ! {
20 e.encode_struct[T](val, 1, mut wr)!
21 dump(val)
22}
23
24fn (e &Encoder) encode_struct[U](val U, level int, mut wr Writer) ! {
25 $for field in U.fields {
26 $if field.indirections > 0 {
27 $if field.indirections == 1 {
28 e.encode_value_with_level(*val.$(field.name), mut wr)!
29 }
30 $if field.indirections == 2 {
31 e.encode_value_with_level(**val.$(field.name), mut wr)!
32 }
33 $if field.indirections == 3 {
34 e.encode_value_with_level(***val.$(field.name), mut wr)!
35 }
36 }
37 }
38}
39
40fn main() {
41 e := Encoder{}
42 mut sb := Writer{}
43
44 mut string_initialized_with_reference := 'ads'
45 mut bool_initialized_with_reference := false
46
47 e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut
48 sb) or {}
49 e.encode_value(StructTypePointer[bool]{ val: &bool_initialized_with_reference }, mut
50 sb) or {}
51
52 bool_val := true
53 ptr_bool := &bool_val
54
55 e.encode_value(StructTypePointerPointer[bool]{ val: &ptr_bool }, mut sb) or {}
56}
57