mirror of
https://github.com/status-im/nim-chronos.git
synced 2025-02-21 23:48:15 +00:00
Let complete/fail cancelled Future[T]/FutureVar[T] for free.
This commit is contained in:
parent
7d7753b7fd
commit
15b25140b4
@ -210,6 +210,7 @@ proc remove(callbacks: var Deque[AsyncCallback], item: AsyncCallback) =
|
||||
p.deleted = true
|
||||
|
||||
proc complete[T](future: Future[T], val: T, loc: ptr SrcLoc) =
|
||||
if not(future.cancelled()):
|
||||
checkFinished(future, loc)
|
||||
doAssert(isNil(future.error))
|
||||
future.value = val
|
||||
@ -221,16 +222,18 @@ template complete*[T](future: Future[T], val: T) =
|
||||
complete(future, val, getSrcLocation())
|
||||
|
||||
proc complete(future: Future[void], loc: ptr SrcLoc) =
|
||||
## Completes a void ``future``.
|
||||
if not(future.cancelled()):
|
||||
checkFinished(future, loc)
|
||||
doAssert(isNil(future.error))
|
||||
future.state = FutureState.Finished
|
||||
future.callbacks.call()
|
||||
|
||||
template complete*(future: Future[void]) =
|
||||
## Completes a void ``future``.
|
||||
complete(future, getSrcLocation())
|
||||
|
||||
proc complete[T](future: FutureVar[T], loc: ptr SrcLoc) =
|
||||
if not(future.cancelled()):
|
||||
template fut: untyped = Future[T](future)
|
||||
checkFinished(fut, loc)
|
||||
doAssert(isNil(fut.error))
|
||||
@ -242,6 +245,7 @@ template complete*[T](futvar: FutureVar[T]) =
|
||||
complete(futvar, getSrcLocation())
|
||||
|
||||
proc complete[T](futvar: FutureVar[T], val: T, loc: ptr SrcLoc) =
|
||||
if not(futvar.cancelled()):
|
||||
template fut: untyped = Future[T](futvar)
|
||||
checkFinished(fut, loc)
|
||||
doAssert(isNil(fut.error))
|
||||
@ -256,6 +260,7 @@ template complete*[T](futvar: FutureVar[T], val: T) =
|
||||
complete(futvar, val, getSrcLocation())
|
||||
|
||||
proc fail[T](future: Future[T], error: ref Exception, loc: ptr SrcLoc) =
|
||||
if not(future.cancelled()):
|
||||
checkFinished(future, loc)
|
||||
future.state = FutureState.Failed
|
||||
future.error = error
|
||||
|
Loading…
x
Reference in New Issue
Block a user