v2 / vlib / v / tests / structs / struct_fields_storing_functions_test.v
29 lines · 26 sloc · 465 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1type Async_cb = fn (x []u8, mut y []u8) int
2
3fn async_cb(b []u8, mut res []u8) int {
4 if b.len > 0 {
5 res << b
6 }
7 return 0
8}
9
10struct Ep_arg {
11mut:
12 sfd int
13 cb Async_cb
14}
15
16fn test_struct_fn_field_can_be_used_directly() {
17 buf := [u8(1), 2, 3]
18 mut res := []u8{}
19 res << 0x88
20 async_cb(buf[0..2], mut res)
21 data := Ep_arg{
22 sfd: 1234
23 cb: async_cb
24 }
25 data.cb(buf[1..2], mut res)
26 res << 0x99
27 eprintln(res)
28 assert res == [u8(0x88), 0x01, 0x02, 0x02, 0x99]
29}
30