varargs overloads (#477)

* varargs overloads

for convenience and compatibility

* no parameterless varargs calls with generic overloads
This commit is contained in:
Jacek Sieka 2023-11-20 11:04:28 +01:00 committed by GitHub
parent f03cdfcc40
commit fa0bf405e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 8 deletions

View File

@ -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]).} =
@ -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

View File

@ -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.} =