summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorKonstantin Molchanov <Konstantin Molchanov>2016-05-31 22:33:51 +0400
committerKonstantin Molchanov <Konstantin Molchanov>2016-05-31 22:33:51 +0400
commitf44e0653560ea180ea1d4200f358a641816cdf2b (patch)
tree0b70239b8dd382c93391aeec7108e4e0a9a7cdad /lib
parentc821cebf27a4ffa923580c5c034074eeb6547dca (diff)
downloadNim-f44e0653560ea180ea1d4200f358a641816cdf2b.tar.gz
sttdlib: asyncdispatch: `all`: Add Future[void] support.
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/asyncdispatch.nim34
1 files changed, 19 insertions, 15 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index aa3fff581..50349b104 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -9,7 +9,7 @@
 
 include "system/inclrtl"
 
-import os, oids, tables, strutils, macros, times, heapqueue
+import os, oids, tables, strutils, macros, times, heapqueue, sequtils
 
 import nativesockets, net, queues
 
@@ -355,30 +355,34 @@ proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
   fut2.callback = cb
   return retFuture
 
-proc all*[T](futs: varargs[Future[T]]): Future[seq[T]] =
+proc all*[T](futs: varargs[Future[T]]): auto =
   ## Returns a future which will complete once all futures in ``futs``
   ## complete.
   ##
   ## The resulting future will hold the values of all awaited futures,
   ## in the order they are passed.
 
-  var
-    retFuture = newFuture[seq[T]]("asyncdispatch.all")
-    retValues = newSeq[T](len(futs))
-    completedFutures = 0
+  when T is void:
+    return foldl(futs, a and b)
 
-  for i, fut in futs:
-    proc setCallback(i: int) =
-      fut.callback = proc(f: Future[T]) =
-        retValues[i] = f.read()
-        inc(completedFutures)
+  else:
+    var
+      retFuture = newFuture[seq[T]]("asyncdispatch.all")
+      retValues = newSeq[T](len(futs))
+      completedFutures = 0
 
-        if completedFutures == len(retValues):
-          retFuture.complete(retValues)
+    for i, fut in futs:
+      proc setCallback(i: int) =
+        fut.callback = proc(f: Future[T]) =
+          retValues[i] = f.read()
+          inc(completedFutures)
 
-    setCallback(i)
+          if completedFutures == len(retValues):
+            retFuture.complete(retValues)
 
-  return retFuture
+      setCallback(i)
+
+    return retFuture
 
 type
   PDispatcherBase = ref object of RootRef