| 1 | struct Encoder {} |
| 2 | |
| 3 | struct Writer {} |
| 4 | |
| 5 | struct ComptimeTypeofIdxInner { |
| 6 | x int |
| 7 | } |
| 8 | |
| 9 | struct ComptimeTypeofIdxOuter { |
| 10 | mut: |
| 11 | inner &ComptimeTypeofIdxInner = unsafe { nil } |
| 12 | } |
| 13 | |
| 14 | struct StructType[T] { |
| 15 | mut: |
| 16 | val &T |
| 17 | val2 T |
| 18 | } |
| 19 | |
| 20 | fn (e &Encoder) encode_struct[U](val U, mut wr Writer) ! { |
| 21 | $for field in U.fields { |
| 22 | if field.indirections == 1 { |
| 23 | assert field.indirections == 1 |
| 24 | value := val.$(field.name) |
| 25 | $if field.indirections == 1 { |
| 26 | assert *value == 'ads' |
| 27 | } $else { |
| 28 | assert false |
| 29 | } |
| 30 | } else { |
| 31 | assert field.name == 'val2' |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn test_indirection_checking() { |
| 37 | e := Encoder{} |
| 38 | mut sb := Writer{} |
| 39 | mut string_pointer := 'ads' |
| 40 | e.encode_struct(StructType[string]{ val: &string_pointer }, mut sb)! |
| 41 | } |
| 42 | |
| 43 | fn allocate_comptime_typeof_idx_ptrs[T](mut s T) { |
| 44 | $for f in T.fields { |
| 45 | $if f.indirections == 1 { |
| 46 | s.$(f.name) = &typeof(s.$(f.name)).idx{} |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn test_comptime_typeof_idx_struct_init() { |
| 52 | mut outer := ComptimeTypeofIdxOuter{} |
| 53 | allocate_comptime_typeof_idx_ptrs(mut outer) |
| 54 | assert outer.inner != unsafe { nil } |
| 55 | assert outer.inner.x == 0 |
| 56 | } |
| 57 | |