summary refs log tree commit diff stats
path: root/src/json
diff options
context:
space:
mode:
authorCrystal <crystal@wizard.tower>2024-04-15 19:35:19 +0100
committerCrystal <crystal@wizard.tower>2024-04-15 19:35:19 +0100
commit7f1258ae6f9971bc2a22c7fe2672d90ee976b1f6 (patch)
tree8de37e5c8c6945db07f5e5162e1740aed2d28b28 /src/json
parent6bcc912cafd549558b9720de53ff15758a28f9e0 (diff)
downloadwww-master.tar.gz
Me when the when me me HEAD master
Diffstat (limited to 'src/json')
0 files changed, 0 insertions, 0 deletions
1d186ee9c335a54ae9700110afe4ec9e888b7b9d'>1d186ee9c ^
10142e4a8 ^
































1d186ee9c ^
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


                  
 
            
 
































                                                                      
 
discard """
output: "1\nmessa"
"""

import async

# 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()