tinfer_closure_for_nestedproc.nim 889 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. discard """
  2. action: compile
  3. """
  4. # bug #9441
  5. import asyncdispatch, asyncfutures, strtabs
  6. type
  7. Request = object
  8. Context = object
  9. position: int
  10. accept: bool
  11. headers: StringTableRef
  12. Handler = proc (r: ref Request, c: Context): Future[Context]
  13. proc respond(req: Request): Future[void] = discard
  14. proc handle*(h: Handler): auto = # (proc (req: Request): Future[void]) =
  15. proc server(req: Request): Future[void] {.async.} =
  16. let emptyCtx = Context(
  17. position: 0,
  18. accept: true,
  19. headers: newStringTable()
  20. )
  21. var reqHeap = new(Request)
  22. reqHeap[] = req
  23. var
  24. f: Future[Context]
  25. ctx: Context
  26. try:
  27. f = h(reqHeap, emptyCtx)
  28. ctx = await f
  29. except:
  30. discard
  31. if f.failed:
  32. await req.respond()
  33. else:
  34. if not ctx.accept:
  35. await req.respond()
  36. return server
  37. waitFor handle(nil)(Request())