Skip to content

Commit

Permalink
varargs overloads (#477)
Browse files Browse the repository at this point in the history
* varargs overloads

for convenience and compatibility

* no parameterless varargs calls with generic overloads
  • Loading branch information
arnetheduck authored Nov 20, 2023
1 parent f03cdfc commit fa0bf40
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
38 changes: 34 additions & 4 deletions chronos/internal/asyncfutures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1094,10 +1094,18 @@ proc allFutures*[T](futs: varargs[Future[T]]): Future[void] {.
##
## On cancel all the awaited futures ``futs`` WILL NOT BE cancelled.
# Because we can't capture varargs[T] in closures we need to create copy.
var nfuts: seq[FutureBase]
for future in futs:
nfuts.add(future)
allFutures(nfuts)
allFutures(futs.mapIt(FutureBase(it)))

proc allFutures*[T, E](futs: varargs[InternalRaisesFuture[T, E]]): Future[void] {.
async: (raw: true, raises: [CancelledError]).} =
## Returns a future which will complete only when all futures in ``futs``
## will be completed, failed or canceled.
##
## If the argument is empty, the returned future COMPLETES immediately.
##
## On cancel all the awaited futures ``futs`` WILL NOT BE cancelled.
# Because we can't capture varargs[T] in closures we need to create copy.
allFutures(futs.mapIt(FutureBase(it)))

proc allFinished*[F: SomeFuture](futs: varargs[F]): Future[seq[F]] {.
async: (raw: true, raises: [CancelledError]).} =
Expand Down Expand Up @@ -1239,6 +1247,28 @@ proc race*(futs: varargs[FutureBase]): Future[FutureBase] {.

return retFuture

proc race*[T](futs: varargs[Future[T]]): Future[FutureBase] {.
async: (raw: true, raises: [ValueError, CancelledError]).} =
## Returns a future which will complete only when all futures in ``futs``
## will be completed, failed or canceled.
##
## If the argument is empty, the returned future COMPLETES immediately.
##
## On cancel all the awaited futures ``futs`` WILL NOT BE cancelled.
# Because we can't capture varargs[T] in closures we need to create copy.
race(futs.mapIt(FutureBase(it)))

proc race*[T, E](futs: varargs[InternalRaisesFuture[T, E]]): Future[FutureBase] {.
async: (raw: true, raises: [ValueError, CancelledError]).} =
## Returns a future which will complete only when all futures in ``futs``
## will be completed, failed or canceled.
##
## If the argument is empty, the returned future COMPLETES immediately.
##
## On cancel all the awaited futures ``futs`` WILL NOT BE cancelled.
# Because we can't capture varargs[T] in closures we need to create copy.
race(futs.mapIt(FutureBase(it)))

when (chronosEventEngine in ["epoll", "kqueue"]) or defined(windows):
import std/os

Expand Down
13 changes: 9 additions & 4 deletions tests/testfut.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1314,12 +1314,17 @@ suite "Future[T] behavior test suite":
test "race(zero) test":
var tseq = newSeq[FutureBase]()
var fut1 = race(tseq)
var fut2 = race()
var fut3 = race([])
check:
# https://github.com/nim-lang/Nim/issues/22964
not compiles(block:
var fut2 = race())
not compiles(block:
var fut3 = race([]))

check:
fut1.failed()
fut2.failed()
fut3.failed()
# fut2.failed()
# fut3.failed()

asyncTest "race(varargs) test":
proc vlient1() {.async.} =
Expand Down

0 comments on commit fa0bf40

Please sign in to comment.