Update future to result utils.
This commit is contained in:
parent
cba704d45d
commit
3e68f916f6
|
@ -1,19 +1,30 @@
|
||||||
import chronos/futures, stew/results, chronos
|
import chronos/futures, stew/results, chronos
|
||||||
|
import ./futures
|
||||||
|
|
||||||
|
proc toOk(future: Future[void]): Result[void, string] =
|
||||||
|
return results.ok()
|
||||||
|
|
||||||
|
proc toOk[T](future: Future[T]): Result[T, string] =
|
||||||
|
return results.ok(future.read())
|
||||||
|
|
||||||
|
proc toResult*[T](future: Future[T]): Result[T, string] =
|
||||||
|
if future.cancelled():
|
||||||
|
return results.err("Future cancelled/timed out.")
|
||||||
|
elif future.finished():
|
||||||
|
if not future.failed():
|
||||||
|
return future.toOk()
|
||||||
|
else:
|
||||||
|
return results.err("Future finished but failed.")
|
||||||
|
else:
|
||||||
|
return results.err("Future still not finished.")
|
||||||
|
|
||||||
proc waitForResult*[T](
|
proc waitForResult*[T](
|
||||||
fut: Future[T], timeout: Duration = 1.seconds
|
future: Future[T], timeout = DURATION_TIMEOUT
|
||||||
): Future[Result[T, string]] {.async.} =
|
): Future[Result[T, string]] {.async.} =
|
||||||
try:
|
discard await future.withTimeout(timeout)
|
||||||
let val = await fut.wait(timeout)
|
return future.toResult()
|
||||||
return Result[T, string].ok(val)
|
|
||||||
except Exception as e:
|
|
||||||
return Result[T, string].err(e.msg)
|
|
||||||
|
|
||||||
proc waitForResult*(
|
proc reset*[T](future: Future[T]): void =
|
||||||
fut: Future[void], timeout: Duration = 1.seconds
|
# Likely an incomplete reset, but good enough for testing purposes (for now)
|
||||||
): Future[Result[void, string]] {.async.} =
|
future.internalError = nil
|
||||||
try:
|
future.internalState = FutureState.Pending
|
||||||
await fut.wait(timeout)
|
|
||||||
return Result[void, string].ok()
|
|
||||||
except Exception as e:
|
|
||||||
return Result[void, string].err(e.msg)
|
|
||||||
|
|
Loading…
Reference in New Issue