From 10142e4a80a2eb8107c382ffcf30ab9fba51976a Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 13 Apr 2018 12:44:47 +0300 Subject: Fix some usages of typedesc in async procs This also fixes a compilation error in modules, based only on the new async module (i.e. not importing the full asyncdispatch) --- tests/async/tgeneric_async.nim | 43 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/async/tgeneric_async.nim b/tests/async/tgeneric_async.nim index af6370181..bab2d1a31 100644 --- a/tests/async/tgeneric_async.nim +++ b/tests/async/tgeneric_async.nim @@ -1,9 +1,40 @@ +discard """ +output: "1\nmessa" +""" -import asyncdispatch +import async -when true: - # bug #2377 - proc test[T](v: T) {.async.} = - echo $v +# bug #2377 +proc test[T](v: T) {.async.} = + echo $v + +asyncCheck test[int](1) + +# More complex case involving typedesc and static params +type + SomeMsg = object + data: string + +template msgId(M: type SomeMsg): int = 1 + +proc recvMsg(): Future[tuple[msgId: int, msgData: string]] {.async.} = + return (1, "message") + +proc read(data: string, T: type SomeMsg, maxBytes: int): T = + result.data = data[0 ..< min(data.len, maxBytes)] + +proc nextMsg*(MsgType: typedesc, + maxBytes: static[int]): Future[MsgType] {.async.} = + const wantedId = MsgType.msgId + + while true: + var (nextMsgId, nextMsgData) = await recvMsg() + if nextMsgId == wantedId: + return nextMsgData.read(MsgType, maxBytes) + +proc main {.async.} = + let msg = await nextMsg(SomeMsg, 5) + echo msg.data + +asyncCheck main() - asyncCheck test[int](1) -- cgit 1.4.1-2-gfad0 href='/danisanti/profani-tty/stats/src/main.c'>stats
path: root/src/main.c
blob: 968ddf8ab2da1cff6a9860187ace2ec70a73a967 (plain) (blame)
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